48 lines
2.4 KiB
JavaScript
48 lines
2.4 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
|
|
import { compareSnapshots, parseMetadataRows } from './database-schema-diff.mjs'
|
|
|
|
test('metadata transport preserves Unicode, escaped control characters and expression defaults', () => {
|
|
const rows = [{ schema: 'sample', table: 'orders', defaultValue: '国内\r\n海外\t"quote"\\path', generation: "concat('x', '\\n')" }, { defaultValue: null }]
|
|
const output = rows.map(row => Buffer.from(JSON.stringify(row), 'utf8').toString('hex')).join('\r\n') + '\r\n'
|
|
assert.deepEqual(parseMetadataRows(output), rows)
|
|
assert.deepEqual(parseMetadataRows(''), [])
|
|
})
|
|
|
|
test('metadata transport rejects invalid or truncated records', () => {
|
|
assert.throws(() => parseMetadataRows('abc'), /HEX JSON/)
|
|
assert.throws(() => parseMetadataRows('not-json'), /HEX JSON/)
|
|
assert.throws(() => parseMetadataRows(Buffer.from('{"schema":').toString('hex')), SyntaxError)
|
|
})
|
|
|
|
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'])
|
|
})
|