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
This commit is contained in:
Jessica Chowdhury
2024-11-22 10:37:54 -05:00
committed by GitHub
parent 251c4c30d7
commit fb3242df0a
5 changed files with 42 additions and 8 deletions

View File

@@ -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 })

View File

@@ -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',

View File

@@ -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',

View File

@@ -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
@@ -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}`)

View File

@@ -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 = (