This commit is contained in:
2026-09-20 18:51:10 +08:00
parent 20a748cf7f
commit 417cbc6363
14 changed files with 551 additions and 12 deletions
+34
View File
@@ -0,0 +1,34 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { compareSnapshots } from './database-schema-diff.mjs'
test('database schema diff ignores column order but catches contract changes', () => {
const left = {
schemas: [{ schema: 'sample' }],
tables: [{ schema: 'sample', table: 'orders', type: 'BASE TABLE', engine: 'InnoDB', collation: 'utf8mb4_0900_ai_ci' }],
columns: [
{ schema: 'sample', table: 'orders', column: 'id', ordinal: 1, type: 'bigint', nullable: 'NO', defaultValue: null, extra: '', charset: null, collation: null, generation: '' },
],
}
const reordered = structuredClone(left)
reordered.columns[0].ordinal = 9
assert.deepEqual(compareSnapshots(left, reordered).columns, { leftOnly: [], rightOnly: [], changed: [] })
reordered.columns[0].nullable = 'YES'
const changed = compareSnapshots(left, reordered).columns.changed
assert.equal(changed.length, 1)
assert.equal(changed[0].key, 'sample.orders.id')
})
test('database schema diff reports objects present on only one side', () => {
const left = { schemas: [{ schema: 'sample' }], tables: [], columns: [] }
const right = {
schemas: [{ schema: 'sample' }],
tables: [{ schema: 'sample', table: 'orders', type: 'BASE TABLE', engine: 'InnoDB', collation: 'utf8mb4_0900_ai_ci' }],
columns: [{ schema: 'sample', table: 'orders', column: 'id', type: 'bigint', nullable: 'NO', defaultValue: null, extra: '', charset: null, collation: null, generation: '' }],
}
const diff = compareSnapshots(left, right)
assert.deepEqual(diff.tables.rightOnly, ['sample.orders'])
assert.deepEqual(diff.columns.rightOnly, ['sample.orders.id'])
})