Skip to main content

modo_db/
migration.rs

1use std::future::Future;
2use std::pin::Pin;
3
4/// The `_modo_migrations` table tracks which migrations have been executed.
5pub(crate) mod migration_entity {
6    use sea_orm::entity::prelude::*;
7
8    #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
9    #[sea_orm(table_name = "_modo_migrations")]
10    pub struct Model {
11        #[sea_orm(primary_key, auto_increment = false)]
12        pub version: i64,
13        pub description: String,
14        pub executed_at: String,
15    }
16
17    #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
18    pub enum Relation {}
19
20    impl ActiveModelBehavior for ActiveModel {}
21}
22
23/// Type alias for migration handler functions.
24///
25/// The function receives a reference to the database connection and returns a
26/// boxed future that resolves to `Result<(), modo::Error>`. This is the shape
27/// expected by the `#[modo_db::migration]` macro.
28pub type MigrationFn = fn(
29    &sea_orm::DatabaseConnection,
30) -> Pin<Box<dyn Future<Output = Result<(), modo::Error>> + Send + '_>>;
31
32/// Registration info for a migration, collected via `inventory`.
33///
34/// Use the `#[modo_db::migration(version = N, description = "...")]` attribute
35/// macro to register migrations automatically — do not construct this struct
36/// directly.
37pub struct MigrationRegistration {
38    pub version: u64,
39    pub description: &'static str,
40    pub group: &'static str,
41    pub handler: MigrationFn,
42}
43
44inventory::collect!(MigrationRegistration);
45
46// Register _modo_migrations as a framework entity
47inventory::submit! {
48    crate::EntityRegistration {
49        table_name: "_modo_migrations",
50        group: "default",
51        register_fn: |sb| sb.register(migration_entity::Entity),
52        is_framework: true,
53        extra_sql: &[],
54    }
55}