1.8 KiB
1.8 KiB
SQL Migrations
Incremental schema changes applied in order by timestamp prefix.
Naming Convention
Migration directories follow the format:
YYYY-MM-DD-HHMMSS-NNNN_description_goes_here/
up.sql
down.sql
These are created with the diesel tool, and in particular:
diesel migration generate description_goes_here
NNNNis a zero-padded sequence number for ordering multiple migrations on the same timestamp.up.sqlapplies the migration forward.down.sqlreverses it.
Table Conventions
- Primary key
idcolumns must beBIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT. Do not use signed IDs. - Always create tables with
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin. - Application-owned state/status/type columns must not have schema defaults. Use
NOT NULLwithoutDEFAULTwhen required, and have application inserts explicitly supply the value. Document the corresponding Rust enum; application queries bind its variants instead of hardcoding enum strings in SQL. Seecrates/schema/database/mysql_queries/AGENTS.mdfor the query rules. - All SQL files should start with the standard IntelliJ noinspection comments:
-- noinspection SqlDialectInspectionForFile -- noinspection SqlNoDataSourceInspectionForFile -- noinspection SqlResolveForFile
Rules
- Keep documentation in this directory as regular files, not symlinks. Diesel can treat a documentation symlink as a migration directory and reject the entire migration list.
- Every
up.sqlmust have a correspondingdown.sqlthat cleanly reverses the migration. - For
CREATE TABLEmigrations, thedown.sqlis typicallyDROP TABLE IF EXISTS table_name;. - For
ALTER TABLEmigrations, thedown.sqlreverses the alteration (e.g.DROP COLUMN).