|
| 1 | +import { randomUUID } from 'node:crypto' |
| 2 | +import { Pool } from 'pg' |
| 3 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest' |
| 4 | +import { streamByName } from '@workos/sync-engine' |
| 5 | +import { fromPgPool, type SqlClient } from '../src/db/client.js' |
| 6 | +import { migrate } from '../src/schema/migrate.js' |
| 7 | +import { quoteIdent } from '../src/schema/ddl.js' |
| 8 | +import { detectColumnDrift } from '../src/schema/drift.js' |
| 9 | +import { GENERATED_BY } from '../src/schema/views.js' |
| 10 | + |
| 11 | +function pauseAfterSchema(client: SqlClient, created: () => void, resume: Promise<void>): SqlClient { |
| 12 | + return { |
| 13 | + ...client, |
| 14 | + async exec(sql) { |
| 15 | + await client.exec(sql) |
| 16 | + if (sql.startsWith('create schema')) { |
| 17 | + created() |
| 18 | + await resume |
| 19 | + } |
| 20 | + }, |
| 21 | + transaction: (fn) => client.transaction((tx) => fn(pauseAfterSchema(tx, created, resume))), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +describe.skipIf(!process.env.TEST_DATABASE_URL)('schema initialization on PostgreSQL', () => { |
| 26 | + const users = streamByName('users')! |
| 27 | + const changed = { ...users, columns: users.columns.map((column) => |
| 28 | + column.name === 'email' ? { ...column, path: 'primary_email' } : column, |
| 29 | + ) } |
| 30 | + let pools: Pool[] |
| 31 | + let db: SqlClient |
| 32 | + let other: SqlClient |
| 33 | + let observer: SqlClient |
| 34 | + let otherPid: number |
| 35 | + let schema: string |
| 36 | + |
| 37 | + beforeEach(async () => { |
| 38 | + schema = `migrate_test_${randomUUID().replaceAll('-', '')}` |
| 39 | + pools = Array.from({ length: 3 }, () => new Pool({ |
| 40 | + connectionString: process.env.TEST_DATABASE_URL, max: 1, statement_timeout: 10_000, |
| 41 | + })) |
| 42 | + db = fromPgPool(pools[0]!) |
| 43 | + other = fromPgPool(pools[1]!) |
| 44 | + observer = fromPgPool(pools[2]!) |
| 45 | + otherPid = (await other.query<{ pid: number }>('select pg_backend_pid() as pid')).rows[0]!.pid |
| 46 | + }) |
| 47 | + |
| 48 | + afterEach(async () => { |
| 49 | + try { |
| 50 | + await observer.exec(`drop schema if exists "${schema}" cascade`) |
| 51 | + } finally { |
| 52 | + await Promise.all(pools.map((pool) => pool.end())) |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + it('waits for an in-progress first migration before creating any catalog objects', async () => { |
| 57 | + let created!: () => void |
| 58 | + let resume!: () => void |
| 59 | + const ready = new Promise<void>((resolve) => { created = resolve }) |
| 60 | + const paused = new Promise<void>((resolve) => { resume = resolve }) |
| 61 | + const first = db.transaction((tx) => migrate(pauseAfterSchema(tx, created, paused), [users], schema)) |
| 62 | + let second: ReturnType<typeof migrate> | undefined |
| 63 | + try { |
| 64 | + await Promise.race([ready, first]) |
| 65 | + second = migrate(other, [users], schema) |
| 66 | + await expect.poll(async () => { |
| 67 | + const result = await observer.query<{ wait_event_type: string }>( |
| 68 | + 'select wait_event_type from pg_stat_activity where pid = $1', [otherPid], |
| 69 | + ) |
| 70 | + return result.rows[0]?.wait_event_type |
| 71 | + }, { timeout: 5_000, interval: 10 }).toBe('Lock') |
| 72 | + resume() |
| 73 | + const results = await Promise.all([first, second]) |
| 74 | + expect(results.map((result) => result.applied)).toEqual([true, false]) |
| 75 | + expect((await other.query(`select count(*)::int as n from "${schema}"._migrations`)).rows).toEqual([{ n: 1 }]) |
| 76 | + expect((await other.query(`select * from "${schema}".users_live`)).rows).toEqual([]) |
| 77 | + } finally { |
| 78 | + resume() |
| 79 | + await Promise.allSettled(second ? [first, second] : [first]) |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + it('allows concurrent warm startup with single-connection pools', async () => { |
| 84 | + await migrate(db, [users], schema) |
| 85 | + const results = await Promise.all([migrate(db, [users], schema), migrate(other, [users], schema)]) |
| 86 | + expect(results.map((result) => result.applied)).toEqual([false, false]) |
| 87 | + }) |
| 88 | + |
| 89 | + it('rolls back failed bootstrap and releases the lock for another initializer', async () => { |
| 90 | + await expect(migrate(db, [{ ...users, key: ['missing_column'] }], schema)).rejects.toThrow(/does not exist/) |
| 91 | + expect((await observer.query('select to_regnamespace($1) as name', [schema])).rows).toEqual([{ name: null }]) |
| 92 | + expect((await migrate(other, [users], schema)).applied).toBe(true) |
| 93 | + }) |
| 94 | + |
| 95 | + it('recovers from a refused view replacement without losing grants', async () => { |
| 96 | + await migrate(db, [users], schema) |
| 97 | + const columns = await db.query<{ column_name: string }>( |
| 98 | + `select column_name from information_schema.columns |
| 99 | + where table_schema = $1 and table_name = 'users' order by ordinal_position desc`, [schema], |
| 100 | + ) |
| 101 | + await db.exec(` |
| 102 | + drop view "${schema}".users_live; |
| 103 | + create view "${schema}".users_live as |
| 104 | + select ${columns.rows.map((column) => quoteIdent(column.column_name)).join(', ')} from "${schema}".users; |
| 105 | + comment on view "${schema}".users_live is '${GENERATED_BY}: older definition'; |
| 106 | + grant select on "${schema}".users_live to public; |
| 107 | + delete from "${schema}"._migrations; |
| 108 | + `) |
| 109 | + expect((await migrate(other, [users], schema, undefined, { repairViews: true })).applied).toBe(true) |
| 110 | + const grants = await observer.query( |
| 111 | + `select privilege_type from information_schema.table_privileges |
| 112 | + where table_schema = $1 and table_name = 'users_live' and grantee = 'PUBLIC'`, [schema], |
| 113 | + ) |
| 114 | + expect(grants.rows).toEqual([{ privilege_type: 'SELECT' }]) |
| 115 | + }) |
| 116 | + |
| 117 | + it('can check column drift again after repairing it in the migration transaction', async () => { |
| 118 | + await migrate(db, [users], schema) |
| 119 | + await db.query(`insert into "${schema}".users (_env, _raw, _updated_at) |
| 120 | + values ('prod', '{"id":"user_1","email":"old@example.test","primary_email":"new@example.test"}', now())`) |
| 121 | + const result = await other.transaction(async (tx) => { |
| 122 | + const repaired = await migrate(tx, [changed], schema, undefined, { repair: true }) |
| 123 | + expect(await detectColumnDrift(tx, [changed], schema)).toEqual([]) |
| 124 | + return repaired |
| 125 | + }) |
| 126 | + expect(result.repaired.map((column) => column.column)).toEqual(['email']) |
| 127 | + expect(result.drift).toEqual([]) |
| 128 | + expect((await observer.query(`select email from "${schema}".users_live`)).rows).toEqual([{ email: 'new@example.test' }]) |
| 129 | + expect((await migrate(db, [changed], schema)).applied).toBe(false) |
| 130 | + }) |
| 131 | + |
| 132 | + it('reports a refused column repair without aborting the migration transaction', async () => { |
| 133 | + await migrate(db, [users], schema) |
| 134 | + await db.exec(`create view "${schema}".custom_emails as select email from "${schema}".users`) |
| 135 | + const result = await migrate(other, [changed], schema, undefined, { repair: true }) |
| 136 | + expect(result.repaired).toEqual([]) |
| 137 | + expect(result.drift).toHaveLength(1) |
| 138 | + expect((await observer.query(`select * from "${schema}".custom_emails`)).rows).toEqual([]) |
| 139 | + expect((await observer.query(`select * from "${schema}".users_live`)).rows).toEqual([]) |
| 140 | + expect((await migrate(db, [users], schema)).applied).toBe(false) |
| 141 | + }) |
| 142 | +}) |
0 commit comments