|
| 1 | +--- |
| 2 | +name: crdb-change |
| 3 | +description: Generate CockroachDB SQL and migrations for schema changes. Use when creating migrations, updating the database schema, or when the user mentions migrations, schema changes, or dbinit.sql. |
| 4 | +--- |
| 5 | + |
| 6 | +# CockroachDB database changes |
| 7 | + |
| 8 | +Generate database changes for this repository. This includes changes to: |
| 9 | + |
| 10 | +- `schema/crdb/dbinit.sql` |
| 11 | +- Migrations for changes |
| 12 | + |
| 13 | +## Instructions |
| 14 | + |
| 15 | +Follow these steps in order. Do not skip ahead. |
| 16 | + |
| 17 | +### Step 1: Ascertain the scope of the request |
| 18 | + |
| 19 | +If not already provided, prompt the user whether they'd like to: |
| 20 | + |
| 21 | +- **Perform both a dbinit.sql change and a migration**: If this is the case, then: |
| 22 | + 1. Ask the user to describe the changes they'd like to perform to `dbinit.sql`. |
| 23 | + 2. Make those changes, asking the user followup questions as necessary. |
| 24 | + 3. Go directly to step 3, with the scope of the migration being to cover those changes. |
| 25 | +- **Write migrations for pre-existing changes**: In this case, changes need to be fetched from version control. Go to step 2. |
| 26 | + |
| 27 | +### Step 2: Get the diff |
| 28 | + |
| 29 | +Check if `.jj` exists in the repository to determine whether to use jj or git commands. |
| 30 | + |
| 31 | +Prompt the user to ask where the schema changes are: |
| 32 | + |
| 33 | +- **Uncommitted changes**: Changes not yet committed. |
| 34 | + - git: `git diff -- schema/crdb/dbinit.sql` (unstaged) or `git diff --cached -- schema/crdb/dbinit.sql` (staged) |
| 35 | + - jj: `jj diff -- schema/crdb/dbinit.sql` |
| 36 | + |
| 37 | +- **This commit only** (stacked diff workflow): Changes are in the current commit only. |
| 38 | + - git: `git diff HEAD^ -- schema/crdb/dbinit.sql` |
| 39 | + - jj: `jj diff --from @-- -- schema/crdb/dbinit.sql` |
| 40 | + |
| 41 | +- **This branch** (feature branch workflow): Changes span the entire branch. |
| 42 | + - git: `git diff $(git merge-base HEAD main) -- schema/crdb/dbinit.sql` |
| 43 | + - jj: `jj diff --from 'fork_point(trunk() | @)' -- schema/crdb/dbinit.sql` |
| 44 | + |
| 45 | +If the diff doesn't show anything, ask the user which ref to diff from. |
| 46 | + |
| 47 | +### Step 3: Create migration folder |
| 48 | + |
| 49 | +Create a new folder under `schema/crdb/` using the provided name or a short descriptive name derived from the schema changes. |
| 50 | + |
| 51 | +Use existing folder names in `schema/crdb/` as examples for naming conventions. |
| 52 | + |
| 53 | +NOTE: The numbered folders, e.g. 1.0.0, are for legacy support only. No additional numbered directories should be added. |
| 54 | + |
| 55 | +### Step 4: Write migration files |
| 56 | + |
| 57 | +Based on the diff from step 1, write migration files in order: |
| 58 | + |
| 59 | +- Use `up01.sql`, `up02.sql` etc. (zero-padded) if you have more than 10 files. |
| 60 | +- Use `up1.sql`, `up2.sql` etc. if you have 10 or fewer files. |
| 61 | +- For Data Definition Language (DDL) statements, **one statement per file!** |
| 62 | +- For Data Modifying Language (DML) statements, multiple statements are allowed per file. |
| 63 | +- For `ALTER TABLE` with multiple columns, you can add them all in one statement. |
| 64 | +- When adding `NOT NULL` columns to existing tables, add temporary defaults, then remove them in later migration files. |
| 65 | +- Use `IF NOT EXISTS` for idempotency where supported. |
| 66 | +- Individual `up.sql` files are executed within a transaction (this always happens), and should be idempotent (this is an expectation that the migration author must uphold, with, e.g. `IF NOT EXISTS`). |
| 67 | + |
| 68 | +### Step 5: Update dbinit.sql version |
| 69 | + |
| 70 | +Bump the version number at the end of `schema/crdb/dbinit.sql`. |
| 71 | + |
| 72 | +### Step 6: Update SCHEMA_VERSION |
| 73 | + |
| 74 | +In `nexus/db-model/src/schema_versions.rs`, bump `SCHEMA_VERSION`. |
| 75 | + |
| 76 | +### Step 7: Add to KNOWN_VERSIONS |
| 77 | + |
| 78 | +In `nexus/db-model/src/schema_versions.rs`, add the new version to the `KNOWN_VERSIONS` list. |
| 79 | + |
| 80 | +### Step 8: Test the migration |
| 81 | + |
| 82 | +Run `cargo nextest run -p omicron-nexus schema` to verify that the migration is correct. |
| 83 | + |
| 84 | +## Common issues |
| 85 | + |
| 86 | +- Don't guess table names—use the diff from step 1 to find the correct table. |
| 87 | +- When adding constraints, always use `IF NOT EXISTS` for idempotency. |
| 88 | +- For columns with defaults in migration but not in final schema, add the defaults during migration then remove them. |
| 89 | +- Don't skip step 1—always run the diff command first to understand what needs to be migrated. |
| 90 | + |
| 91 | +## Data migration tests |
| 92 | + |
| 93 | +When creating a migration that affects existing data (like adding columns to existing tables), also add a data migration test in `nexus/tests/integration_tests/schema.rs`: |
| 94 | + |
| 95 | +- Add `before_X_0_0` function to create test data in the old format. |
| 96 | +- Add `after_X_0_0` function to verify the migration worked correctly. |
| 97 | +- Add the version to the `get_migration_checks()` map. |
| 98 | + |
| 99 | +This ensures old rows can be migrated smoothly in production. |
| 100 | + |
| 101 | +## Reference |
| 102 | + |
| 103 | +Consult `schema/crdb/README.adoc` for more information. |
0 commit comments