Skip to content

Commit

Permalink
feat: add skip and force accept flags to migration commands (#8843)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JessChowdhury authored Nov 22, 2024
1 parent 251c4c3 commit fb3242d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
6 changes: 5 additions & 1 deletion packages/db-mongodb/src/createMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 })

Expand Down
8 changes: 6 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, migrationName, payload, skipEmpty },
) {
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
Expand Down Expand Up @@ -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',
Expand Down
6 changes: 5 additions & 1 deletion packages/drizzle/src/postgres/createMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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',
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)
.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
Expand Down Expand Up @@ -72,6 +92,7 @@ export const migrate = async ({ config, parsedArgs }: Args): Promise<void> => {
forceAcceptWarning,
migrationName: args[1],
payload,
skipEmpty,
})
} catch (err) {
throw new Error(`Error creating migration: ${err.message}`)
Expand Down
7 changes: 4 additions & 3 deletions packages/payload/src/database/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> | void

export type Transaction = (
Expand Down

0 comments on commit fb3242d

Please sign in to comment.