|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Pin: SQLite introspection must report EVERY member of a composite primary |
| 5 | + * key, in DECLARED KEY ORDER. |
| 6 | + * |
| 7 | + * `PRAGMA table_info` does not report `pk` as a boolean. It reports the |
| 8 | + * column's **1-based position within the primary key** — `0` for "not part of |
| 9 | + * the key", `1` for the first key column, `2` for the second, and so on. |
| 10 | + * `introspectPrimaryKeys` previously filtered on `pk === 1`, which kept only |
| 11 | + * the first member of a composite key and silently dropped the rest. |
| 12 | + * |
| 13 | + * Both output signals were wrong together and for the same reason: |
| 14 | + * `introspectSchema` derives `col.isPrimary` FROM `primaryKeys` |
| 15 | + * (`if (primaryKeys.includes(col.name)) col.isPrimary = true`), so a consumer |
| 16 | + * could not recover the missing member by cross-checking the two. Both are |
| 17 | + * asserted here. |
| 18 | + * |
| 19 | + * `SqliteWasmDriver` and `TursoDriver` extend `SqlDriver` and override neither |
| 20 | + * `introspectPrimaryKeys` nor `introspectSchema`, so they inherit this arm. |
| 21 | + * Only the better-sqlite3 binding is executed here. |
| 22 | + */ |
| 23 | + |
| 24 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 25 | +import { SqlDriver } from '../src/index.js'; |
| 26 | + |
| 27 | +/** |
| 28 | + * The consumer-visible consequence, modelled locally: comparing a DECLARED |
| 29 | + * composite key against what introspection recovered. This is what |
| 30 | + * schema-drift comparison and the federated `external_catalog` addressing key |
| 31 | + * do with `primaryKeys` — an under-reported member reads as drift. |
| 32 | + * |
| 33 | + * Order-sensitive on purpose: `pk` is an ordinal, so a key reported in column |
| 34 | + * order rather than declared-key order is a different key for addressing and |
| 35 | + * upsert-conflict-target purposes. |
| 36 | + */ |
| 37 | +function compareDeclaredKey(declared: string[], introspected: string[]): string[] { |
| 38 | + const findings: string[] = []; |
| 39 | + for (const col of declared) { |
| 40 | + if (!introspected.includes(col)) findings.push(`missing_key_member:${col}`); |
| 41 | + } |
| 42 | + for (const col of introspected) { |
| 43 | + if (!declared.includes(col)) findings.push(`unexpected_key_member:${col}`); |
| 44 | + } |
| 45 | + if (findings.length === 0 && declared.join(',') !== introspected.join(',')) { |
| 46 | + findings.push(`key_order_mismatch:${declared.join(',')}!=${introspected.join(',')}`); |
| 47 | + } |
| 48 | + return findings; |
| 49 | +} |
| 50 | + |
| 51 | +describe('SqlDriver composite primary-key introspection (SQLite)', () => { |
| 52 | + let driver: SqlDriver; |
| 53 | + let knexInstance: any; |
| 54 | + |
| 55 | + beforeEach(async () => { |
| 56 | + driver = new SqlDriver({ |
| 57 | + client: 'better-sqlite3', |
| 58 | + connection: { filename: ':memory:' }, |
| 59 | + useNullAsDefault: true, |
| 60 | + }); |
| 61 | + knexInstance = (driver as any).knex; |
| 62 | + }); |
| 63 | + |
| 64 | + afterEach(async () => { |
| 65 | + await knexInstance.destroy(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('pins the SQLite fact this repair rests on: `pk` is a 1-based ordinal, not a boolean', async () => { |
| 69 | + await knexInstance.schema.createTable('order_lines', (t: any) => { |
| 70 | + t.string('order_id').notNullable(); |
| 71 | + t.integer('line_no').notNullable(); |
| 72 | + t.string('sku'); |
| 73 | + t.primary(['order_id', 'line_no']); |
| 74 | + }); |
| 75 | + |
| 76 | + const rows: any[] = await knexInstance.raw('PRAGMA table_info(order_lines)'); |
| 77 | + const pkByName = Object.fromEntries(rows.map((r) => [r.name, r.pk])); |
| 78 | + |
| 79 | + // If SQLite ever reported `pk` as a boolean, the fix below would be wrong. |
| 80 | + expect(pkByName).toEqual({ order_id: 1, line_no: 2, sku: 0 }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('reports every member of a composite key, and derives isPrimary for all of them', async () => { |
| 84 | + await knexInstance.schema.createTable('order_lines', (t: any) => { |
| 85 | + t.string('order_id').notNullable(); |
| 86 | + t.integer('line_no').notNullable(); |
| 87 | + t.string('sku'); |
| 88 | + t.primary(['order_id', 'line_no']); |
| 89 | + }); |
| 90 | + |
| 91 | + const schema = await driver.introspectSchema(); |
| 92 | + const table = schema.tables['order_lines']; |
| 93 | + |
| 94 | + // Signal 1: the table-level list. `line_no` was dropped before the fix. |
| 95 | + expect(table.primaryKeys).toEqual(['order_id', 'line_no']); |
| 96 | + |
| 97 | + // Signal 2: the per-column flag, derived FROM signal 1 — repaired with it. |
| 98 | + const isPrimaryByName = Object.fromEntries(table.columns.map((c) => [c.name, c.isPrimary === true])); |
| 99 | + expect(isPrimaryByName).toEqual({ order_id: true, line_no: true, sku: false }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('orders primaryKeys by pk ordinal, not by column position', async () => { |
| 103 | + // Declared key order (shipment_id, carrier_code) deliberately differs from |
| 104 | + // column order (carrier_code, shipment_id, leg_seq): iterating table_info |
| 105 | + // rows in row order would yield the columns in the wrong key order. |
| 106 | + await knexInstance.schema.createTable('shipment_legs', (t: any) => { |
| 107 | + t.string('carrier_code').notNullable(); |
| 108 | + t.string('shipment_id').notNullable(); |
| 109 | + t.integer('leg_seq'); |
| 110 | + t.primary(['shipment_id', 'carrier_code']); |
| 111 | + }); |
| 112 | + |
| 113 | + const rows: any[] = await knexInstance.raw('PRAGMA table_info(shipment_legs)'); |
| 114 | + // Row order is column order; the ordinals run against it. |
| 115 | + expect(rows.map((r) => [r.name, r.pk])).toEqual([ |
| 116 | + ['carrier_code', 2], |
| 117 | + ['shipment_id', 1], |
| 118 | + ['leg_seq', 0], |
| 119 | + ]); |
| 120 | + |
| 121 | + const schema = await driver.introspectSchema(); |
| 122 | + expect(schema.tables['shipment_legs'].primaryKeys).toEqual(['shipment_id', 'carrier_code']); |
| 123 | + }); |
| 124 | + |
| 125 | + it('reads as NO drift when a declared composite key is compared against introspection', async () => { |
| 126 | + await knexInstance.schema.createTable('order_lines', (t: any) => { |
| 127 | + t.string('order_id').notNullable(); |
| 128 | + t.integer('line_no').notNullable(); |
| 129 | + t.string('sku'); |
| 130 | + t.primary(['order_id', 'line_no']); |
| 131 | + }); |
| 132 | + |
| 133 | + const schema = await driver.introspectSchema(); |
| 134 | + const introspected = schema.tables['order_lines'].primaryKeys; |
| 135 | + |
| 136 | + expect(compareDeclaredKey(['order_id', 'line_no'], introspected)).toEqual([]); |
| 137 | + |
| 138 | + // Negative control: the comparison above is a real detector, not a |
| 139 | + // vacuously-empty one. A genuinely different declared key still drifts. |
| 140 | + expect(compareDeclaredKey(['order_id', 'warehouse_id'], introspected)).toEqual([ |
| 141 | + 'missing_key_member:warehouse_id', |
| 142 | + 'unexpected_key_member:line_no', |
| 143 | + ]); |
| 144 | + expect(compareDeclaredKey(['line_no', 'order_id'], introspected)).toEqual([ |
| 145 | + 'key_order_mismatch:line_no,order_id!=order_id,line_no', |
| 146 | + ]); |
| 147 | + }); |
| 148 | + |
| 149 | + it('still reports a single-column key exactly, and an unkeyed table as empty', async () => { |
| 150 | + await knexInstance.schema.createTable('widgets', (t: any) => { |
| 151 | + t.string('id').primary(); |
| 152 | + t.string('name'); |
| 153 | + }); |
| 154 | + |
| 155 | + // No primary key at all: every `pk` is 0. Guards the repair against |
| 156 | + // becoming `pk >= 0`, which would report every column as a key member. |
| 157 | + await knexInstance.schema.createTable('audit_lines', (t: any) => { |
| 158 | + t.string('actor'); |
| 159 | + t.string('action'); |
| 160 | + }); |
| 161 | + |
| 162 | + const schema = await driver.introspectSchema(); |
| 163 | + |
| 164 | + expect(schema.tables['widgets'].primaryKeys).toEqual(['id']); |
| 165 | + expect(schema.tables['audit_lines'].primaryKeys).toEqual([]); |
| 166 | + |
| 167 | + const auditPrimary = schema.tables['audit_lines'].columns.map((c) => c.isPrimary === true); |
| 168 | + expect(auditPrimary).toEqual([false, false]); |
| 169 | + }); |
| 170 | +}); |
0 commit comments