Migration

Trait Migration 

Source
pub trait Migration {
    const ID: &'static str;
    const DEPENDENCIES: &'static [&'static str] = _;

    // Required methods
    fn up() -> Vec<Operation>;
    fn down() -> Vec<Operation>;
}
Expand description

A database migration with typed up/down operations.

Implement this trait for each migration in your application.

§Example

use oxide_sql_core::migrations::{
    Migration, Operation, CreateTableBuilder,
    bigint, varchar, timestamp,
};

pub struct Migration0001;

impl Migration for Migration0001 {
    const ID: &'static str = "0001_create_users";

    fn up() -> Vec<Operation> {
        vec![
            CreateTableBuilder::new()
                .name("users")
                .column(bigint("id").primary_key().autoincrement().build())
                .column(varchar("username", 255).not_null().unique().build())
                .build()
                .into(),
        ]
    }

    fn down() -> Vec<Operation> {
        vec![
            Operation::drop_table("users"),
        ]
    }
}

Required Associated Constants§

Source

const ID: &'static str

Unique migration identifier (e.g., “0001_initial”, “0002_add_email”).

This ID is stored in the migrations table to track which migrations have been applied.

Provided Associated Constants§

Source

const DEPENDENCIES: &'static [&'static str] = _

Dependencies on other migrations (must run first).

Each string should be the ID of another migration.

Required Methods§

Source

fn up() -> Vec<Operation>

Apply the migration (forward).

Returns a list of operations to execute.

Source

fn down() -> Vec<Operation>

Reverse the migration (backward).

Returns a list of operations to execute to undo the migration. Return an empty vec if the migration is not reversible.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§