laterite-core 0.1.0

Laterite CMF application kernel: config, errors, database, migrations, modules
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Portable, reversible migrations.
//!
//! A migration is a Rust unit implementing [`Migration`]: a stable `name`, an
//! `up`, and an optional `down` (absent means the migration is irreversible).
//! DDL is written with the `sea-query` schema builder through the [`Schema`]
//! handle, which renders for whichever backend the deployment runs on, so one
//! migration serves Postgres, MySQL, and SQLite. Raw SQL stays available as an
//! escape hatch, and [`SqlMigration`] wraps a pure-SQL up/down pair.
//!
//! A module exposes an ordered [`MigrationSet`] (its version history). The
//! runner applies pending migrations ([`run`]) and reverses them ([`rollback`],
//! [`reset`]), tracking what is applied by `(module_id, name)` in a single
//! `laterite_migrations` table. Queries run over `sqlx::Any`, so the same runner
//! drives every supported backend.

use async_trait::async_trait;
use sea_query::{
    ColumnDef, Expr, Iden, Index, MysqlQueryBuilder, PostgresQueryBuilder, Query,
    QueryStatementWriter, SchemaStatementBuilder, SqliteQueryBuilder, Table,
};
use sqlx::{AnyConnection, AnyPool};

use crate::error::{CoreError, CoreResult};

/// The database backend a deployment runs on. Selects the `sea-query` renderer
/// so migrations and the runner emit SQL the target understands.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DbBackend {
    Postgres,
    Mysql,
    Sqlite,
}

impl DbBackend {
    /// Infers the backend from a connection URL scheme
    /// (`postgres://`, `mysql://`, `sqlite:`).
    pub fn from_url(url: &str) -> CoreResult<Self> {
        if url.starts_with("postgres") {
            Ok(Self::Postgres)
        } else if url.starts_with("mysql") {
            Ok(Self::Mysql)
        } else if url.starts_with("sqlite") {
            Ok(Self::Sqlite)
        } else {
            Err(CoreError::Config(format!(
                "unrecognised database URL scheme: {url}"
            )))
        }
    }
}

/// A portable boolean column, stored as a 0/1 integer. Use this in a migration
/// instead of `ColumnDef::new(name).boolean()`: `sqlx::Any` cannot decode a
/// SQLite `boolean` column, so integer is the representation that works on every
/// backend. Bind and read the value as a normal `bool` through the query layer
/// (see `crate::query`). Chain `.not_null()`, `.default(0)`, and so on as usual.
pub fn bool_col<T: sea_query::IntoIden>(name: T) -> ColumnDef {
    ColumnDef::new(name).integer().to_owned()
}

/// The length of a [`key_col`], generous enough for UUIDs, codes, usernames,
/// emails, and token hashes while staying within MySQL's index-length limit.
pub const KEY_LEN: u32 = 255;

/// A portable key column: a bounded `varchar` rather than `text`. Use this in a
/// migration for any column that participates in a key, index, or foreign key
/// (ids, codes, tokens, and columns named in an `Index`): MySQL cannot index a
/// `text` column without a prefix length, so a plain `text` id or unique column
/// fails there. A bounded string keys cleanly on every backend. It holds the
/// same UTF-8 strings a `text` column would, so application code is unchanged.
/// Chain `.not_null()`, `.primary_key()`, `.unique_key()`, and so on as usual.
pub fn key_col<T: sea_query::IntoIden>(name: T) -> ColumnDef {
    ColumnDef::new(name).string_len(KEY_LEN).to_owned()
}

fn schema_sql<S: SchemaStatementBuilder>(backend: DbBackend, stmt: &S) -> String {
    match backend {
        DbBackend::Postgres => stmt.build(PostgresQueryBuilder),
        DbBackend::Mysql => stmt.build(MysqlQueryBuilder),
        DbBackend::Sqlite => stmt.build(SqliteQueryBuilder),
    }
}

fn query_sql<Q: QueryStatementWriter>(backend: DbBackend, stmt: &Q) -> String {
    match backend {
        DbBackend::Postgres => stmt.to_string(PostgresQueryBuilder),
        DbBackend::Mysql => stmt.to_string(MysqlQueryBuilder),
        DbBackend::Sqlite => stmt.to_string(SqliteQueryBuilder),
    }
}

/// The handle a migration uses to change the schema. Renders `sea-query`
/// statements for the running backend, with a raw-SQL escape hatch.
pub struct Schema<'c> {
    conn: &'c mut AnyConnection,
    backend: DbBackend,
}

impl Schema<'_> {
    pub fn backend(&self) -> DbBackend {
        self.backend
    }

    /// Runs a `sea-query` schema statement (`Table::create/alter/drop`,
    /// `Index::create`, ...). Takes the statement by value and renders it to SQL
    /// before awaiting, so the (non-`Send`) statement is not held across the
    /// await point.
    pub async fn exec<S: SchemaStatementBuilder>(&mut self, stmt: S) -> CoreResult<()> {
        let sql = schema_sql(self.backend, &stmt);
        drop(stmt);
        sqlx::query(&sql).execute(&mut *self.conn).await?;
        Ok(())
    }

    /// Runs raw SQL, for anything the builder does not express. Portability is
    /// the caller's responsibility.
    pub async fn raw(&mut self, sql: &str) -> CoreResult<()> {
        sqlx::query(sql).execute(&mut *self.conn).await?;
        Ok(())
    }
}

