Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/check-schema-drift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions spec/sql/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/db/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 (
Expand Down
30 changes: 29 additions & 1 deletion src/db/sql-contract.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -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[] {
Expand Down
Loading