Skip to content

Commit

Permalink
fix(db-postgres): migrate:create errors with previous schemas (#8786)
Browse files Browse the repository at this point in the history
Fixes #8782
  • Loading branch information
r1tsuu authored Oct 18, 2024
1 parent c8ed645 commit e9c1222
Show file tree
Hide file tree
Showing 6 changed files with 2,380 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/db-postgres/src/createMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const createMigration: CreateMigration = async function createMigration(
fs.mkdirSync(dir)
}

const { generateDrizzleJson, generateMigration } = require('drizzle-kit/api')
const { generateDrizzleJson, generateMigration, upPgSnapshot } = require('drizzle-kit/api')

const [yyymmdd, hhmmss] = new Date().toISOString().split('T')
const formattedDate = yyymmdd.replace(/\D/g, '')
Expand Down Expand Up @@ -99,6 +99,11 @@ export const createMigration: CreateMigration = async function createMigration(
}

const drizzleJsonAfter = generateDrizzleJson(this.schema)

if (drizzleJsonBefore.version < drizzleJsonAfter.version) {
drizzleJsonBefore = upPgSnapshot(drizzleJsonBefore)
}

const sqlStatementsUp = await generateMigration(drizzleJsonBefore, drizzleJsonAfter)
const sqlStatementsDown = await generateMigration(drizzleJsonAfter, drizzleJsonBefore)

Expand Down
3 changes: 3 additions & 0 deletions test/database/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
migrations
v5_migrations/*
!v5_migrations/20241018_162142_test_v5.ts
!v5_migrations/20241018_162142_test_v5.json
28 changes: 27 additions & 1 deletion test/database/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('database', () => {
})

afterAll(() => {
removeFiles(path.normalize(payload.db.migrationDir))
removeFiles(path.normalize(payload.db.migrationDir), (name) => !name.includes('test_v5'))
})

it('should run migrate:create', async () => {
Expand All @@ -74,6 +74,32 @@ describe('database', () => {
expect(migrationFile).toContain('_test')
})

it('should run migrate:create with older drizzle version schema', async () => {
const db = payload.db as unknown as PostgresAdapter

// eslint-disable-next-line jest/no-if
if (db.name !== 'postgres') return

// eslint-disable-next-line jest/no-if
if (db.schemaName && db.schemaName !== 'public') {
return
}

const args = {
_: ['migrate:create', 'test'],
forceAcceptWarning: true,
}
const ogMigrationDir = payload.db.migrationDir
payload.db.migrationDir = path.resolve(__dirname, 'v5_migrations')
await migrate(args)

// read files names in migrationsDir
const migrationFile = path.normalize(fs.readdirSync(payload.db.migrationDir)[2])
expect(migrationFile).toContain('_test')
removeFiles(path.normalize(payload.db.migrationDir), (name) => !name.includes('test_v5'))
payload.db.migrationDir = ogMigrationDir
})

it('should run migrate', async () => {
const args = {
_: ['migrate'],
Expand Down
Loading

0 comments on commit e9c1222

Please sign in to comment.