mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 21:11:14 +00:00
fix(db): store addons.enabled as the integer flag on Postgres too
addons.enabled was the one flag MikroORM's boolean type declared as a Postgres boolean, while SQLite and every other flag hold 0/1, so an addon migration's usual SET enabled = 1 would fail there and a Kysely read would answer true instead of 1. The column now uses IntegerFlagType: the entity still reads a boolean, both engines store 0/1, and the regenerated Postgres baseline declares it bigint default 0. The entity generator renders the type for the columns in BOOLEAN_COLUMNS.
This commit is contained in:
@@ -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<number, boolean>` — 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<boolean, number>` (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<boolean, number>`: 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\s*,\s*number>/, '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<boolean, number> 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 <Name>` — 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<GenerateResult> {
|
||||
defaults: [],
|
||||
timestamps: [],
|
||||
jsonColumns: [],
|
||||
flags: [],
|
||||
retypedScalars: [],
|
||||
repositoryMarkers: [],
|
||||
referencedColumns: [],
|
||||
@@ -2039,6 +2069,7 @@ export async function generateEntities(): Promise<GenerateResult> {
|
||||
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),
|
||||
|
||||
Reference in New Issue
Block a user