Clean up the schema: drop unused tables, rename scopes, publishable awards
Migrations 020–023, with the code that reads each: - 020 drops people_lists, people_list_members, v_chapters and v_person_affiliations. Nothing queried any of them. - 021 renames event_sections to event_scopes and events.section_id to scope_id, finishing what 015 described. The API sends `scopes` and `scope_id`, and useEvents, EventListCards and EventCalendar take `scope`. It also inserts national/regional/partner, which only the retired seed ever created: a database built from migrations alone had no scope for the Retreats bands. - 022 drops events.sort_order and people.sort_order. Events now sort by date (upcoming soonest first, past latest first, undated last) on /events, org pages and the countdown. People were only ever sorted by sort_name on the site. Every other sort_order stays. - 023 rebuilds teams with created_at and updated_at plus a touch trigger, so the teams editor gets the same optimistic concurrency as the other entities. Awards can be drafted: is_published (added in 011) is on both descriptor halves with a Publishing group, and the award list, award page, org awards and event awards leave drafts out. The migration runner now turns foreign keys off around the per-file transactions and runs foreign_key_check before each commit. PRAGMA foreign_keys is a no-op inside a transaction, so 009's warning was right and a rebuild of a referenced table (023) couldn't be written otherwise. CLAUDE.md is updated to match. Also removes the stray src/App.tsx.save. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
8790f861fe
commit
25e592bfea
19 changed files with 339 additions and 423 deletions
|
|
@ -82,6 +82,16 @@ export function tx(db, fn) {
|
|||
|
||||
Migrations only ever go forward. To undo something, write a
|
||||
new migration.
|
||||
|
||||
Foreign keys are off while migrations run, the recipe from
|
||||
the SQLite docs for changing a table's shape. PRAGMA
|
||||
foreign_keys is a no-op inside a transaction, so it has to be
|
||||
set here, around the per-file transactions, rather than in
|
||||
the file. With it on, rebuilding a table something references
|
||||
(drop the old one, rename the new one into place) either
|
||||
cascades into the children or fails the commit. Instead each
|
||||
file ends with a foreign_key_check, and any orphan it leaves
|
||||
rolls that file back.
|
||||
───────────────────────────────────────────────────────────── */
|
||||
|
||||
export function migrate(db, { log = console.log } = {}) {
|
||||
|
|
@ -92,25 +102,41 @@ export function migrate(db, { log = console.log } = {}) {
|
|||
.sort();
|
||||
|
||||
let applied = 0;
|
||||
const enforced = db.prepare("PRAGMA foreign_keys").get().foreign_keys;
|
||||
db.exec("PRAGMA foreign_keys = OFF");
|
||||
|
||||
for (const file of files) {
|
||||
const version = Number.parseInt(file.slice(0, 3), 10);
|
||||
try {
|
||||
for (const file of files) {
|
||||
const version = Number.parseInt(file.slice(0, 3), 10);
|
||||
|
||||
if (!Number.isInteger(version) || version < 1) {
|
||||
throw new Error(`Migration "${file}" must start with a number, e.g. 001_`);
|
||||
if (!Number.isInteger(version) || version < 1) {
|
||||
throw new Error(`Migration "${file}" must start with a number, e.g. 001_`);
|
||||
}
|
||||
if (version <= current) continue;
|
||||
|
||||
const sql = readFileSync(join(MIGRATIONS_DIR, file), "utf8");
|
||||
|
||||
tx(db, () => {
|
||||
db.exec(sql);
|
||||
|
||||
const orphans = db.prepare("PRAGMA foreign_key_check").all();
|
||||
if (orphans.length > 0) {
|
||||
const where = orphans
|
||||
.slice(0, 5)
|
||||
.map((o) => `${o.table} row ${o.rowid} → ${o.parent}`)
|
||||
.join(", ");
|
||||
throw new Error(`${file} leaves ${orphans.length} foreign key violation(s): ${where}`);
|
||||
}
|
||||
|
||||
// Not parameterisable, but version is a validated integer.
|
||||
db.exec(`PRAGMA user_version = ${version}`);
|
||||
});
|
||||
|
||||
log(`migrated → ${file}`);
|
||||
applied += 1;
|
||||
}
|
||||
if (version <= current) continue;
|
||||
|
||||
const sql = readFileSync(join(MIGRATIONS_DIR, file), "utf8");
|
||||
|
||||
tx(db, () => {
|
||||
db.exec(sql);
|
||||
// Not parameterisable, but version is a validated integer.
|
||||
db.exec(`PRAGMA user_version = ${version}`);
|
||||
});
|
||||
|
||||
log(`migrated → ${file}`);
|
||||
applied += 1;
|
||||
} finally {
|
||||
if (enforced) db.exec("PRAGMA foreign_keys = ON");
|
||||
}
|
||||
|
||||
const final = db.prepare("PRAGMA user_version").get().user_version;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue