How Automatic Schema Migrations Work
mainTurbosql automates database schema management via the #[derive(Turbosql)] macro:
- Generation: At compile time, the macro generates a
migrations.tomlfile in your project root describing the schema. - Migration: When you change a struct, the macro generates new SQL migration statements and appends them to
migrations.toml. These migrations are embedded in your binary. - Application: On launch, the binary automatically applies any pending migrations in sequence to the existing database file.
Important Migration Rules:
- One-way Ratchet: Migrations are append-only. An older binary cannot operate on a database file that has been migrated to a newer schema.
- Manual Edits: You can manually remove unused or reverted migrations from
migrations.tomlbefore release, but any database that has already applied them will error and require a rebuild. - Custom Migrations: You can manually add your own SQL migration entries (e.g., for creating indexes) to the bottom of the
migrations_append_onlylist inmigrations.toml.