Skip to main content

docbox_database/models/
root_migration.rs

1use crate::{DbExecutor, DbResult};
2use chrono::{DateTime, Utc};
3use serde::Serialize;
4use sqlx::prelude::FromRow;
5
6/// Structure for tracking migrations applied to the root
7#[derive(Debug, Clone, FromRow, Serialize)]
8pub struct RootMigration {
9    pub name: String,
10    pub applied_at: DateTime<Utc>,
11}
12
13pub struct CreateRootMigration {
14    pub name: String,
15    pub applied_at: DateTime<Utc>,
16}
17
18impl RootMigration {
19    /// Create a new tenant migration
20    pub async fn create(db: impl DbExecutor<'_>, create: CreateRootMigration) -> DbResult<()> {
21        sqlx::query(
22            r#"
23            INSERT INTO "docbox_root_migrations" ("name", "applied_at")
24            VALUES ($1, $2)
25        "#,
26        )
27        .bind(create.name)
28        .bind(create.applied_at)
29        .execute(db)
30        .await?;
31
32        Ok(())
33    }
34
35    /// Find all applied migrations
36    pub async fn all(db: impl DbExecutor<'_>) -> DbResult<Vec<RootMigration>> {
37        sqlx::query_as(r#"SELECT * FROM "docbox_root_migrations""#)
38            .fetch_all(db)
39            .await
40    }
41}