/// One migration: a stable name, an `up`, and an optional `down`.
#[async_trait(?Send)]
pub trait Migration: Send + Sync {
    /// The stable name recorded in the tracking table. Never change it once the
    /// migration has shipped.
    fn name(&self) -> &str;

    /// Applies the migration.
    async fn up(&self, schema: &mut Schema<'_>) -> CoreResult<()>;

    /// Reverses the migration. The default marks it irreversible; the runner
    /// fills in the module id. Override to make a migration reversible.
    async fn down(&self, _schema: &mut Schema<'_>) -> CoreResult<()> {
        Err(CoreError::Irreversible {
            module: String::new(),
            name: self.name().to_string(),
        })
    }
}

/// A migration whose up and down are raw SQL strings. `down` is optional; its
/// absence makes the migration irreversible.
pub struct SqlMigration {
    name: String,
    up: String,
    down: Option<String>,
}

impl SqlMigration {
    pub fn new(name: impl Into<String>, up: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            up: up.into(),
            down: None,
        }
    }

    pub fn reversible(mut self, down: impl Into<String>) -> Self {
        self.down = Some(down.into());
        self
    }
}

#[async_trait(?Send)]
impl Migration for SqlMigration {
    fn name(&self) -> &str {
        &self.name
    }

    async fn up(&self, schema: &mut Schema<'_>) -> CoreResult<()> {
        schema.raw(&self.up).await
    }

    async fn down(&self, schema: &mut Schema<'_>) -> CoreResult<()> {
        match &self.down {
            Some(sql) => schema.raw(sql).await,
            None => Err(CoreError::Irreversible {
                module: String::new(),
                name: self.name.clone(),
            }),
        }
    }
}

/// A module's ordered migrations: the version history for `module_id`.
pub struct MigrationSet {
    pub module_id: &'static str,
    pub migrations: Vec<Box<dyn Migration>>,
}

impl MigrationSet {
    pub fn new(module_id: &'static str, migrations: Vec<Box<dyn Migration>>) -> Self {
        Self {
            module_id,
            migrations,
        }
    }
}

#[derive(Iden)]
enum LateriteMigrations {
    Table,
    ModuleId,
    Name,
}

async fn ensure_tracking_table(pool: &AnyPool, backend: DbBackend) -> CoreResult<()> {
    let stmt = Table::create()
        .table(LateriteMigrations::Table)
        .if_not_exists()
        .col(
            ColumnDef::new(LateriteMigrations::ModuleId)
                .string_len(255)
                .not_null(),
        )
        .col(
            ColumnDef::new(LateriteMigrations::Name)
                .string_len(255)
                .not_null(),
        )
        .primary_key(
            Index::create()
                .col(LateriteMigrations::ModuleId)
                .col(LateriteMigrations::Name),
        )
        .to_owned();
    sqlx::query(&schema_sql(backend, &stmt))
        .execute(pool)
        .await?;
    Ok(())
}

async fn is_applied(
    pool: &AnyPool,
    backend: DbBackend,
    module_id: &str,
    name: &str,
) -> CoreResult<bool> {
    let stmt = Query::select()
        .column(LateriteMigrations::Name)
        .from(LateriteMigrations::Table)
        .and_where(Expr::col(LateriteMigrations::ModuleId).eq(module_id))
        .and_where(Expr::col(LateriteMigrations::Name).eq(name))
        .limit(1)
        .to_owned();
    let found: Option<String> = sqlx::query_scalar(&query_sql(backend, &stmt))
        .fetch_optional(pool)
        .await?;
    Ok(found.is_some())
}

/// The names applied for a module, in application order.
pub async fn applied(
    pool: &AnyPool,
    backend: DbBackend,
    module_id: &str,
) -> CoreResult<Vec<String>> {
    ensure_tracking_table(pool, backend).await?;
    let stmt = Query::select()
        .column(LateriteMigrations::Name)
        .from(LateriteMigrations::Table)
        .and_where(Expr::col(LateriteMigrations::ModuleId).eq(module_id))
        .order_by(LateriteMigrations::Name, sea_query::Order::Asc)
        .to_owned();
    let names: Vec<String> = sqlx::query_scalar(&query_sql(backend, &stmt))
        .fetch_all(pool)
        .await?;
    Ok(names)
}

