Skip to content

Commit fb3242d

Browse files
authored
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
1 parent 251c4c3 commit fb3242d

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

packages/db-mongodb/src/createMigration.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const createMigration: CreateMigration = async function createMigration({
2323
file,
2424
migrationName,
2525
payload,
26+
skipEmpty,
2627
}) {
2728
const filename = fileURLToPath(import.meta.url)
2829
const dirname = path.dirname(filename)
@@ -49,7 +50,10 @@ export const createMigration: CreateMigration = async function createMigration({
4950
const formattedName = migrationName?.replace(/\W/g, '_')
5051
const fileName = migrationName ? `${timestamp}_${formattedName}.ts` : `${timestamp}_migration.ts`
5152
const filePath = `${dir}/${fileName}`
52-
fs.writeFileSync(filePath, migrationFileContent)
53+
54+
if (!skipEmpty) {
55+
fs.writeFileSync(filePath, migrationFileContent)
56+
}
5357

5458
writeMigrationIndex({ migrationsDir: payload.db.migrationDir })
5559

packages/db-sqlite/src/createMigration.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const require = createRequire(import.meta.url)
1717

1818
export const createMigration: CreateMigration = async function createMigration(
1919
this: SQLiteAdapter,
20-
{ file, forceAcceptWarning, migrationName, payload },
20+
{ file, migrationName, payload, skipEmpty },
2121
) {
2222
const filename = fileURLToPath(import.meta.url)
2323
const dirname = path.dirname(filename)
@@ -79,7 +79,11 @@ export const createMigration: CreateMigration = async function createMigration(
7979
.join('\n')
8080
}
8181

82-
if (!upSQL?.length && !downSQL?.length && !forceAcceptWarning) {
82+
if (!upSQL?.length && !downSQL?.length) {
83+
if (skipEmpty) {
84+
process.exit(0)
85+
}
86+
8387
const { confirm: shouldCreateBlankMigration } = await prompts(
8488
{
8589
name: 'confirm',

packages/drizzle/src/postgres/createMigration.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const require = createRequire(import.meta.url)
1414

1515
export const createMigration: CreateMigration = async function createMigration(
1616
this: BasePostgresAdapter,
17-
{ dirname, file, forceAcceptWarning, migrationName, payload },
17+
{ dirname, file, forceAcceptWarning, migrationName, payload, skipEmpty },
1818
) {
1919
const dir = payload.db.migrationDir
2020
if (!fs.existsSync(dir)) {
@@ -78,6 +78,10 @@ export const createMigration: CreateMigration = async function createMigration(
7878
}
7979

8080
if (!upSQL?.length && !downSQL?.length && !forceAcceptWarning) {
81+
if (skipEmpty) {
82+
process.exit(0)
83+
}
84+
8185
const { confirm: shouldCreateBlankMigration } = await prompts(
8286
{
8387
name: 'confirm',

packages/payload/src/bin/migrate.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,27 @@ type Args = {
3232
}
3333

3434
export const migrate = async ({ config, parsedArgs }: Args): Promise<void> => {
35-
const { _: args, file, forceAcceptWarning, help } = parsedArgs
35+
const { _: args, file, forceAcceptWarning: forceAcceptFromProps, help } = parsedArgs
36+
37+
const formattedArgs = Object.keys(parsedArgs)
38+
.map((key) => {
39+
const formattedKey = key.replace(/^[-_]+/, '')
40+
if (!formattedKey) {
41+
return null
42+
}
43+
44+
return formattedKey
45+
.split('-')
46+
.map((word, index) =>
47+
index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1),
48+
)
49+
.join('')
50+
})
51+
.filter(Boolean)
52+
53+
const forceAcceptWarning = forceAcceptFromProps || formattedArgs.includes('forceAcceptWarning')
54+
const skipEmpty = formattedArgs.includes('skipEmpty')
55+
3656
if (help) {
3757
// eslint-disable-next-line no-console
3858
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<void> => {
7292
forceAcceptWarning,
7393
migrationName: args[1],
7494
payload,
95+
skipEmpty,
7596
})
7697
} catch (err) {
7798
throw new Error(`Error creating migration: ${err.message}`)

packages/payload/src/database/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,13 @@ export type CreateMigration = (args: {
164164
/** dirname of the package, required in drizzle */
165165
dirname?: string
166166
file?: string
167-
/**
168-
* Skips the prompt asking to create empty migrations
169-
*/
170167
forceAcceptWarning?: boolean
171168
migrationName?: string
172169
payload: Payload
170+
/**
171+
* Skips the prompt asking to create empty migrations
172+
*/
173+
skipEmpty?: boolean
173174
}) => Promise<void> | void
174175

175176
export type Transaction = (

0 commit comments

Comments
 (0)