|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { resolveMutationPrimaryKey } from '@/features/sql-console/mutation-primary-key' |
| 3 | +import type { TableInfo } from '@/features/sql-console/types' |
| 4 | + |
| 5 | +function makeColumn(name: string, primaryKey: boolean) { |
| 6 | + return { name, type: 'integer', nullable: false, primaryKey } |
| 7 | +} |
| 8 | + |
| 9 | +function makeTable( |
| 10 | + name: string, |
| 11 | + columns: { name: string; primaryKey?: boolean }[], |
| 12 | + schema?: string |
| 13 | +): TableInfo { |
| 14 | + return { |
| 15 | + name, |
| 16 | + schema, |
| 17 | + type: 'table', |
| 18 | + rowCount: 0, |
| 19 | + columns: columns.map(function (column) { |
| 20 | + return { name: column.name, type: 'integer', primaryKey: column.primaryKey ?? false } |
| 21 | + }) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +describe('resolveMutationPrimaryKey', function () { |
| 26 | + it('uses a single declared primary key from the result column definitions', function () { |
| 27 | + const resolution = resolveMutationPrimaryKey({ |
| 28 | + sourceTable: 'users', |
| 29 | + resultColumns: ['user_id', 'name'], |
| 30 | + columnDefinitions: [makeColumn('user_id', true), makeColumn('name', false)], |
| 31 | + schemaTables: [] |
| 32 | + }) |
| 33 | + expect(resolution).toEqual({ kind: 'ok', name: 'user_id' }) |
| 34 | + }) |
| 35 | + |
| 36 | + it('disables mutations when there is no source table', function () { |
| 37 | + const resolution = resolveMutationPrimaryKey({ |
| 38 | + sourceTable: undefined, |
| 39 | + resultColumns: ['id'], |
| 40 | + columnDefinitions: [], |
| 41 | + schemaTables: [] |
| 42 | + }) |
| 43 | + expect(resolution.kind).toBe('disabled') |
| 44 | + }) |
| 45 | + |
| 46 | + it('does NOT fall back to a column merely named "id"', function () { |
| 47 | + const resolution = resolveMutationPrimaryKey({ |
| 48 | + sourceTable: 'staging_events', |
| 49 | + resultColumns: ['id', 'payload'], |
| 50 | + columnDefinitions: [makeColumn('id', false), makeColumn('payload', false)], |
| 51 | + schemaTables: [ |
| 52 | + makeTable('staging_events', [{ name: 'id' }, { name: 'payload' }]) |
| 53 | + ] |
| 54 | + }) |
| 55 | + expect(resolution.kind).toBe('disabled') |
| 56 | + }) |
| 57 | + |
| 58 | + it('resolves the primary key from the schema when the result carries no PK metadata', function () { |
| 59 | + const resolution = resolveMutationPrimaryKey({ |
| 60 | + sourceTable: 'users', |
| 61 | + resultColumns: ['id', 'name'], |
| 62 | + columnDefinitions: [makeColumn('id', false), makeColumn('name', false)], |
| 63 | + schemaTables: [makeTable('users', [{ name: 'id', primaryKey: true }, { name: 'name' }])] |
| 64 | + }) |
| 65 | + expect(resolution).toEqual({ kind: 'ok', name: 'id' }) |
| 66 | + }) |
| 67 | + |
| 68 | + it('resolves a non-id primary key from the schema', function () { |
| 69 | + const resolution = resolveMutationPrimaryKey({ |
| 70 | + sourceTable: 'users', |
| 71 | + resultColumns: ['user_uuid', 'name'], |
| 72 | + columnDefinitions: [], |
| 73 | + schemaTables: [ |
| 74 | + makeTable('users', [{ name: 'user_uuid', primaryKey: true }, { name: 'name' }]) |
| 75 | + ] |
| 76 | + }) |
| 77 | + expect(resolution).toEqual({ kind: 'ok', name: 'user_uuid' }) |
| 78 | + }) |
| 79 | + |
| 80 | + it('matches schema-qualified source tables against the schema list', function () { |
| 81 | + const resolution = resolveMutationPrimaryKey({ |
| 82 | + sourceTable: 'public.users', |
| 83 | + resultColumns: ['id'], |
| 84 | + columnDefinitions: [], |
| 85 | + schemaTables: [makeTable('users', [{ name: 'id', primaryKey: true }], 'public')] |
| 86 | + }) |
| 87 | + expect(resolution).toEqual({ kind: 'ok', name: 'id' }) |
| 88 | + }) |
| 89 | + |
| 90 | + it('does not match a table from a different schema', function () { |
| 91 | + const resolution = resolveMutationPrimaryKey({ |
| 92 | + sourceTable: 'audit.users', |
| 93 | + resultColumns: ['id'], |
| 94 | + columnDefinitions: [], |
| 95 | + schemaTables: [makeTable('users', [{ name: 'id', primaryKey: true }], 'public')] |
| 96 | + }) |
| 97 | + expect(resolution.kind).toBe('disabled') |
| 98 | + }) |
| 99 | + |
| 100 | + it('disables mutations for composite primary keys declared in the result', function () { |
| 101 | + const resolution = resolveMutationPrimaryKey({ |
| 102 | + sourceTable: 'memberships', |
| 103 | + resultColumns: ['user_id', 'team_id'], |
| 104 | + columnDefinitions: [makeColumn('user_id', true), makeColumn('team_id', true)], |
| 105 | + schemaTables: [] |
| 106 | + }) |
| 107 | + expect(resolution).toEqual({ |
| 108 | + kind: 'disabled', |
| 109 | + reason: 'Composite primary keys are not yet supported for SQL result mutations.' |
| 110 | + }) |
| 111 | + }) |
| 112 | + |
| 113 | + it('disables mutations for composite primary keys declared in the schema', function () { |
| 114 | + const resolution = resolveMutationPrimaryKey({ |
| 115 | + sourceTable: 'memberships', |
| 116 | + resultColumns: ['user_id', 'team_id'], |
| 117 | + columnDefinitions: [], |
| 118 | + schemaTables: [ |
| 119 | + makeTable('memberships', [ |
| 120 | + { name: 'user_id', primaryKey: true }, |
| 121 | + { name: 'team_id', primaryKey: true } |
| 122 | + ]) |
| 123 | + ] |
| 124 | + }) |
| 125 | + expect(resolution).toEqual({ |
| 126 | + kind: 'disabled', |
| 127 | + reason: 'Composite primary keys are not yet supported for SQL result mutations.' |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + it('disables mutations when the primary key column is not in the result set', function () { |
| 132 | + const resolution = resolveMutationPrimaryKey({ |
| 133 | + sourceTable: 'users', |
| 134 | + resultColumns: ['name', 'email'], |
| 135 | + columnDefinitions: [], |
| 136 | + schemaTables: [makeTable('users', [{ name: 'id', primaryKey: true }, { name: 'name' }])] |
| 137 | + }) |
| 138 | + expect(resolution.kind).toBe('disabled') |
| 139 | + if (resolution.kind === 'disabled') { |
| 140 | + expect(resolution.reason).toContain('"id"') |
| 141 | + } |
| 142 | + }) |
| 143 | + |
| 144 | + it('matches primary key names case-insensitively and returns the result spelling', function () { |
| 145 | + const resolution = resolveMutationPrimaryKey({ |
| 146 | + sourceTable: 'Users', |
| 147 | + resultColumns: ['ID', 'Name'], |
| 148 | + columnDefinitions: [], |
| 149 | + schemaTables: [makeTable('users', [{ name: 'id', primaryKey: true }, { name: 'name' }])] |
| 150 | + }) |
| 151 | + expect(resolution).toEqual({ kind: 'ok', name: 'ID' }) |
| 152 | + }) |
| 153 | + |
| 154 | + it('disables mutations when the table is unknown to the schema', function () { |
| 155 | + const resolution = resolveMutationPrimaryKey({ |
| 156 | + sourceTable: 'mystery_table', |
| 157 | + resultColumns: ['id'], |
| 158 | + columnDefinitions: [], |
| 159 | + schemaTables: [makeTable('users', [{ name: 'id', primaryKey: true }])] |
| 160 | + }) |
| 161 | + expect(resolution.kind).toBe('disabled') |
| 162 | + }) |
| 163 | +}) |
0 commit comments