|
1 | 1 | import { describe, it, expect, beforeEach, afterEach } from 'vitest' |
2 | 2 | import { createTestDb, describeDb, type TestDb } from './helpers/mysql.js' |
3 | 3 | import { migrate, fingerprint, DDL_VERSION, lockName } from '../src/schema/migrate.js' |
| 4 | +import type { QueryResult, SqlClient } from '../src/db/client.js' |
4 | 5 | import { |
| 6 | + bookkeepingSql, |
5 | 7 | buildSchemaSql, |
6 | 8 | safeIdentifier, |
7 | 9 | columnExpression, |
@@ -107,6 +109,66 @@ describe('DDL generation', () => { |
107 | 109 | }) |
108 | 110 | }) |
109 | 111 |
|
| 112 | +describe('migration lock', () => { |
| 113 | + /** |
| 114 | + * A client that answers only what `migrate` asks before any DDL: the lock |
| 115 | + * itself, then the "already applied" short-circuit of `applySchema`. So a |
| 116 | + * granted lock ends in `applied: false` with nothing executed, and a |
| 117 | + * refused one in a throw before the schema is touched. |
| 118 | + */ |
| 119 | + function fakeClient(lockRow: Record<string, unknown> | undefined) { |
| 120 | + const log: string[] = [] |
| 121 | + const client: SqlClient = { |
| 122 | + async query<T>(sql: string): Promise<QueryResult<T>> { |
| 123 | + log.push(sql) |
| 124 | + const rows = (r: unknown[]): QueryResult<T> => ({ rows: r as T[], affectedRows: 0 }) |
| 125 | + if (sql.startsWith('select get_lock(')) return rows(lockRow ? [lockRow] : []) |
| 126 | + if (sql.includes('information_schema.tables')) { |
| 127 | + return rows(bookkeepingSql('workos').map(([table_name]) => ({ table_name }))) |
| 128 | + } |
| 129 | + // The schema exists and this fingerprint is already recorded. |
| 130 | + return rows([{ 1: 1 }]) |
| 131 | + }, |
| 132 | + async exec(sql) { |
| 133 | + log.push(sql) |
| 134 | + }, |
| 135 | + transaction: () => Promise.reject(new Error('migrate must not open a transaction')), |
| 136 | + pinned: (fn) => fn(client), |
| 137 | + close: async () => {}, |
| 138 | + } |
| 139 | + return { client, log } |
| 140 | + } |
| 141 | + |
| 142 | + const granted: [label: string, row: Record<string, unknown>][] = [ |
| 143 | + ['MySQL honours the alias', { l: 1 }], |
| 144 | + ['Vitess names the column after the expression', { 'get_lock(?, 60)': 1 }], |
| 145 | + ['the driver hands the value back as a string', { 'get_lock(?, 60)': '1' }], |
| 146 | + ] |
| 147 | + |
| 148 | + it.each(granted)('proceeds when the lock is granted and %s', async (_label, row) => { |
| 149 | + const { client, log } = fakeClient(row) |
| 150 | + const result = await migrate(client, [users], 'workos') |
| 151 | + expect(result.applied).toBe(false) |
| 152 | + expect(log[0]).toMatch(/^select get_lock\(/) |
| 153 | + expect(log[log.length - 1]).toMatch(/^select release_lock\(/) |
| 154 | + }) |
| 155 | + |
| 156 | + const refused: [label: string, row: Record<string, unknown> | undefined][] = [ |
| 157 | + ['the lock timed out', { l: 0 }], |
| 158 | + ['the server reports an error', { l: null }], |
| 159 | + ['Vitess times out', { 'get_lock(?, 60)': 0 }], |
| 160 | + ['no row comes back', undefined], |
| 161 | + ] |
| 162 | + |
| 163 | + it.each(refused)('stops before any DDL when %s', async (_label, row) => { |
| 164 | + const { client, log } = fakeClient(row) |
| 165 | + await expect(migrate(client, [users], 'workos')).rejects.toThrow('could not take the migration lock for workos') |
| 166 | + // Nothing was held, so nothing is released, and the schema was never read. |
| 167 | + expect(log).toHaveLength(1) |
| 168 | + expect(log[0]).toMatch(/^select get_lock\(/) |
| 169 | + }) |
| 170 | +}) |
| 171 | + |
110 | 172 | describeDb('migrations against real MySQL', () => { |
111 | 173 | let db: TestDb |
112 | 174 |
|
|
0 commit comments