Skip to content

Commit 4457aa3

Browse files
authored
fix: slight correct for locking in migration (#56)
1 parent 68b9e3f commit 4457aa3

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ node_modules/
22
dist/
33
*.log
44
.DS_Store
5+
# Database URLs for the test suites carry passwords; only the examples are tracked.
6+
.env
7+
.env.*
8+
!.env.*.example

‎packages/sync-mysql/src/schema/migrate.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,12 @@ export async function migrate(
9999
): Promise<MigrateResult> {
100100
return db.pinned(async (conn) => {
101101
const lock = lockName(schema)
102-
const got = await conn.query<{ l: number | null }>(`select get_lock(?, 60) as l`, [lock])
103-
if (got.rows[0]?.l !== 1) throw new Error(`could not take the migration lock for ${schema} within 60s`)
102+
// Read the value by position, not by alias: Vitess (PlanetScale) answers a
103+
// lock function itself and names the column after the expression, so `l`
104+
// would be undefined there even though the lock was granted.
105+
const got = await conn.query<Record<string, number | null>>(`select get_lock(?, 60) as l`, [lock])
106+
const taken = Object.values(got.rows[0] ?? {})[0]
107+
if (Number(taken) !== 1) throw new Error(`could not take the migration lock for ${schema} within 60s`)
104108
try {
105109
return await applySchema(conn, streams, schema, logger, options)
106110
} finally {

‎packages/sync-mysql/test/schema.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
22
import { createTestDb, describeDb, type TestDb } from './helpers/mysql.js'
33
import { migrate, fingerprint, DDL_VERSION, lockName } from '../src/schema/migrate.js'
4+
import type { QueryResult, SqlClient } from '../src/db/client.js'
45
import {
6+
bookkeepingSql,
57
buildSchemaSql,
68
safeIdentifier,
79
columnExpression,
@@ -107,6 +109,66 @@ describe('DDL generation', () => {
107109
})
108110
})
109111

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+
110172
describeDb('migrations against real MySQL', () => {
111173
let db: TestDb
112174

0 commit comments

Comments
 (0)