diff --git a/scripts/check-schema-drift.ts b/scripts/check-schema-drift.ts index 6e6b1f5a..52a929e8 100644 --- a/scripts/check-schema-drift.ts +++ b/scripts/check-schema-drift.ts @@ -10,8 +10,8 @@ import { spawnSync } from "node:child_process"; import { createClient } from "@libsql/client"; import { fileURLToPath, pathToFileURL } from "node:url"; import { + authoringMigrationSql, buildSqlContractDocument, - publishedMigrationSql, readDatabaseFileHeader, } from "../src/db/sql-contract.js"; import { SQLITE_APPLICATION_ID } from "../src/db/client.js"; @@ -152,7 +152,7 @@ function expectedMigrationFileContents(): string { -- database_state rows. See spec/sql/README.md and spec/sql/contract.json. `; - return `${header}${publishedMigrationSql()}\n`; + return `${header}${authoringMigrationSql()}\n`; } function checkGeneratedFilesFresh(): void { diff --git a/spec/sql/README.md b/spec/sql/README.md index af08cebc..185519d2 100644 --- a/spec/sql/README.md +++ b/spec/sql/README.md @@ -1,8 +1,10 @@ # Athletic Standard SQL contract -Language-neutral SQLite schema for Athletic Standard v1. The TypeScript migration in -`src/db/migrations/` is the source of truth. `pnpm generate:sql-contract` writes the -files in this directory. +Language-neutral SQLite schema for Athletic Standard v1. **Runtime and Rust embed the +plain SQL files in this directory** (`migrations/*.sql`). The TypeScript modules in +`src/db/migrations/` remain the authoring source until the Rust Records stage; run +`pnpm generate:sql-contract` after editing those modules so published SQL stays in sync. +`pnpm check:schema-drift` fails when the files here diverge from a freshly created database. ## Versions diff --git a/src/db/migrate.ts b/src/db/migrate.ts index b43514d4..62b72422 100644 --- a/src/db/migrate.ts +++ b/src/db/migrate.ts @@ -20,7 +20,7 @@ import { SQLITE_APPLICATION_MARKER, type AthleticDatabase, } from "./client.js"; -import { INITIAL_SCHEMA_SQL } from "./migrations/001-initial.js"; +import { readCanonicalMigrationSqlFromDisk } from "./sql-contract.js"; import { createUuidV7 } from "./uuid.js"; export interface DatabaseMetadata { @@ -110,7 +110,7 @@ async function installInitialSchema( if (Number(foreignKeys.rows[0]?.foreign_keys) !== 1) { throw new Error("SQLite foreign-key enforcement is disabled in migration transaction"); } - await transaction.executeMultiple(INITIAL_SCHEMA_SQL); + await transaction.executeMultiple(readCanonicalMigrationSqlFromDisk()); await transaction.execute({ sql: ` INSERT INTO database_metadata ( diff --git a/src/db/sql-contract.ts b/src/db/sql-contract.ts index 75a237e1..22142c10 100644 --- a/src/db/sql-contract.ts +++ b/src/db/sql-contract.ts @@ -1,7 +1,35 @@ +import { readFileSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; import { ENFORCEMENT_TRIGGERS_SQL } from "./migrations/enforcement-triggers.js"; import { INITIAL_SCHEMA_SQL } from "./migrations/001-initial.js"; import { SQL_SCHEMA_VERSION, SQLITE_APPLICATION_ID } from "./client.js"; +const packageRoot = join(dirname(fileURLToPath(import.meta.url)), "..", ".."); + +/** Published migration file embedded by Rust and executed by the Node runner. */ +export const CANONICAL_MIGRATION_RELATIVE_PATH = "spec/sql/migrations/001-initial.sql"; + +function stripGeneratedSqlHeader(sql: string): string { + const lines = sql.split("\n"); + const start = lines.findIndex((line) => /^CREATE\s/i.test(line.trim())); + if (start === -1) { + throw new Error("Canonical migration SQL contains no CREATE statement"); + } + return lines.slice(start).join("\n").trim(); +} + +/** SQL applied at runtime; must match `pnpm generate:sql-contract` output. */ +export function readCanonicalMigrationSqlFromDisk(): string { + const path = join(packageRoot, CANONICAL_MIGRATION_RELATIVE_PATH); + return stripGeneratedSqlHeader(readFileSync(path, "utf8")); +} + +/** Authoring source until the Records stage moves edits into plain SQL files only. */ +export function authoringMigrationSql(): string { + return INITIAL_SCHEMA_SQL.trim(); +} + export const MINIMUM_SQLITE_VERSION = "3.45.0"; export const REQUIRED_PRAGMAS = [ @@ -250,7 +278,7 @@ export const MIGRATION_CHAIN = [ ] as const; export function publishedMigrationSql(): string { - return INITIAL_SCHEMA_SQL.trim(); + return readCanonicalMigrationSqlFromDisk(); } export function enforcementTriggerNames(): string[] {