chore: rework pg migration creation
This commit is contained in:
@@ -91,44 +91,5 @@ export const connect: Connect = async function connect(this: PostgresAdapter, pa
|
||||
}
|
||||
}
|
||||
|
||||
this.migrationDir = '.migrations'
|
||||
|
||||
// Create drizzle snapshot if it doesn't exist
|
||||
if (!fs.existsSync(`${this.migrationDir}/drizzle-snapshot.json`)) {
|
||||
// Ensure migration dir exists
|
||||
if (!fs.existsSync(this.migrationDir)) {
|
||||
fs.mkdirSync(this.migrationDir)
|
||||
}
|
||||
|
||||
const drizzleJSON = generateDrizzleJson(this.schema)
|
||||
|
||||
fs.writeFileSync(
|
||||
`${this.migrationDir}/drizzle-snapshot.json`,
|
||||
JSON.stringify(drizzleJSON, null, 2),
|
||||
)
|
||||
}
|
||||
|
||||
const jsonSchema = configToJSONSchema(this.payload.config)
|
||||
|
||||
await apply()
|
||||
|
||||
const devPush = await this.db
|
||||
.select()
|
||||
.from(migrationsSchema)
|
||||
.where(eq(migrationsSchema.batch, '-1'))
|
||||
|
||||
if (!devPush.length) {
|
||||
await this.db.insert(migrationsSchema).values({
|
||||
name: 'dev',
|
||||
batch: '-1',
|
||||
schema: JSON.stringify(jsonSchema),
|
||||
})
|
||||
} else {
|
||||
await this.db
|
||||
.update(migrationsSchema)
|
||||
.set({
|
||||
schema: JSON.stringify(jsonSchema),
|
||||
})
|
||||
.where(eq(migrationsSchema.batch, '-1'))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
/* eslint-disable no-restricted-syntax, no-await-in-loop */
|
||||
import type { DrizzleSnapshotJSON } from 'drizzle-kit/utils'
|
||||
import type { CreateMigration } from 'payload/database'
|
||||
|
||||
import { generateDrizzleJson, generateMigration } from 'drizzle-kit/utils'
|
||||
@@ -25,7 +26,7 @@ export const createMigration: CreateMigration = async function createMigration(
|
||||
migrationName,
|
||||
) {
|
||||
payload.logger.info({ msg: 'Creating migration from postgres adapter...' })
|
||||
const dir = migrationDir || '.migrations' // TODO: Verify path after linking
|
||||
const dir = migrationDir || 'migrations'
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir)
|
||||
}
|
||||
@@ -40,22 +41,35 @@ export const createMigration: CreateMigration = async function createMigration(
|
||||
const fileName = `${timestamp}_${formattedName}.ts`
|
||||
const filePath = `${dir}/${fileName}`
|
||||
|
||||
const snapshotJSON = fs.readFileSync(`${dir}/drizzle-snapshot.json`, 'utf8')
|
||||
const drizzleJsonBefore = generateDrizzleJson(JSON.parse(snapshotJSON))
|
||||
const drizzleJsonAfter = generateDrizzleJson(this.schema, drizzleJsonBefore.id)
|
||||
const sqlStatements = await generateMigration(drizzleJsonBefore, drizzleJsonAfter)
|
||||
const migrationQuery = await payload.find({
|
||||
collection: 'payload-migrations',
|
||||
limit: 1,
|
||||
sort: '-name',
|
||||
})
|
||||
|
||||
const drizzleJsonBefore = migrationQuery.docs[0]?.schema as DrizzleSnapshotJSON
|
||||
|
||||
const drizzleJsonAfter = generateDrizzleJson(this.schema)
|
||||
const sqlStatements = await generateMigration(
|
||||
drizzleJsonBefore || {
|
||||
id: '00000000-0000-0000-0000-000000000000',
|
||||
_meta: {
|
||||
columns: {},
|
||||
schemas: {},
|
||||
tables: {},
|
||||
},
|
||||
dialect: 'pg',
|
||||
enums: {},
|
||||
prevId: '00000000-0000-0000-0000-000000000000',
|
||||
schemas: {},
|
||||
tables: {},
|
||||
version: '5',
|
||||
},
|
||||
drizzleJsonAfter,
|
||||
)
|
||||
|
||||
fs.writeFileSync(
|
||||
filePath,
|
||||
migrationTemplate(sqlStatements.length ? sqlStatements?.join('\n') : undefined),
|
||||
)
|
||||
|
||||
// TODO:
|
||||
// Get the most recent migration schema from the file system
|
||||
// we will use that as the "before"
|
||||
// then for after, we will call `connect` and `init` to create the new schema dynamically
|
||||
// once we have new schema created, we will convert it to JSON using generateDrizzleJSON
|
||||
// we then run `generateMigration` to get a list of SQL statements to pair 'em up
|
||||
// and then inject them each into the `migrationTemplate` above,
|
||||
// outputting the file into the migrations folder accordingly
|
||||
// also make sure to output the JSON schema snapshot into a `./migrationsDir/meta` folder like Drizzle does
|
||||
}
|
||||
|
||||
57
packages/db-postgres/src/migrate.ts
Normal file
57
packages/db-postgres/src/migrate.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
/* eslint-disable no-restricted-syntax, no-await-in-loop */
|
||||
import type { DatabaseAdapter } from 'payload/database'
|
||||
import type { PayloadRequest } from 'payload/types'
|
||||
|
||||
import { generateDrizzleJson } from 'drizzle-kit/utils'
|
||||
import { readMigrationFiles } from 'payload/database'
|
||||
|
||||
import type { PostgresAdapter } from './types'
|
||||
|
||||
export async function migrate(this: PostgresAdapter): Promise<void> {
|
||||
const { payload } = this
|
||||
const migrationFiles = await readMigrationFiles({ payload })
|
||||
const migrationQuery = await payload.find({
|
||||
collection: 'payload-migrations',
|
||||
limit: 0,
|
||||
sort: '-name',
|
||||
})
|
||||
|
||||
const latestBatch = Number(migrationQuery.docs[0]?.batch ?? 0)
|
||||
|
||||
const newBatch = latestBatch + 1
|
||||
|
||||
// Execute 'up' function for each migration sequentially
|
||||
for (const migration of migrationFiles) {
|
||||
const existingMigration = migrationQuery.docs.find(
|
||||
(existing) => existing.name === migration.name,
|
||||
)
|
||||
|
||||
// Run migration if not found in database
|
||||
if (existingMigration) {
|
||||
continue // eslint-disable-line no-continue
|
||||
}
|
||||
|
||||
const start = Date.now()
|
||||
|
||||
payload.logger.info({ msg: `Migrating: ${migration.name}` })
|
||||
|
||||
const pgAdapter = payload.db as PostgresAdapter // TODO: Fix this typing
|
||||
const drizzleJSON = generateDrizzleJson(pgAdapter.schema)
|
||||
|
||||
try {
|
||||
await migration.up({ payload })
|
||||
payload.logger.info({ msg: `Migrated: ${migration.name} (${Date.now() - start}ms)` })
|
||||
await payload.create({
|
||||
collection: 'payload-migrations',
|
||||
data: {
|
||||
name: migration.name,
|
||||
batch: newBatch,
|
||||
schema: drizzleJSON,
|
||||
},
|
||||
})
|
||||
} catch (err: unknown) {
|
||||
payload.logger.error({ err, msg: `Error running migration ${migration.name}` })
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user