Skip to main content

laterite_admin/settings/
migrations.rs

1//! The settings schema, as a portable migration.
2
3use laterite_core::strata::*;
4
5use super::store::Settings;
6
7/// The stable migration namespace for the settings store.
8pub const MODULE_ID: &str = "laterite.settings";
9
10/// The settings migrations, for registration with the application's runner.
11pub fn migrations() -> MigrationSet {
12    MigrationSet::new(MODULE_ID, vec![Box::new(CreateSettings)])
13}
14
15struct CreateSettings;
16
17#[async_trait(?Send)]
18impl Migration for CreateSettings {
19    fn name(&self) -> &str {
20        "0001_create_settings"
21    }
22    async fn up(&self, s: &mut Schema<'_>) -> CoreResult<()> {
23        // `value` holds a settings model serialised as JSON text (portable JSON),
24        // keyed by a stable code.
25        s.exec(
26            Table::create()
27                .table(Settings::Table)
28                .if_not_exists()
29                .col(key_col(Settings::Code).not_null().primary_key())
30                .col(
31                    ColumnDef::new(Settings::Value)
32                        .text()
33                        .not_null()
34                        .default("{}"),
35                )
36                .col(ColumnDef::new(Settings::UpdatedAt).text().not_null())
37                .to_owned(),
38        )
39        .await
40    }
41    async fn down(&self, s: &mut Schema<'_>) -> CoreResult<()> {
42        s.exec(Table::drop().table(Settings::Table).to_owned())
43            .await
44    }
45}