From fb3242df0a6d32257fb36dd3a90bc0a7881968ab Mon Sep 17 00:00:00 2001 From: Jessica Chowdhury <67977755+JessChowdhury@users.noreply.github.com> Date: Fri, 22 Nov 2024 10:37:54 -0500 Subject: [PATCH] feat: add skip and force accept flags to migration commands (#8843) 1. Adds flag `--skip-empty` to `migrate:create` to bypass the empty migration file prompt. - Blank migration file will not be created if this flag is passed. 3. Adds flag `--force-accept-warning` to `migrate:fresh` to bypass the drop database prompt --- packages/db-mongodb/src/createMigration.ts | 6 ++++- packages/db-sqlite/src/createMigration.ts | 8 +++++-- .../drizzle/src/postgres/createMigration.ts | 6 ++++- packages/payload/src/bin/migrate.ts | 23 ++++++++++++++++++- packages/payload/src/database/types.ts | 7 +++--- 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/packages/db-mongodb/src/createMigration.ts b/packages/db-mongodb/src/createMigration.ts index 3475baba8..c99eee52c 100644 --- a/packages/db-mongodb/src/createMigration.ts +++ b/packages/db-mongodb/src/createMigration.ts @@ -23,6 +23,7 @@ export const createMigration: CreateMigration = async function createMigration({ file, migrationName, payload, + skipEmpty, }) { const filename = fileURLToPath(import.meta.url) const dirname = path.dirname(filename) @@ -49,7 +50,10 @@ export const createMigration: CreateMigration = async function createMigration({ const formattedName = migrationName?.replace(/\W/g, '_') const fileName = migrationName ? `${timestamp}_${formattedName}.ts` : `${timestamp}_migration.ts` const filePath = `${dir}/${fileName}` - fs.writeFileSync(filePath, migrationFileContent) + + if (!skipEmpty) { + fs.writeFileSync(filePath, migrationFileContent) + } writeMigrationIndex({ migrationsDir: payload.db.migrationDir }) diff --git a/packages/db-sqlite/src/createMigration.ts b/packages/db-sqlite/src/createMigration.ts index 4beb5acab..e9c0505d3 100644 --- a/packages/db-sqlite/src/createMigration.ts +++ b/packages/db-sqlite/src/createMigration.ts @@ -17,7 +17,7 @@ const require = createRequire(import.meta.url) export const createMigration: CreateMigration = async function createMigration( this: SQLiteAdapter, - { file, forceAcceptWarning, migrationName, payload }, + { file, migrationName, payload, skipEmpty }, ) { const filename = fileURLToPath(import.meta.url) const dirname = path.dirname(filename) @@ -79,7 +79,11 @@ export const createMigration: CreateMigration = async function createMigration( .join('\n') } - if (!upSQL?.length && !downSQL?.length && !forceAcceptWarning) { + if (!upSQL?.length && !downSQL?.length) { + if (skipEmpty) { + process.exit(0) + } + const { confirm: shouldCreateBlankMigration } = await prompts( { name: 'confirm', diff --git a/packages/drizzle/src/postgres/createMigration.ts b/packages/drizzle/src/postgres/createMigration.ts index 30790cd71..04e5879ca 100644 --- a/packages/drizzle/src/postgres/createMigration.ts +++ b/packages/drizzle/src/postgres/createMigration.ts @@ -14,7 +14,7 @@ const require = createRequire(import.meta.url) export const createMigration: CreateMigration = async function createMigration( this: BasePostgresAdapter, - { dirname, file, forceAcceptWarning, migrationName, payload }, + { dirname, file, forceAcceptWarning, migrationName, payload, skipEmpty }, ) { const dir = payload.db.migrationDir if (!fs.existsSync(dir)) { @@ -78,6 +78,10 @@ export const createMigration: CreateMigration = async function createMigration( } if (!upSQL?.length && !downSQL?.length && !forceAcceptWarning) { + if (skipEmpty) { + process.exit(0) + } + const { confirm: shouldCreateBlankMigration } = await prompts( { name: 'confirm', diff --git a/packages/payload/src/bin/migrate.ts b/packages/payload/src/bin/migrate.ts index daec12ee2..db4d2aa45 100644 --- a/packages/payload/src/bin/migrate.ts +++ b/packages/payload/src/bin/migrate.ts @@ -32,7 +32,27 @@ type Args = { } export const migrate = async ({ config, parsedArgs }: Args): Promise => { - const { _: args, file, forceAcceptWarning, help } = parsedArgs + const { _: args, file, forceAcceptWarning: forceAcceptFromProps, help } = parsedArgs + + const formattedArgs = Object.keys(parsedArgs) + .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 skipEmpty = formattedArgs.includes('skipEmpty') + if (help) { // eslint-disable-next-line no-console console.log(`\n\n${availableCommandsMsg}\n`) // Avoid having to init payload to get the logger @@ -72,6 +92,7 @@ export const migrate = async ({ config, parsedArgs }: Args): Promise => { forceAcceptWarning, migrationName: args[1], payload, + skipEmpty, }) } catch (err) { throw new Error(`Error creating migration: ${err.message}`) diff --git a/packages/payload/src/database/types.ts b/packages/payload/src/database/types.ts index f138db9d6..d7697e0cd 100644 --- a/packages/payload/src/database/types.ts +++ b/packages/payload/src/database/types.ts @@ -164,12 +164,13 @@ export type CreateMigration = (args: { /** dirname of the package, required in drizzle */ dirname?: string file?: string - /** - * Skips the prompt asking to create empty migrations - */ forceAcceptWarning?: boolean migrationName?: string payload: Payload + /** + * Skips the prompt asking to create empty migrations + */ + skipEmpty?: boolean }) => Promise | void export type Transaction = (