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:
committed by
GitHub
parent
251c4c30d7
commit
fb3242df0a
@@ -23,6 +23,7 @@ export const createMigration: CreateMigration = async function createMigration({
|
|||||||
file,
|
file,
|
||||||
migrationName,
|
migrationName,
|
||||||
payload,
|
payload,
|
||||||
|
skipEmpty,
|
||||||
}) {
|
}) {
|
||||||
const filename = fileURLToPath(import.meta.url)
|
const filename = fileURLToPath(import.meta.url)
|
||||||
const dirname = path.dirname(filename)
|
const dirname = path.dirname(filename)
|
||||||
@@ -49,7 +50,10 @@ export const createMigration: CreateMigration = async function createMigration({
|
|||||||
const formattedName = migrationName?.replace(/\W/g, '_')
|
const formattedName = migrationName?.replace(/\W/g, '_')
|
||||||
const fileName = migrationName ? `${timestamp}_${formattedName}.ts` : `${timestamp}_migration.ts`
|
const fileName = migrationName ? `${timestamp}_${formattedName}.ts` : `${timestamp}_migration.ts`
|
||||||
const filePath = `${dir}/${fileName}`
|
const filePath = `${dir}/${fileName}`
|
||||||
fs.writeFileSync(filePath, migrationFileContent)
|
|
||||||
|
if (!skipEmpty) {
|
||||||
|
fs.writeFileSync(filePath, migrationFileContent)
|
||||||
|
}
|
||||||
|
|
||||||
writeMigrationIndex({ migrationsDir: payload.db.migrationDir })
|
writeMigrationIndex({ migrationsDir: payload.db.migrationDir })
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ const require = createRequire(import.meta.url)
|
|||||||
|
|
||||||
export const createMigration: CreateMigration = async function createMigration(
|
export const createMigration: CreateMigration = async function createMigration(
|
||||||
this: SQLiteAdapter,
|
this: SQLiteAdapter,
|
||||||
{ file, forceAcceptWarning, migrationName, payload },
|
{ file, migrationName, payload, skipEmpty },
|
||||||
) {
|
) {
|
||||||
const filename = fileURLToPath(import.meta.url)
|
const filename = fileURLToPath(import.meta.url)
|
||||||
const dirname = path.dirname(filename)
|
const dirname = path.dirname(filename)
|
||||||
@@ -79,7 +79,11 @@ export const createMigration: CreateMigration = async function createMigration(
|
|||||||
.join('\n')
|
.join('\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!upSQL?.length && !downSQL?.length && !forceAcceptWarning) {
|
if (!upSQL?.length && !downSQL?.length) {
|
||||||
|
if (skipEmpty) {
|
||||||
|
process.exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
const { confirm: shouldCreateBlankMigration } = await prompts(
|
const { confirm: shouldCreateBlankMigration } = await prompts(
|
||||||
{
|
{
|
||||||
name: 'confirm',
|
name: 'confirm',
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ const require = createRequire(import.meta.url)
|
|||||||
|
|
||||||
export const createMigration: CreateMigration = async function createMigration(
|
export const createMigration: CreateMigration = async function createMigration(
|
||||||
this: BasePostgresAdapter,
|
this: BasePostgresAdapter,
|
||||||
{ dirname, file, forceAcceptWarning, migrationName, payload },
|
{ dirname, file, forceAcceptWarning, migrationName, payload, skipEmpty },
|
||||||
) {
|
) {
|
||||||
const dir = payload.db.migrationDir
|
const dir = payload.db.migrationDir
|
||||||
if (!fs.existsSync(dir)) {
|
if (!fs.existsSync(dir)) {
|
||||||
@@ -78,6 +78,10 @@ export const createMigration: CreateMigration = async function createMigration(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!upSQL?.length && !downSQL?.length && !forceAcceptWarning) {
|
if (!upSQL?.length && !downSQL?.length && !forceAcceptWarning) {
|
||||||
|
if (skipEmpty) {
|
||||||
|
process.exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
const { confirm: shouldCreateBlankMigration } = await prompts(
|
const { confirm: shouldCreateBlankMigration } = await prompts(
|
||||||
{
|
{
|
||||||
name: 'confirm',
|
name: 'confirm',
|
||||||
|
|||||||
@@ -32,7 +32,27 @@ type Args = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const migrate = async ({ config, parsedArgs }: Args): Promise<void> => {
|
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) {
|
if (help) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(`\n\n${availableCommandsMsg}\n`) // Avoid having to init payload to get the logger
|
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,
|
forceAcceptWarning,
|
||||||
migrationName: args[1],
|
migrationName: args[1],
|
||||||
payload,
|
payload,
|
||||||
|
skipEmpty,
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(`Error creating migration: ${err.message}`)
|
throw new Error(`Error creating migration: ${err.message}`)
|
||||||
|
|||||||
@@ -164,12 +164,13 @@ export type CreateMigration = (args: {
|
|||||||
/** dirname of the package, required in drizzle */
|
/** dirname of the package, required in drizzle */
|
||||||
dirname?: string
|
dirname?: string
|
||||||
file?: string
|
file?: string
|
||||||
/**
|
|
||||||
* Skips the prompt asking to create empty migrations
|
|
||||||
*/
|
|
||||||
forceAcceptWarning?: boolean
|
forceAcceptWarning?: boolean
|
||||||
migrationName?: string
|
migrationName?: string
|
||||||
payload: Payload
|
payload: Payload
|
||||||
|
/**
|
||||||
|
* Skips the prompt asking to create empty migrations
|
||||||
|
*/
|
||||||
|
skipEmpty?: boolean
|
||||||
}) => Promise<void> | void
|
}) => Promise<void> | void
|
||||||
|
|
||||||
export type Transaction = (
|
export type Transaction = (
|
||||||
|
|||||||
Reference in New Issue
Block a user