/// Applies every pending migration across the given sets, in listed order and,
/// within a set, in declared order. Each migration runs in its own transaction.
pub async fn run(pool: &AnyPool, backend: DbBackend, sets: &[MigrationSet]) -> CoreResult<()> {
    ensure_tracking_table(pool, backend).await?;
    for set in sets {
        for migration in &set.migrations {
            if is_applied(pool, backend, set.module_id, migration.name()).await? {
                continue;
            }
            let mut tx = pool.begin().await?;
            {
                let mut schema = Schema {
                    conn: &mut tx,
                    backend,
                };
                migration.up(&mut schema).await?;
            }
            let insert = Query::insert()
                .into_table(LateriteMigrations::Table)
                .columns([LateriteMigrations::ModuleId, LateriteMigrations::Name])
                .values_panic([set.module_id.into(), migration.name().into()])
                .to_owned();
            sqlx::query(&query_sql(backend, &insert))
                .execute(&mut *tx)
                .await?;
            tx.commit().await?;
        }
    }
    Ok(())
}

/// Reverses the last `steps` applied migrations of one module, most recent
/// first. An irreversible migration in the way stops the rollback.
pub async fn rollback(
    pool: &AnyPool,
    backend: DbBackend,
    set: &MigrationSet,
    steps: usize,
) -> CoreResult<()> {
    ensure_tracking_table(pool, backend).await?;
    let mut done = 0;
    for migration in set.migrations.iter().rev() {
        if done >= steps {
            break;
        }
        if !is_applied(pool, backend, set.module_id, migration.name()).await? {
            continue;
        }
        let mut tx = pool.begin().await?;
        {
            let mut schema = Schema {
                conn: &mut tx,
                backend,
            };
            migration.down(&mut schema).await.map_err(|e| match e {
                CoreError::Irreversible { name, .. } => CoreError::Irreversible {
                    module: set.module_id.to_string(),
                    name,
                },
                other => other,
            })?;
        }
        let delete = Query::delete()
            .from_table(LateriteMigrations::Table)
            .and_where(Expr::col(LateriteMigrations::ModuleId).eq(set.module_id))
            .and_where(Expr::col(LateriteMigrations::Name).eq(migration.name()))
            .to_owned();
        sqlx::query(&query_sql(backend, &delete))
            .execute(&mut *tx)
            .await?;
        tx.commit().await?;
        done += 1;
    }
    Ok(())
}

/// Reverses every applied migration of one module.
pub async fn reset(pool: &AnyPool, backend: DbBackend, set: &MigrationSet) -> CoreResult<()> {
    rollback(pool, backend, set, set.migrations.len()).await
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Iden)]
    enum Demo {
        Table,
        Id,
    }

    struct CreateDemo;

    #[async_trait(?Send)]
    impl Migration for CreateDemo {
        fn name(&self) -> &str {
            "0001_create_demo"
        }
        async fn up(&self, schema: &mut Schema<'_>) -> CoreResult<()> {
            schema
                .exec(
                    Table::create()
                        .table(Demo::Table)
                        .if_not_exists()
                        .col(ColumnDef::new(Demo::Id).integer().not_null())
                        .to_owned(),
                )
                .await
        }
        async fn down(&self, schema: &mut Schema<'_>) -> CoreResult<()> {
            schema
                .exec(Table::drop().table(Demo::Table).to_owned())
                .await
        }
    }

    async fn sqlite_pool() -> AnyPool {
        sqlx::any::install_default_drivers();
        sqlx::any::AnyPoolOptions::new()
            .max_connections(1)
            .connect("sqlite::memory:")
            .await
            .unwrap()
    }

    #[tokio::test]
    async fn applies_and_rolls_back_on_sqlite() {
        let pool = sqlite_pool().await;
        let backend = DbBackend::Sqlite;
        let set = MigrationSet::new("test.demo", vec![Box::new(CreateDemo)]);

        run(&pool, backend, std::slice::from_ref(&set))
            .await
            .unwrap();
        // The table exists and rerunning is a no-op.
        run(&pool, backend, std::slice::from_ref(&set))
            .await
            .unwrap();
        sqlx::query("insert into demo (id) values (1)")
            .execute(&pool)
            .await
            .unwrap();
        assert_eq!(applied(&pool, backend, "test.demo").await.unwrap().len(), 1);

        reset(&pool, backend, &set).await.unwrap();
        // The table is gone and the tracking row is cleared.
        assert!(sqlx::query("select count(*) from demo")
            .fetch_one(&pool)
            .await
            .is_err());
        assert!(applied(&pool, backend, "test.demo")
            .await
            .unwrap()
            .is_empty());
    }

    #[tokio::test]
    async fn irreversible_migration_reports_module_and_name() {
        let pool = sqlite_pool().await;
        let backend = DbBackend::Sqlite;
        let set = MigrationSet::new(
            "test.oneway",
            vec![Box::new(SqlMigration::new(
                "0001_make_t",
                "create table t (id integer not null)",
            ))],
        );
        run(&pool, backend, std::slice::from_ref(&set))
            .await
            .unwrap();
        let err = rollback(&pool, backend, &set, 1).await.unwrap_err();
        match err {
            CoreError::Irreversible { module, name } => {
                assert_eq!(module, "test.oneway");
                assert_eq!(name, "0001_make_t");
            }
            other => panic!("expected Irreversible, got {other:?}"),
        }
    }
}