Expand description
Migration: versioned schema migrations for turso-orm.
A migration is a type implementing MigrationTrait (with up and
down) and MigrationName (derived with DeriveMigrationName from
the module name). A MigratorTrait lists the migrations in order and
applies them through a SchemaManager, which wraps the transaction a
migration runs in and offers DDL helpers and catalog lookups.
The crate owns the bookkeeping — which migrations have been applied, in
which order — and the transactional envelope around each one. It does not
own DDL rendering (that is turso_sql, re-exported through the prelude)
nor the connection (that is turso_orm).
§Design decisions
- Each migration runs together with its bookkeeping insert inside one
BEGIN IMMEDIATEtransaction, so a failing migration leaves neither a partial schema nor a stale version row behind. - Versions are the migration names, taken from the module path, so that
every migration struct can simply be called
Migrationand the file name is the single source of truth for ordering. downdefaults to failing withDbErr::Migration, so a migration that cannot be reverted says so instead of silently doing nothing.
§Example
ⓘ
use turso_orm_migration::prelude::*;
mod m20240101_000001_create_user {
use turso_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager<'_>) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table("user")
.col(ColumnDef::integer("id").primary_key().auto_increment())
.col(ColumnDef::text("email").not_null().unique_key()),
)
.await
}
async fn down(&self, manager: &SchemaManager<'_>) -> Result<(), DbErr> {
manager.drop_table(Table::drop().table("user")).await
}
}
}
pub struct Migrator;
#[async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20240101_000001_create_user::Migration)]
}
}Re-exports§
pub use turso_orm;
Modules§
- prelude
- Everything a migration module needs, meant to be glob-imported.
Structs§
- Migration
Status - The status of one declared migration.
- Schema
Manager - Runs DDL inside the migration’s transaction and inspects the catalog.
Enums§
- DbErr
- The error returned by every fallible operation of the ORM.
Traits§
- Migration
Name - The name of a migration, used as its version key in the bookkeeping table.
- Migration
Trait - A migration: a reversible schema change.
- Migrator
Trait - Lists migrations and applies or reverts them in order.
Attribute Macros§
Derive Macros§
- Derive
Migration Name - Derives
MigrationNamefrom the module path.