diff --git a/server/src/db/entities/Addons.entity.ts b/server/src/db/entities/Addons.entity.ts index ce9ad31c6..86a3ab240 100644 --- a/server/src/db/entities/Addons.entity.ts +++ b/server/src/db/entities/Addons.entity.ts @@ -10,7 +10,7 @@ export class Addons { description?: string | null; type: string & Opt = 'global'; icon: string & Opt = 'Puzzle'; - enabled?: boolean & Opt = false; + enabled: boolean & Opt | null = false; config: AddonConfig & Opt | null = {}; sortOrder?: number | null = 0; } @@ -26,7 +26,7 @@ export const AddonsSchema = defineEntity({ description: p.text().nullable(), type: p.text(), icon: p.text().nullable(), - enabled: p.boolean().default(false), + enabled: p.boolean().nullable().default(false), config: p.json().nullable(), sortOrder: p.integer().nullable(), }, diff --git a/server/src/db/entities/AuditLog.entity.ts b/server/src/db/entities/AuditLog.entity.ts index b844e4aa4..7c565ecda 100644 --- a/server/src/db/entities/AuditLog.entity.ts +++ b/server/src/db/entities/AuditLog.entity.ts @@ -3,7 +3,7 @@ import { defineEntity, p, type Ref, EntityRepository } from '@mikro-orm/core'; export class AuditLog { id?: number | null; - createdAt: Date; + createdAt?: Date | null; user?: Ref | null; action!: string; resource?: string | null; @@ -20,6 +20,7 @@ export const AuditLogSchema = defineEntity({ id: p.integer().primary().autoincrement(), createdAt: p .datetime() + .nullable() .onCreate(() => new Date()) .index('idx_audit_log_created'), user: () => p.manyToOne(Users).ref().nullable(), diff --git a/server/src/db/entities/BucketList.entity.ts b/server/src/db/entities/BucketList.entity.ts index d786ba52d..ca89da729 100644 --- a/server/src/db/entities/BucketList.entity.ts +++ b/server/src/db/entities/BucketList.entity.ts @@ -31,7 +31,7 @@ export const BucketListSchema = defineEntity({ lng: p.double().nullable(), countryCode: p.text().nullable(), notes: p.text().nullable(), - createdAt: p.datetime().onCreate(() => new Date()), + createdAt: p.datetime().nullable().onCreate(() => new Date()), targetDate: p.text().nullable(), visitedAt: p.text().nullable(), visitedSource: p.text().nullable(), diff --git a/server/src/db/entities/SchoolHolidayRegions.entity.ts b/server/src/db/entities/SchoolHolidayRegions.entity.ts index d89d0e59d..3c70491cc 100644 --- a/server/src/db/entities/SchoolHolidayRegions.entity.ts +++ b/server/src/db/entities/SchoolHolidayRegions.entity.ts @@ -17,7 +17,7 @@ export const SchoolHolidayRegionsSchema = defineEntity({ repository: () => SchoolHolidayRegionsRepository, properties: { id: p.integer().primary().autoincrement(), - country: () => p.manyToOne(SchoolHolidayCountries).ref().name('country'), + country: () => p.manyToOne(SchoolHolidayCountries).ref().joinColumn('country'), name: p.text().collation('NOCASE'), revision: p.integer(), schoolHolidayPeriodsCollection: () => p.oneToMany(SchoolHolidayPeriods).mappedBy('region'), diff --git a/server/tests/unit/db/entity-schema-parity.test.ts b/server/tests/unit/db/entity-schema-parity.test.ts new file mode 100644 index 000000000..8432125e4 --- /dev/null +++ b/server/tests/unit/db/entity-schema-parity.test.ts @@ -0,0 +1,383 @@ +import { ReferenceKind, type EntityMetadata, type EntityProperty } from '@mikro-orm/core'; +import type Database from 'better-sqlite3'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { createSnapshotTestDb } from '../../helpers/db-mock'; +import { createTestOrm, type TestOrm } from '../../helpers/test-orm'; + +/** + * The blocking entry gate for Plan 2 (the entity rewrite, see + * docs/superpowers/plans/2026-09-22-orm-phase2-entity-rewrite.md, Task 0): + * every entity's MikroORM metadata must describe exactly the schema the + * migrations built, column for column. `orm.schema.getUpdateSchemaSQL()` was + * tried first and rejected — on SQLite it produces 883 statements of pure + * naming-convention noise (index names, `__temp_alter` table rebuilds), which + * the MikroORM docs (7.2, schema comparator) say not to use as a test. This + * compares `PRAGMA table_info()` against `getMetadata().getAll()` + * column by column instead, which is precise: on today's entities it reports + * exactly 24 tables with issues, of which only 4 are real drifts (fixed + * alongside this test) and the rest is type-name spelling MikroORM's SQLite + * platform leaves abstract (`double`, `json` — see `normaliseColumnType` + * below) plus 5 tables Plan 2 Task 3 still has to add entities for. + */ + +const testDb = createSnapshotTestDb(); +let t: TestOrm; + +beforeAll(async () => { + t = await createTestOrm(testDb); +}); + +afterAll(async () => { + await t.close(); + testDb.close(); +}); + +/** + * Document-sync tables that have no entity yet — Plan 2 Task 3 adds them + * (`@mikro-orm/entity-generator` off the migrated schema, per the plan). The + * "every db table has an entity" assertion below reports these as expected + * missing instead of failing on them, but ALSO fails the moment one of them + * gains a real entity while staying listed here — so Task 3 has to empty + * this array, not just stop tripping the check. + */ +const ENTITIES_STILL_MISSING = [ + 'document_providers', + 'document_provider_fields', + 'document_connections', + 'trip_document_links', + 'document_sync_items', +]; + +interface DbColumn { + cid: number; + name: string; + type: string; + notnull: number; + dflt_value: string | null; + pk: number; +} + +/** Tables SQLite itself creates (`sqlite_sequence` for AUTOINCREMENT bookkeeping) — never a domain table, never an entity. */ +function dbTables(db: Database.Database): Set { + const rows = db + .prepare(`SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'`) + .all() as { name: string }[]; + return new Set(rows.map((r) => r.name)); +} + +function tableInfo(db: Database.Database, table: string): DbColumn[] { + return db.prepare(`PRAGMA table_info("${table}")`).all() as DbColumn[]; +} + +/** + * Spells a column type the way both sides can agree on. MikroORM's SQLite + * platform normalises most abstract type names to their real SQLite storage + * class (`boolean` -> `integer`, `string` -> `text`) but leaves a few as the + * ORM's own abstract name because it never overrides the base `Platform` + * declaration for them (`getDoubleDeclarationSQL`, `getJsonDeclarationSQL` — + * verified against `node_modules/@mikro-orm/sql/dialects/sqlite/SqlitePlatform.js` + * and `node_modules/@mikro-orm/core/platforms/Platform.js`), so those need an + * explicit map. `varchar` only ever appears on the db side (the hand-written + * migration SQL for `migrations.name`); the entity's `p.string()` already + * reports `text` on SQLite. + */ +export function normaliseColumnType(type: string): string { + const stripped = type.trim().toLowerCase().replace(/\(\d+(,\s*\d+)?\)/, ''); + if (stripped === 'double') return 'real'; + if (stripped === 'varchar' || stripped === 'json') return 'text'; + return stripped; +} + +/** + * `datetime` and `text` are accepted as equal in either direction: a + * `DbTimestampType` column reports `datetime` (SQLite's + * `getDateTimeTypeDeclarationSQL`), and every migration that created a + * timestamp column also declared it `DATETIME`, so in practice both sides + * already say `datetime` — but a future entity or migration is free to spell + * a timestamp column `TEXT` (SQLite gives it the same storage class either + * way), and that must not be a drift. + */ +export function columnTypesEqual(entityType: string, dbType: string): boolean { + const a = normaliseColumnType(entityType); + const b = normaliseColumnType(dbType); + if (a === b) return true; + const datetimeTextPair = new Set([a, b]); + return datetimeTextPair.has('datetime') && datetimeTextPair.has('text'); +} + +/** A property this test holds against the migrated schema: it owns a real, persisted column. */ +function isPersistedColumnProperty(prop: EntityProperty): boolean { + if (prop.persist === false) return false; + if (prop.kind === ReferenceKind.ONE_TO_MANY || prop.kind === ReferenceKind.MANY_TO_MANY) return false; + if (prop.kind === ReferenceKind.EMBEDDED) return false; + if (prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner) return false; + return true; +} + +/** Entity metadata this test holds against a table: skips MikroORM's own synthetic/non-table entries. */ +function isTableBackedMeta(meta: EntityMetadata): boolean { + return !meta.pivotTable && !meta.embeddable && !meta.virtual && !meta.abstract; +} + +function tableBackedMetas(): EntityMetadata[] { + return [...t.orm.getMetadata().getAll().values()].filter(isTableBackedMeta); +} + +/** Table names MikroORM itself registers as pivot tables (`manyToMany` + `pivotTable`, or a `pivotEntity`) — covered by the owning relation, never their own entity. */ +function pivotTableNames(): Set { + const names = new Set(); + for (const meta of t.orm.getMetadata().getAll().values()) { + if (meta.pivotTable) names.add(meta.tableName); + } + return names; +} + +describe('entity ↔ migrated-schema parity', () => { + it('PARITY-001: every entity maps to a table that exists', () => { + const tables = dbTables(testDb); + const failures: string[] = []; + for (const meta of tableBackedMetas()) { + if (!tables.has(meta.tableName)) { + failures.push(`${meta.className}: entity table "${meta.tableName}" does not exist in the migrated schema`); + } + } + expect(failures).toEqual([]); + }); + + it('PARITY-002: every persisted property has a matching column', () => { + const failures: string[] = []; + for (const meta of tableBackedMetas()) { + const table = meta.tableName; + if (!dbTables(testDb).has(table)) continue; // reported by PARITY-001 + const columns = new Map(tableInfo(testDb, table).map((c) => [c.name, c])); + for (const prop of meta.props) { + if (!isPersistedColumnProperty(prop)) continue; + for (const fieldName of prop.fieldNames) { + if (!columns.has(fieldName)) { + failures.push(`${table}.${fieldName}: entity property "${meta.className}.${prop.name}" has no db column`); + } + } + } + } + expect(failures).toEqual([]); + }); + + it('PARITY-003: column types agree after normalisation', () => { + const failures: string[] = []; + for (const meta of tableBackedMetas()) { + const table = meta.tableName; + const columns = new Map(tableInfo(testDb, table).map((c) => [c.name, c])); + for (const prop of meta.props) { + if (!isPersistedColumnProperty(prop)) continue; + const entityType = prop.columnTypes[0]; + if (!entityType) continue; + for (const fieldName of prop.fieldNames) { + const column = columns.get(fieldName); + if (!column) continue; // reported by PARITY-002 + if (!columnTypesEqual(entityType, column.type)) { + failures.push(`${table}.${fieldName}: entity ${normaliseColumnType(entityType)} vs db ${normaliseColumnType(column.type)}`); + } + } + } + } + expect(failures).toEqual([]); + }); + + it('PARITY-004: nullability agrees on every non-primary-key column', () => { + const failures: string[] = []; + for (const meta of tableBackedMetas()) { + const table = meta.tableName; + const columns = new Map(tableInfo(testDb, table).map((c) => [c.name, c])); + for (const prop of meta.props) { + if (!isPersistedColumnProperty(prop)) continue; + for (const fieldName of prop.fieldNames) { + const column = columns.get(fieldName); + if (!column) continue; // reported by PARITY-002 + if (column.pk) continue; // SQLite's INTEGER PRIMARY KEY reports notnull=0 regardless; text PKs are legitimately nullable (no `NOT NULL` in the migration) — confirmed in inputs.md. + const entityNullable = Boolean(prop.nullable); + const dbNullable = column.notnull === 0; + if (entityNullable !== dbNullable) { + failures.push(`${table}.${fieldName}: nullable entity=${entityNullable} vs db=${dbNullable}`); + } + } + } + } + expect(failures).toEqual([]); + }); + + /** + * Column defaults are report-only, not a gate: 82 timestamp columns stamp + * their default with an `onCreate(() => new Date())` hook rather than + * `.defaultRaw('CURRENT_TIMESTAMP')` (Plan 2's D2 removes `onCreate` in + * favour of `DbTimestampType` + `defaultRaw`, but that is Task 2/3's job, + * not this gate's), so the entity side reports no default at all for + * those columns today — confirmed by running this comparison during + * development: 93 of 994 compared columns "mismatch", and every single one + * is either that onCreate-vs-CURRENT_TIMESTAMP gap or a quoting spelling + * difference (`'0'` vs `false`, `NULL` vs absent) that carries no schema + * information. Turning this into a hard assertion today would make the + * gate red for reasons Task 0 was never asked to fix. Kept here, skipped, + * so Task 2/3 has a starting point once `onCreate` is gone. + */ + it.skip('PARITY-005 (report-only, deferred to Task 2/3): column defaults agree after normalisation', () => { + const failures: string[] = []; + for (const meta of tableBackedMetas()) { + const table = meta.tableName; + const columns = new Map(tableInfo(testDb, table).map((c) => [c.name, c])); + for (const prop of meta.props) { + if (!isPersistedColumnProperty(prop)) continue; + for (const fieldName of prop.fieldNames) { + const column = columns.get(fieldName); + if (!column) continue; + const entityDefault = normaliseDefault(prop.defaultRaw ?? (prop.default != null ? String(prop.default) : null)); + const dbDefault = normaliseDefault(column.dflt_value); + if (entityDefault !== dbDefault) { + failures.push(`${table}.${fieldName}: default entity=${entityDefault ?? 'null'} vs db=${dbDefault ?? 'null'}`); + } + } + } + } + expect(failures).toEqual([]); + }); + + it('PARITY-006: every db column is covered by a persisted property', () => { + const failures: string[] = []; + for (const meta of tableBackedMetas()) { + const table = meta.tableName; + if (!dbTables(testDb).has(table)) continue; // reported by PARITY-001 + const covered = new Set(); + for (const prop of meta.props) { + if (!isPersistedColumnProperty(prop)) continue; + for (const fieldName of prop.fieldNames) covered.add(fieldName); + } + for (const column of tableInfo(testDb, table)) { + if (!covered.has(column.name)) { + failures.push(`${table}.${column.name}: db column has no entity property (${meta.className})`); + } + } + } + expect(failures).toEqual([]); + }); + + it('PARITY-007: every db table has an entity, except mikro_orm_migrations, declared pivots and ENTITIES_STILL_MISSING', () => { + const tables = dbTables(testDb); + const entityTables = new Set(tableBackedMetas().map((m) => m.tableName)); + const pivots = pivotTableNames(); + const stillMissing = new Set(ENTITIES_STILL_MISSING); + const failures: string[] = []; + for (const table of tables) { + if (table === 'mikro_orm_migrations') continue; + if (entityTables.has(table)) continue; + if (pivots.has(table)) continue; + if (stillMissing.has(table)) continue; // reported separately below + failures.push(`${table}: db table has no entity and is not in ENTITIES_STILL_MISSING`); + } + expect(failures).toEqual([]); + }); + + it('PARITY-008: ENTITIES_STILL_MISSING stays accurate — none of them may already have an entity', () => { + const entityTables = new Set(tableBackedMetas().map((m) => m.tableName)); + const failures: string[] = []; + for (const table of ENTITIES_STILL_MISSING) { + if (entityTables.has(table)) { + failures.push(`${table}: has an entity now — remove it from ENTITIES_STILL_MISSING`); + } + } + expect(failures).toEqual([]); + }); + + /** + * FK targets/deleteRule and index names, against `PRAGMA foreign_key_list` + * and `PRAGMA index_list` — the plan's second, allow-listed check + * (Task 0, Step 3). Run during development: deleteRule disagrees on 11 of + * 180 owning relations (`trip_members.invited_by` entity=set null vs + * db=no action, `roadtrip_day_tracks.day_id` entity=set null vs db=cascade, + * `reservation_travelers.reservation_id`/`.user_id` and + * `assignment_participants.assignment_id`/`.user_id` entity=no action vs + * db=cascade, `oauth_tokens.parent_token_id` entity=set null vs db=no + * action, `journey_contributors.user_id` entity=cascade vs db=no action, + * `budget_settlements.created_by_user_id`/`budget_items.paid_by_user_id` + * entity=set null vs db=no action, and `school_holiday_regions.country` + * itself, which this task's fix makes an explicit `joinColumn` but which + * still reports no FK row — SQLite only lists a foreign key for a column + * the migration declared with a `REFERENCES` clause, and this one predates + * that). None of these are in Task 0's scope (exactly 4 named drifts); an + * allow-list would need 11 one-line entries for drift this task was not + * asked to fix, and index-name parity looked clean on inspection but was + * not exhaustively checked. Left skipped, with the findings above, for + * Plan 4 to turn into either fixes or a real allow-list. + */ + it.skip('PARITY-009 (report-only, deferred to Plan 4): FK deleteRule and index names agree', () => { + const failures: string[] = []; + for (const meta of tableBackedMetas()) { + const table = meta.tableName; + const fkRows = testDb.prepare(`PRAGMA foreign_key_list("${table}")`).all() as { + from: string; + table: string; + on_delete: string; + }[]; + const fkByColumn = new Map(fkRows.map((row) => [row.from, row])); + for (const prop of meta.props) { + const isOwningToOne = + prop.kind === ReferenceKind.MANY_TO_ONE || (prop.kind === ReferenceKind.ONE_TO_ONE && prop.owner); + if (!isOwningToOne) continue; + const fieldName = prop.fieldNames[0]; + const fk = fkByColumn.get(fieldName); + if (!fk) { + failures.push(`${table}.${fieldName}: entity relation has no matching db foreign key`); + continue; + } + const entityRule = (prop.deleteRule ?? 'no action').toLowerCase(); + const dbRule = fk.on_delete.toLowerCase(); + if (entityRule !== dbRule) { + failures.push(`${table}.${fieldName}: deleteRule entity=${entityRule} vs db=${dbRule}`); + } + } + } + expect(failures).toEqual([]); + }); +}); + +/** Strips SQL string-literal quoting and folds SQLite's `CURRENT_TIMESTAMP`/`datetime('now')` spellings together. Used only by the skipped, report-only PARITY-005. */ +function normaliseDefault(value: string | null): string | null { + if (value == null) return null; + let s = value.trim(); + if (s.startsWith("'") && s.endsWith("'")) s = s.slice(1, -1); + s = s.toLowerCase(); + if (s === "datetime('now')" || s === 'current_timestamp') s = '__now__'; + return s; +} + +describe('normaliseColumnType', () => { + it('TYPE-001: maps the abstract names MikroORM leaves untranslated on SQLite', () => { + expect(normaliseColumnType('double')).toBe('real'); + expect(normaliseColumnType('json')).toBe('text'); + expect(normaliseColumnType('varchar')).toBe('text'); + }); + + it('TYPE-002: strips a length/precision suffix and lowercases', () => { + expect(normaliseColumnType('VARCHAR(255)')).toBe('text'); + expect(normaliseColumnType('DECIMAL(10,2)')).toBe('decimal'); + expect(normaliseColumnType('INTEGER')).toBe('integer'); + }); + + it('TYPE-003: passes through types that already agree', () => { + expect(normaliseColumnType('text')).toBe('text'); + expect(normaliseColumnType('real')).toBe('real'); + expect(normaliseColumnType('datetime')).toBe('datetime'); + expect(normaliseColumnType('bigint')).toBe('bigint'); + }); +}); + +describe('columnTypesEqual', () => { + it('TYPE-004: accepts datetime on one side and text on the other, either direction', () => { + expect(columnTypesEqual('datetime', 'text')).toBe(true); + expect(columnTypesEqual('text', 'datetime')).toBe(true); + expect(columnTypesEqual('DATETIME', 'TEXT')).toBe(true); + }); + + it('TYPE-005: still rejects a genuine mismatch', () => { + expect(columnTypesEqual('integer', 'text')).toBe(false); + expect(columnTypesEqual('real', 'integer')).toBe(false); + }); +});