Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add skip and force accept flags to migration commands #8843

Merged
merged 10 commits into from
Nov 22, 2024
2 changes: 1 addition & 1 deletion packages/db-mongodb/src/createMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const migrationTemplate = ({ downSQL, imports, upSQL }: MigrationTemplateArgs):
MigrateUpArgs,
MigrateDownArgs,
} from '@payloadcms/db-mongodb'
${imports}
${imports ? imports : ''}
denolfe marked this conversation as resolved.
Show resolved Hide resolved

export async function up({ payload, req }: MigrateUpArgs): Promise<void> {
${upSQL ?? ` // Migration code`}
Expand Down
4 changes: 2 additions & 2 deletions packages/db-sqlite/src/createMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const require = createRequire(import.meta.url)

export const createMigration: CreateMigration = async function createMigration(
this: SQLiteAdapter,
{ file, forceAcceptWarning, migrationName, payload },
{ file, forceAcceptWarning, migrationName, payload, skipVerify },
) {
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
Expand Down Expand Up @@ -79,7 +79,7 @@ export const createMigration: CreateMigration = async function createMigration(
.join('\n')
}

if (!upSQL?.length && !downSQL?.length && !forceAcceptWarning) {
if (!upSQL?.length && !downSQL?.length && !forceAcceptWarning && !skipVerify) {
denolfe marked this conversation as resolved.
Show resolved Hide resolved
const { confirm: shouldCreateBlankMigration } = await prompts(
{
name: 'confirm',
Expand Down
4 changes: 2 additions & 2 deletions packages/drizzle/src/postgres/createMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const require = createRequire(import.meta.url)

export const createMigration: CreateMigration = async function createMigration(
this: BasePostgresAdapter,
{ file, forceAcceptWarning, migrationName, payload },
{ file, forceAcceptWarning, migrationName, payload, skipVerify },
) {
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
Expand Down Expand Up @@ -81,7 +81,7 @@ export const createMigration: CreateMigration = async function createMigration(
downSQL = `${sqlExecute}\n ${sqlStatementsDown?.join('\n')}\`)`
}

if (!upSQL?.length && !downSQL?.length && !forceAcceptWarning) {
if (!upSQL?.length && !downSQL?.length && !forceAcceptWarning && !skipVerify) {
const { confirm: shouldCreateBlankMigration } = await prompts(
{
name: 'confirm',
Expand Down
23 changes: 22 additions & 1 deletion packages/payload/src/bin/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,27 @@ type Args = {
}

export const migrate = async ({ config, parsedArgs }: Args): Promise<void> => {
const { _: args, file, forceAcceptWarning, help } = parsedArgs
const { _: args, file, forceAcceptWarning: forceAcceptFromProps, help } = parsedArgs

const formattedArgs = Object.keys(parsedArgs || {})
denolfe marked this conversation as resolved.
Show resolved Hide resolved
.map((key) => {
const formattedKey = key.replace(/^[-_]+/, '')
if (!formattedKey) {
return null
}

return formattedKey
.split('-')
.map((word, index) =>
index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1),
)
.join('')
})
.filter(Boolean)

const forceAcceptWarning = forceAcceptFromProps || formattedArgs?.includes('forceAcceptWarning')
const skipVerify = formattedArgs?.includes('skipVerify')
denolfe marked this conversation as resolved.
Show resolved Hide resolved

if (help) {
// eslint-disable-next-line no-console
console.log(`\n\n${availableCommandsMsg}\n`) // Avoid having to init payload to get the logger
Expand Down Expand Up @@ -87,6 +107,7 @@ export const migrate = async ({ config, parsedArgs }: Args): Promise<void> => {
forceAcceptWarning,
migrationName: args[1],
payload,
skipVerify,
})
} catch (err) {
throw new Error(`Error creating migration: ${err.message}`)
Expand Down
1 change: 1 addition & 0 deletions packages/payload/src/database/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export type CreateMigration = (args: {
forceAcceptWarning?: boolean
migrationName?: string
payload: Payload
skipVerify?: boolean
}) => Promise<void> | void

export type Transaction = (
Expand Down
Loading