Merge branch '2.0' of github.com:payloadcms/payload into 2.0
This commit is contained in:
@@ -11,6 +11,7 @@ import { migrationTableExists } from './utilities/migrationTableExists'
|
||||
|
||||
const migrationTemplate = (
|
||||
upSQL?: string,
|
||||
downSQL?: string,
|
||||
) => `import { MigrateUpArgs, MigrateDownArgs } from '@payloadcms/db-postgres/types'
|
||||
import { sql } from 'drizzle-orm'
|
||||
|
||||
@@ -24,11 +25,32 @@ ${upSQL}\`);
|
||||
}
|
||||
};
|
||||
|
||||
export async function down({ payload }: MigrateDownArgs): Promise<void> {
|
||||
// Migration code
|
||||
export async function down({ payload }: MigrateUpArgs): Promise<void> {
|
||||
${
|
||||
downSQL
|
||||
? `await payload.db.db.execute(sql\`
|
||||
${downSQL}\`);
|
||||
`
|
||||
: '// Migration code'
|
||||
}
|
||||
};
|
||||
`
|
||||
|
||||
const getDefaultDrizzleSnapshot = (): DrizzleSnapshotJSON => ({
|
||||
id: '00000000-0000-0000-0000-000000000000',
|
||||
_meta: {
|
||||
columns: {},
|
||||
schemas: {},
|
||||
tables: {},
|
||||
},
|
||||
dialect: 'pg',
|
||||
enums: {},
|
||||
prevId: '00000000-0000-0000-0000-00000000000',
|
||||
schemas: {},
|
||||
tables: {},
|
||||
version: '5',
|
||||
})
|
||||
|
||||
export const createMigration: CreateMigration = async function createMigration(
|
||||
this: PostgresAdapter,
|
||||
payload,
|
||||
@@ -50,20 +72,7 @@ export const createMigration: CreateMigration = async function createMigration(
|
||||
const fileName = `${timestamp}_${formattedName}.ts`
|
||||
const filePath = `${dir}/${fileName}`
|
||||
|
||||
let drizzleJsonBefore: DrizzleSnapshotJSON = {
|
||||
id: '00000000-0000-0000-0000-000000000000',
|
||||
_meta: {
|
||||
columns: {},
|
||||
schemas: {},
|
||||
tables: {},
|
||||
},
|
||||
dialect: 'pg',
|
||||
enums: {},
|
||||
prevId: '00000000-0000-0000-0000-000000000000',
|
||||
schemas: {},
|
||||
tables: {},
|
||||
version: '5',
|
||||
}
|
||||
let drizzleJsonBefore = getDefaultDrizzleSnapshot()
|
||||
|
||||
const hasMigrationTable = await migrationTableExists(this.db)
|
||||
|
||||
@@ -80,11 +89,15 @@ export const createMigration: CreateMigration = async function createMigration(
|
||||
}
|
||||
|
||||
const drizzleJsonAfter = generateDrizzleJson(this.schema)
|
||||
const sqlStatements = await generateMigration(drizzleJsonBefore, drizzleJsonAfter)
|
||||
const sqlStatementsUp = await generateMigration(drizzleJsonBefore, drizzleJsonAfter)
|
||||
const sqlStatementsDown = await generateMigration(drizzleJsonAfter, drizzleJsonBefore)
|
||||
|
||||
fs.writeFileSync(
|
||||
filePath,
|
||||
migrationTemplate(sqlStatements.length ? sqlStatements?.join('\n') : undefined),
|
||||
migrationTemplate(
|
||||
sqlStatementsUp.length ? sqlStatementsUp?.join('\n') : undefined,
|
||||
sqlStatementsDown.length ? sqlStatementsDown?.join('\n') : undefined,
|
||||
),
|
||||
)
|
||||
payload.logger.info({ msg: `Migration created at ${filePath}` })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user