schema-migration
Skill4 starsChanged 9 months agoMITView on GitHub
Add or modify a PostgreSQL schema migration for the CMS or Facts store. Covers creating migration functions, stored procedure changes, diff file generation, and TypeScript caller updates.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- dosu-ai/decanttheir own version, first seen Sep 29, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- dosu-ai/decant★ 66same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository22 other files it gives its agents.
- .github/copilot-instructions.mdCopilot instructions
- duroxide-orchestration-versioningSkill
- add-activitySkill
- add-eventSkill
- add-testSkill
- add-toolSkill
- agent-versioningSkill
- azure-ps-test-runner-vmSkill
- debug-orchestrationSkill
- debug-testSkill
- horizondb-idiosyncrasiesSkill
- horizon-facts-eval-sweepSkill
- investigate-duroxide-runtimeSkill
- pilotswarm-aks-deploySkill
- pilotswarm-aks-resetSkill
- pilotswarm-new-env-deploySkill
- pilotswarm-perfSkill
- pilotswarm-portal-app-regSkill
- pilotswarm-portal-auth-assignmentsSkill
- pilotswarm-releaseSkill
- pilotswarm-tuiSkill
- pilotswarm-vpn-client-profileSkill
The file
We split this skill into the rules it gives an agent
12 rules. About 0 are common, 4 are distinctive, and 8 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
8
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 26
Add a new entry to CMS_MIGRATIONS() in cms-migrations.ts or FACTS_MIGRATIONS() in facts-migrations.ts:
- Very specific: line 60
Idempotent: Use IF NOT EXISTS, IF EXISTS, CREATE OR REPLACE so re-running is safe.
- Very specific: line 62
Stored procedures: All new data-access queries must be stored procedures (CREATE OR REPLACE FUNCTION).
- Very specific: line 64
Version numbering: Use sequential 4-digit zero-padded versions (0001, 0002, ...).
- Distinctive: line 68
Every migration that modifies schema or stored procedures must have a companion diff markdown file.
- Very specific: line 70
Create the diff file at packages/sdk/src/migrations/NNNN_diff.md (CMS) or packages/sdk/src/migrations/NNNN_facts_diff.md (Facts).
- Very specific: line 105
Diff: Run diff -u baseline.sql new.sql to get unified diff output.
- Very specific: line 106
Include: Place the diff hunks in a `diff code block under a heading that notes the baseline migration number.
- Distinctive: line 114
After adding or modifying a stored procedure:
- Distinctive: line 148
Never edit a previous migration.
- Very specific: line 150
Add a new migration with CREATE OR REPLACE FUNCTION for the modified procedure.
- Distinctive: line 152
Update the TypeScript caller if the function signature changed.
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Schema Migration
The CMS and Facts stores use versioned SQL migrations with advisory-lock serialization. All Postgres data access goes through stored procedures created by these migrations. Schema changes follow the same pattern as duroxide-pg: ordered migration functions, idempotent DDL, and a required companion diff file for code review.
Architecture
pg-migrator.ts — shared advisory-lock migration runner
cms-migrator.ts — thin CMS wrapper (lock seed 0x636D73)
facts-migrator.ts — thin Facts wrapper (lock seed 0x666163)
cms-migrations.ts — ordered CMS migration list
facts-migrations.ts — ordered Facts migration listEach system maintains its own schema_migrations table within its Postgres schema (copilot_sessions for CMS, pilotswarm_facts for Facts). Migrations are applied automatically on initialize().
Adding a New Migration
1. Write the migration function
Add a new entry to CMS_MIGRATIONS() in cms-migrations.ts or FACTS_MIGRATIONS() in facts-migrations.ts:
{
version: "NNNN",
name: "descriptive_name",
sql: migration_NNNN_descriptive_name(schema),
},Then define the function:
function migration_NNNN_descriptive_name(schema: string): string {
const s = `"${schema}"`;
return `
-- NNNN_descriptive_name: what this migration does.
-- DDL changes (idempotent)
CREATE TABLE IF NOT EXISTS ${s}.new_table (...);
ALTER TABLE ${s}.existing_table ADD COLUMN IF NOT EXISTS new_col TEXT;
-- Stored procedure changes
CREATE OR REPLACE FUNCTION ${s}.my_proc(...) RETURNS ... AS $$
BEGIN
...
END;
$$ LANGUAGE plpgsql;
`;
}2. Key rules for migration SQL
- Idempotent: Use
IF NOT EXISTS,IF EXISTS,CREATE OR REPLACEso re-running is safe. - Schema-parameterized: The
schemaargument is interpolated into all qualified names. Never hard-code a schema name. - Stored procedures: All new data-access queries must be stored procedures (
CREATE OR REPLACE FUNCTION). No inline SQL in TypeScript. - Transactional: Each migration runs inside its own
BEGIN/COMMITblock (handled by the migrator). - Version numbering: Use sequential 4-digit zero-padded versions (
0001,0002, ...). Check the current highest version in the migrations list.
3. Generate the diff file (REQUIRED)
Every migration that modifies schema or stored procedures must have a companion diff markdown file. This is required because git diffs for SQL-in-TypeScript migrations only show the new code, not the delta from the previous version.
Create the diff file at packages/sdk/src/migrations/NNNN_diff.md (CMS) or packages/sdk/src/migrations/NNNN_facts_diff.md (Facts).
Diff format
# Diff for migration NNNN
Migration file: `cms-migrations.ts` — `migration_NNNN_name`
## Table Changes
### `table_name` — new table
(full DDL in a ```sql block)
### `table_name` — modified
(mark new columns with `+` in a ```diff block)
## New Indexes
(full DDL in ```sql blocks, or "None.")
## Function Changes
### `func_name` — new
(signature in a ```diff block with `+` markers, prose description)
### `func_name` — body modified (baseline: NNNN)
(unified diff in a ```diff block showing changed lines with +/- markers)How to produce diffs for modified stored procedures
- Find the baseline: Identify the most recent migration that contains
CREATE OR REPLACE FUNCTION ... func_name. That is the baseline version. - Extract both bodies: Copy the function body from the baseline migration and the new migration.
- Normalize: Replace schema placeholders with
SCHEMA.for consistent diffing. - Diff: Run
diff -u baseline.sql new.sqlto get unified diff output. - Include: Place the diff hunks in a
```diffcode block under a heading that notes the baseline migration number.
See existing examples:
packages/sdk/src/migrations/0004_diff.md(all-new CMS stored procedures)packages/sdk/src/migrations/0002_facts_diff.md(all-new Facts stored procedures)
4. Update TypeScript callers
After adding or modifying a stored procedure:
- Update
sqlForSchema()— add the new function name to thefnmap incms.tsorfacts-store.ts:fn: { myNewProc: `${s}.cms_my_new_proc`, } - Update the provider method — call the stored proc instead of inline SQL:
async myMethod(args: MyArgs): Promise<MyResult> { const { rows } = await this.pool.query( `SELECT * FROM ${this.sql.fn.myNewProc}($1, $2)`, [args.foo, args.bar], ); return rows.map(rowToMyResult); } - Row mappers stay in TypeScript —
rowToSessionRow(),rowToSessionEvent(), etc. still handle PG snake_case → TS camelCase conversion.
5. Build and test
cd packages/sdk
npm run build # TypeScript compilation
npx vitest run test/local/ # integration testsMigrations are applied automatically when initialize() is called. Existing deployments will pick up new migrations on next startup.
Modifying an Existing Stored Procedure
Never edit a previous migration. Instead:
- Add a new migration with
CREATE OR REPLACE FUNCTIONfor the modified procedure. - Generate a diff file showing the delta from the baseline version.
- Update the TypeScript caller if the function signature changed.
Migration System Internals
- Advisory locks: Each system uses a unique lock seed hashed with the schema name to prevent concurrent workers from double-applying migrations.
- Tracking table:
{schema}.schema_migrationswith columns(version TEXT PK, name TEXT, applied_at TIMESTAMPTZ). - Ordering: Migrations are applied in array order. The version string is compared against the tracking table.
- Error handling: Failed migrations trigger
ROLLBACKand re-throw. The migration is not recorded as applied.
Key files
- packages/sdk/src/pg-migrator.ts — shared migration runner
- packages/sdk/src/cms-migrations.ts — CMS migration definitions
- packages/sdk/src/facts-migrations.ts — Facts migration definitions
- packages/sdk/src/cms-migrator.ts — CMS migrator wrapper
- packages/sdk/src/facts-migrator.ts — Facts migrator wrapper
- packages/sdk/src/cms.ts — CMS provider (stored proc callers)
- packages/sdk/src/facts-store.ts — Facts provider (stored proc callers)
- packages/sdk/src/migrations/ — diff files for code review
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

