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
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