diff --git a/server/assets/postgres/baseline.sql b/server/assets/postgres/baseline.sql index 0a2d0e254..0c739e9d5 100644 --- a/server/assets/postgres/baseline.sql +++ b/server/assets/postgres/baseline.sql @@ -1,6 +1,6 @@ -- The schema a fresh Postgres database starts from. Generated by `npm run gen:pg-baseline`; do not edit. CREATE COLLATION IF NOT EXISTS nocase (provider = icu, locale = 'und-u-ks-level2', deterministic = false); -create table "addons" ("id" text null, "name" text not null, "description" text null, "type" text not null default 'global', "icon" text null default 'Puzzle', "enabled" boolean null default false, "config" jsonb null default '{}', "sort_order" bigint null default 0, primary key ("id")); +create table "addons" ("id" text null, "name" text not null, "description" text null, "type" text not null default 'global', "icon" text null default 'Puzzle', "enabled" bigint null default 0, "config" jsonb null default '{}', "sort_order" bigint null default 0, primary key ("id")); create table "app_settings" ("key" text null, "value" text null, primary key ("key")); create table "document_providers" ("id" text null, "name" text not null, "description" text null, "icon" text null default 'FileText', "enabled" bigint null default 0, "sort_order" bigint null default 0, primary key ("id")); create table "document_provider_fields" ("id" bigserial primary key, "provider_id" text not null, "field_key" text not null, "label" text not null, "input_type" text not null default 'text', "placeholder" text null, "hint" text null, "required" bigint null default 0, "secret" bigint null default 0, "sort_order" bigint null default 0); diff --git a/server/scripts/generate-entities.ts b/server/scripts/generate-entities.ts index 5341eb8b8..8267b6112 100644 --- a/server/scripts/generate-entities.ts +++ b/server/scripts/generate-entities.ts @@ -292,26 +292,27 @@ export function RULE1b_markJsonColumns(metadata: EntityMetadata[]): JsonColumnFi return fixups; } -/** Rule 1c (metadata level): mark known boolean columns so they render `p.boolean()` (see BOOLEAN_COLUMNS above). */ +/** Rule 1c (metadata level): mark known boolean columns so they render `p.type(IntegerFlagType)` (see BOOLEAN_COLUMNS above). */ export function RULE1c_markBooleanColumns(metadata: EntityMetadata[]): RetypedScalarFixup[] { const fixups: RetypedScalarFixup[] = []; for (const meta of metadata) { for (const prop of Object.values(meta.properties)) { if (prop.kind !== undefined && prop.kind !== ReferenceKind.SCALAR) continue; if (!BOOLEAN_COLUMNS.has(`${meta.tableName}.${prop.fieldNames[0]}`)) continue; - prop.type = 'boolean'; - // `runtimeType` must move with `type` here too, for the same reason - // Rule 1's doc comment gives: left at its introspected value - // (`'number'`, SQLite has no BOOLEAN storage class), the mismatch - // against `BooleanType`'s own runtime type ('boolean') would make - // `breakdownOfIType` treat the property as ambiguous and wrap the - // class field in `IType` — confirmed by generating - // the real schema before this fix, not assumed. + // `IntegerFlagType` (src/db/types), not `p.boolean()`: MikroORM's own + // boolean declares a `boolean` column on Postgres and writes true/false, + // where every other flag, and this one on SQLite, is the integer 0/1. + prop.type = 'IntegerFlagType'; + // `runtimeType` moves with `type`: the class field is a boolean. The + // renderer still wraps it in `IType` (the column's own + // type is a number) and adds a `.columnType('INTEGER')`; the fixups + // returned here drive the text passes that undo both + // (`fixFlagClassFieldTypes`, `stripRedundantColumnType`). prop.runtimeType = 'boolean'; // The introspected DB default is always the raw SQLite value (`0`/`1`, // a number, since SQLite has no boolean storage class either) — - // coerce it to a real boolean so a `p.boolean()` column's `.default()` - // renders `.default(false)`, not the now ill-typed `.default(0)`. + // coerce it to a real boolean so the flag's `.default()` renders + // `.default(false)`, not the now ill-typed `.default(0)`. if (typeof prop.default === 'number') prop.default = prop.default !== 0; if (typeof prop.defaultRaw === 'string' && /^-?\d+$/.test(prop.defaultRaw)) { prop.defaultRaw = prop.defaultRaw !== '0' ? 'true' : 'false'; @@ -1222,7 +1223,9 @@ export interface RuleFixups { defaults: DefaultFixup[]; timestamps: TimestampFixup[]; jsonColumns: JsonColumnFixup[]; - /** Rule 1's + Rule 1b's + Rule 2's retyped scalars together — all three need the same `.columnType()` strip (see `stripRedundantColumnType`). */ + /** Rule 1c's flags, whose class field `fixFlagClassFieldTypes` turns back into a plain boolean. */ + flags: RetypedScalarFixup[]; + /** Rule 1's + Rule 1b's + Rule 1c's + Rule 2's retyped scalars together — all need the same `.columnType()` strip (see `stripRedundantColumnType`). */ retypedScalars: RetypedScalarFixup[]; /** Rule 10's repository-type markers — one per entity class. */ repositoryMarkers: RepositoryTypeMarkerFixup[]; @@ -1249,14 +1252,10 @@ export function applyRules( ): RuleFixups { const retypedByRule1 = RULE1_fixUnknownScalarTypes(metadata); const jsonColumns = RULE1b_markJsonColumns(metadata); - // Unlike Rule 1's/Rule 1b's retypes, a boolean column's own declaration - // matches its raw INTEGER column type cleanly on SQLite (BooleanType's - // getColumnType() returns the same spelling needsExplicitColumnType() - // compares against) — confirmed by generating the real schema: no stray - // `.columnType()` appears, so RULE1c's fixups do NOT belong in - // `retypedScalars` (stripRedundantColumnType's "found no .columnType() to - // strip" throw catches this immediately if that ever stops being true). - RULE1c_markBooleanColumns(metadata); + // A flag picks up a `.columnType('INTEGER')` like Rule 1's retypes (the + // renderer cannot tell that `IntegerFlagType` already declares INTEGER), so + // its fixups join `retypedScalars`, and its class field needs its own pass. + const flags = RULE1c_markBooleanColumns(metadata); RULE1d_fixNaNNumericDefaults(metadata); const timestamps = RULE2_datetimeToDbTimestampType(metadata); RULE3_integerPkAutoincrement(metadata); @@ -1289,7 +1288,8 @@ export function applyRules( defaults, timestamps, jsonColumns, - retypedScalars: [...retypedByRule1, ...timestamps, ...jsonColumns], + flags, + retypedScalars: [...retypedByRule1, ...timestamps, ...jsonColumns, ...flags], repositoryMarkers, referencedColumns, checkExpressions, @@ -1400,6 +1400,7 @@ export async function collectForeignKeyDeleteRules( // --------------------------------------------------------------------------- export const onImport: ImportsResolver = (alias) => { if (alias === 'DbTimestampType') return { path: '../types', name: 'DbTimestampType' }; + if (alias === 'IntegerFlagType') return { path: '../types', name: 'IntegerFlagType' }; if (alias.endsWith('Repository')) { const entityName = alias.slice(0, -'Repository'.length); return { path: `../repositories/${entityName}.repository`, name: alias }; @@ -1666,6 +1667,33 @@ export function fixJsonClassFieldTypes(source: string, fixups: readonly JsonColu return result; } +/** + * A Rule 1c flag's class field renders as `IType`: the + * renderer sees a boolean runtime type over a number column and marks it + * ambiguous. The entity's field is a plain boolean (`IntegerFlagType` does the + * conversion), so the wrapper is taken off here, as `fixJsonClassFieldTypes` + * does for a JSON column. + */ +export function fixFlagClassFieldTypes(source: string, fixups: readonly RetypedScalarFixup[]): string { + let result = source; + for (const { className, propName } of fixups) { + const match = findClassFieldLine(result, propName); + if (!match) { + throw new Error(`generate-entities: fixFlagClassFieldTypes could not find the class field line for "${className}.${propName}".`); + } + const line = match[1]; + const newLine = line.replace(/IType/, 'boolean'); + if (newLine === line) { + if (/:\s*boolean\b/.test(line)) continue; // already the plain type + throw new Error( + `generate-entities: fixFlagClassFieldTypes found the class field for "${className}.${propName}" but no IType to simplify — check the renderer shape.`, + ); + } + result = result.slice(0, match.index) + newLine + result.slice(match.index + line.length); + } + return result; +} + /** * Inserts the `JSON_INTERFACES` declaration for every distinct type name this * file's JSON columns reference, right before `export class ` — the @@ -1901,6 +1929,7 @@ export function applyTextPasses(source: string, fixups: RuleFixups): string { result = stripRedundantColumnType(result, fixups.retypedScalars); result = injectJsonTypeParams(result, fixups.jsonColumns); result = fixJsonClassFieldTypes(result, fixups.jsonColumns); + result = fixFlagClassFieldTypes(result, fixups.flags); result = injectJsonInterfaces(result, fixups.jsonColumns); result = removeUnusedCoreImportIfUnused(result, 'IType'); result = stripHiddenTypeAnnotation(result); @@ -2001,6 +2030,7 @@ export async function generateEntities(): Promise { defaults: [], timestamps: [], jsonColumns: [], + flags: [], retypedScalars: [], repositoryMarkers: [], referencedColumns: [], @@ -2039,6 +2069,7 @@ export async function generateEntities(): Promise { defaults: fixups.defaults.filter((f) => f.className === className), timestamps: fixups.timestamps.filter((f) => f.className === className), jsonColumns: fixups.jsonColumns.filter((f) => f.className === className), + flags: fixups.flags.filter((f) => f.className === className), retypedScalars: fixups.retypedScalars.filter((f) => f.className === className), repositoryMarkers: fixups.repositoryMarkers.filter((f) => f.className === className), referencedColumns: fixups.referencedColumns.filter((f) => f.className === className), diff --git a/server/src/db/entities/Addons.entity.ts b/server/src/db/entities/Addons.entity.ts index 06d317782..4fc8d9e51 100644 --- a/server/src/db/entities/Addons.entity.ts +++ b/server/src/db/entities/Addons.entity.ts @@ -1,5 +1,6 @@ import { EntityRepositoryType, type Opt, defineEntity, p } from '@mikro-orm/core'; import { AddonsRepository } from '../repositories/Addons.repository'; +import { IntegerFlagType } from '../types'; export interface AddonConfig { [key: string]: unknown; @@ -12,7 +13,7 @@ export class Addons { description?: string | null; type: string & Opt = 'global'; icon?: string | null = 'Puzzle'; - enabled?: boolean | null = false; + enabled?: boolean | null; config?: AddonConfig | null; sort_order?: number | null = 0; } @@ -26,7 +27,7 @@ export const AddonsSchema = defineEntity({ description: p.text().nullable(), type: p.text().default('global'), icon: p.text().nullable(), - enabled: p.boolean().nullable(), + enabled: p.type(IntegerFlagType).nullable().default(false), config: p.json().nullable().default('{}'), sort_order: p.integer().nullable(), }, diff --git a/server/src/db/types/index.ts b/server/src/db/types/index.ts index 707d431cb..0d3431050 100644 --- a/server/src/db/types/index.ts +++ b/server/src/db/types/index.ts @@ -1,2 +1,3 @@ export { DB_TIMESTAMP_RE, DbTimestampType, dbNow, parseDbTimestamp, utcSuffix } from './db-timestamp.type'; export type { DbTimestamp } from './db-timestamp.type'; +export { IntegerFlagType } from './integer-flag.type'; diff --git a/server/src/db/types/integer-flag.type.ts b/server/src/db/types/integer-flag.type.ts new file mode 100644 index 000000000..05e7a8ee4 --- /dev/null +++ b/server/src/db/types/integer-flag.type.ts @@ -0,0 +1,46 @@ +import { Type, type EntityProperty, type Platform } from '@mikro-orm/core'; + +type FlagValue = boolean | null | undefined; +type FlagColumn = number | null | undefined; + +/** + * A flag the entity reads as a boolean and the database stores as SQLite + * stores every flag: the integer 0 or 1, in an INTEGER column (a `bigint` on + * Postgres, `schema-adjust.ts`). + * + * MikroORM's own `BooleanType` declares a `boolean` column on Postgres and + * writes `true`/`false`, so that one column would hold a different type on + * each engine: a migration's `SET enabled = 1` would fail there, and a Kysely + * read would answer `true` where SQLite answers 1. This type keeps the storage + * model of every other flag on both engines while the entity's field stays a + * boolean. A query value goes through it as well, so `find({ enabled: true })` + * compares with 1. + */ +export class IntegerFlagType extends Type { + override convertToDatabaseValue(value: FlagValue): FlagColumn { + if (value === null || value === undefined) return value; + return value ? 1 : 0; + } + + /** 0/1 from either driver (Postgres's `bigint` reads as a number); a boolean from a value set in JS passes through. */ + override convertToJSValue(value: FlagColumn | boolean): FlagValue { + if (value === null || value === undefined) return value; + return Boolean(Number(value)); + } + + override getColumnType(prop: EntityProperty, platform: Platform): string { + return platform.getIntegerTypeDeclarationSQL(prop); + } + + override compareAsType(): string { + return 'boolean'; + } + + override get runtimeType(): string { + return 'boolean'; + } + + override ensureComparable(): boolean { + return false; + } +} diff --git a/server/tests/unit/db/generate-entities.test.ts b/server/tests/unit/db/generate-entities.test.ts index 82a8d0a71..144a58d35 100644 --- a/server/tests/unit/db/generate-entities.test.ts +++ b/server/tests/unit/db/generate-entities.test.ts @@ -31,6 +31,7 @@ import { checkEntities, dumpEntities, fixDbTimestampClassFieldTypes, + fixFlagClassFieldTypes, fixJsonClassFieldTypes, generateEntities, injectJoinColumns, @@ -161,7 +162,7 @@ describe('RULE1b_markJsonColumns', () => { }); describe('RULE1c_markBooleanColumns', () => { - it('RULE1C-001: marks a known boolean column type=boolean, runtimeType=boolean, coerces a falsy numeric default (I4)', () => { + it('RULE1C-001: marks a known boolean column type=IntegerFlagType, runtimeType=boolean, coerces a falsy numeric default (I4)', () => { const p = fixtureProp({ name: 'enabled', type: 'integer', @@ -173,7 +174,7 @@ describe('RULE1c_markBooleanColumns', () => { }); const meta = fixtureMeta('Addons', 'addons', [p]); const fixups = RULE1c_markBooleanColumns([meta]); - expect(p.type).toBe('boolean'); + expect(p.type).toBe('IntegerFlagType'); expect(p.runtimeType).toBe('boolean'); expect(p.default).toBe(false); expect(p.defaultRaw).toBe('false'); @@ -198,6 +199,17 @@ describe('RULE1c_markBooleanColumns', () => { it('BOOLEAN_COLUMNS-001: contains addons.enabled, grepped from the current hand-written entities', () => { expect(BOOLEAN_COLUMNS.has('addons.enabled')).toBe(true); }); + + it("RULE1C-004: a flag's class field loses the IType wrapper the renderer gives it, and a plain one is left alone", () => { + const fixups = [{ className: 'Addons', propName: 'enabled' }]; + const rendered = 'export class Addons {\n enabled?: IType | null;\n}\n'; + expect(fixFlagClassFieldTypes(rendered, fixups)).toBe('export class Addons {\n enabled?: boolean | null;\n}\n'); + const plain = 'export class Addons {\n enabled?: boolean | null;\n}\n'; + expect(fixFlagClassFieldTypes(plain, fixups)).toBe(plain); + expect(() => fixFlagClassFieldTypes('export class Addons {\n enabled?: number | null;\n}\n', fixups)).toThrow( + /no IType to simplify/, + ); + }); }); describe('RULE1d_fixNaNNumericDefaults', () => { @@ -1365,6 +1377,7 @@ describe('applyTextPasses', () => { defaults: [], timestamps: [], jsonColumns: [], + flags: [], retypedScalars: [], repositoryMarkers: [{ className: 'X', repositoryClassName: 'XRepository' }], referencedColumns: [{ className: 'X', propName: 'countryRef', referencedColumnNames: ['code'] }], diff --git a/server/tests/unit/db/types/integer-flag.type.test.ts b/server/tests/unit/db/types/integer-flag.type.test.ts new file mode 100644 index 000000000..fc11ff746 --- /dev/null +++ b/server/tests/unit/db/types/integer-flag.type.test.ts @@ -0,0 +1,34 @@ +/** + * IntegerFlagType (src/db/types/integer-flag.type.ts): a boolean in the + * entity, the integer 0/1 in the column on both engines, so `addons.enabled` + * has the storage model of every other flag on Postgres as well. + */ +import { IntegerFlagType } from '../../../../src/db/types'; +import { BasePostgreSqlPlatform, SqlitePlatform } from '@mikro-orm/sql'; + +import { describe, expect, it } from 'vitest'; + +const flag = new IntegerFlagType(); + +describe('IntegerFlagType', () => { + it('INTFLAG-001: writes 1 and 0, and leaves a missing value missing', () => { + expect(flag.convertToDatabaseValue(true)).toBe(1); + expect(flag.convertToDatabaseValue(false)).toBe(0); + expect(flag.convertToDatabaseValue(null)).toBeNull(); + expect(flag.convertToDatabaseValue(undefined)).toBeUndefined(); + }); + + it('INTFLAG-002: reads the integer either driver hands back, or a boolean set in JS, as a boolean', () => { + expect(flag.convertToJSValue(1)).toBe(true); + expect(flag.convertToJSValue(0)).toBe(false); + expect(flag.convertToJSValue(true)).toBe(true); + expect(flag.convertToJSValue(null)).toBeNull(); + }); + + it('INTFLAG-003: declares the integer column of the platform, which the Postgres schema widens to bigint', () => { + const prop = { name: 'enabled' } as Parameters[0]; + expect(flag.getColumnType(prop, new SqlitePlatform())).toBe('integer'); + expect(flag.getColumnType(prop, new BasePostgreSqlPlatform())).toBe('int'); + expect(flag.runtimeType).toBe('boolean'); + }); +});