Migration 083 gives the section types that need more than blocks their own tables (hero, slides, countdown, stats, paths, featured cards, the Community blog's slots, pinned posts and feeds) and turns the front page into Home's sections: the hero, then its bands in order. Any section can be hidden. The Front page editor, its tables and the Community page's are gone; /api/front-page and /api/community-blog fold into the page's sections (sectionData.js). The admin engine reads and writes a child collection's own side tables and gated nested collections. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
172 lines
6.2 KiB
JavaScript
172 lines
6.2 KiB
JavaScript
/* ═══════════════════════════════════════════════════════════════
|
|
DESCRIPTOR / SCHEMA RECONCILIATION
|
|
|
|
The CRUD engine trusts admin-schema.js about which fields are
|
|
required. The database is the one that actually enforces it. When
|
|
those two disagree the gap shows up as a 500 on somebody's save,
|
|
which is how organizations.country was found.
|
|
|
|
Run this once at boot. For every table a descriptor touches it
|
|
compares the declared columns against PRAGMA table_info and:
|
|
|
|
- marks a descriptor column required when the table says
|
|
NOT NULL with no DEFAULT, so validation catches it as a named
|
|
field instead of SQLite catching it as a 500;
|
|
- reports a descriptor column the table doesn't have;
|
|
- reports a NOT NULL, no-DEFAULT column that no descriptor
|
|
column and no engine-supplied column covers: nothing can
|
|
ever set it, so every insert through the admin will fail.
|
|
|
|
The first is a silent repair. The other two are deployment bugs,
|
|
so they stop the service starting rather
|
|
than waiting to surface one row at a time.
|
|
═══════════════════════════════════════════════════════════════ */
|
|
|
|
/* The values a CHECK (column IN (...)) on the table allows, read from
|
|
its CREATE TABLE, or null when there's no such CHECK. */
|
|
function checkValues(db, table, column) {
|
|
const sql = db.prepare(`SELECT sql FROM sqlite_schema WHERE type = 'table' AND name = ?`).get(table)?.sql ?? "";
|
|
const match = new RegExp(`\\b${column}"?\\s+IN\\s*\\(([^)]*)\\)`, "i").exec(sql);
|
|
if (!match) return null;
|
|
return new Set([...match[1].matchAll(/'((?:[^']|'')*)'/g)].map((m) => m[1].replace(/''/g, "'")));
|
|
}
|
|
|
|
function tableMeta(db, table) {
|
|
const rows = db.prepare(`PRAGMA table_info(${table})`).all();
|
|
if (!rows.length) return null;
|
|
|
|
const meta = new Map();
|
|
for (const row of rows) {
|
|
meta.set(row.name, {
|
|
// A NOT NULL column with no default and no engine to fill it
|
|
// has to arrive in the payload or the insert dies.
|
|
mustSupply: row.notnull === 1 && row.dflt_value === null && row.pk === 0,
|
|
notNullable: row.notnull === 1 && row.pk === 0,
|
|
});
|
|
}
|
|
return meta;
|
|
}
|
|
|
|
/* Every table the descriptor writes to, with the columns it
|
|
declares and the ones the engine fills in on its own. */
|
|
function collectGroups(entity) {
|
|
const groups = [
|
|
{
|
|
table: entity.table,
|
|
columns: entity.columns ?? [],
|
|
engineSupplied: [entity.idColumn],
|
|
},
|
|
];
|
|
|
|
for (const ext of entity.extensions ?? []) {
|
|
groups.push({
|
|
table: ext.table,
|
|
columns: ext.columns ?? [],
|
|
// An extension is keyed either by the parent's own id or by an
|
|
// owner block, the same one children use. Both sets of columns
|
|
// are filled in by the engine, never by the form.
|
|
engineSupplied: [
|
|
ext.idColumn,
|
|
ext.owner?.column,
|
|
ext.owner?.kindColumn,
|
|
...(ext.touch ? ["updated_at"] : []),
|
|
].filter(Boolean),
|
|
});
|
|
}
|
|
|
|
const walk = (child) => {
|
|
groups.push({
|
|
table: child.table,
|
|
columns: child.columns ?? [],
|
|
engineSupplied: [
|
|
child.owner.column,
|
|
child.owner.kindColumn,
|
|
"sort_order",
|
|
...Object.keys(child.owner.inherit ?? {}),
|
|
].filter(Boolean),
|
|
});
|
|
for (const ext of child.extensions ?? []) {
|
|
groups.push({
|
|
table: ext.table,
|
|
columns: ext.columns ?? [],
|
|
engineSupplied: [ext.idColumn, ext.owner?.column, ext.owner?.kindColumn].filter(Boolean),
|
|
});
|
|
}
|
|
for (const nested of child.children ?? []) walk(nested);
|
|
};
|
|
|
|
for (const child of entity.children ?? []) walk(child);
|
|
|
|
return groups;
|
|
}
|
|
|
|
export function syncDescriptorsWithSchema(db, entities) {
|
|
const list = Array.isArray(entities) ? entities.map((e) => [e.name ?? e.table, e]) : Object.entries(entities);
|
|
|
|
const inferred = [];
|
|
const problems = [];
|
|
|
|
for (const [name, entity] of list) {
|
|
for (const group of collectGroups(entity)) {
|
|
const meta = tableMeta(db, group.table);
|
|
|
|
if (!meta) {
|
|
problems.push(`${name}: table "${group.table}" does not exist.`);
|
|
continue;
|
|
}
|
|
|
|
const declared = new Set(group.columns.map((column) => column.name));
|
|
|
|
for (const column of group.columns) {
|
|
const info = meta.get(column.name);
|
|
if (!info) {
|
|
problems.push(
|
|
`${name}: ${group.table}.${column.name} is in the descriptor but not in the table.`,
|
|
);
|
|
continue;
|
|
}
|
|
if (info.mustSupply && !column.required) {
|
|
column.required = true;
|
|
inferred.push(`${name}: ${group.table}.${column.name}`);
|
|
}
|
|
if (info.notNullable) column.notNullable = true;
|
|
|
|
// A dropdown value the table's CHECK refuses would only fail
|
|
// when someone saves it; say so at boot instead.
|
|
if (column.type === "enum") {
|
|
const allowed = checkValues(db, group.table, column.name);
|
|
const refused = allowed ? column.values.filter((value) => !allowed.has(value)) : [];
|
|
if (refused.length) {
|
|
problems.push(
|
|
`${name}: ${group.table}.${column.name} offers ${refused.join(", ")}, ` +
|
|
`which the table's CHECK doesn't allow.`,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
for (const [columnName, info] of meta) {
|
|
if (!info.mustSupply) continue;
|
|
if (declared.has(columnName)) continue;
|
|
if (group.engineSupplied.includes(columnName)) continue;
|
|
problems.push(
|
|
`${name}: ${group.table}.${columnName} is NOT NULL with no default, ` +
|
|
`but no descriptor column covers it, so every insert will fail.`,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (inferred.length) {
|
|
console.warn(
|
|
`[admin-schema] marked required from the schema (add required: true to the descriptor):\n ${inferred.join("\n ")}`,
|
|
);
|
|
}
|
|
|
|
if (problems.length) {
|
|
const report = `[admin-schema] descriptor does not match the database:\n ${problems.join("\n ")}`;
|
|
throw new Error(report);
|
|
}
|
|
|
|
return { inferred, problems };
|
|
}
|