From 5cdbac40b1960536d7ad599089ccfd06aa816e10 Mon Sep 17 00:00:00 2001 From: Taehoon Moon Date: Mon, 2 Dec 2024 12:50:43 +0900 Subject: [PATCH] Add Meta table to core/ schema - store schema_version (#81) --- core/src/schema.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/core/src/schema.rs b/core/src/schema.rs index ecc67bd..518abba 100644 --- a/core/src/schema.rs +++ b/core/src/schema.rs @@ -9,6 +9,14 @@ use { }; pub async fn setup(storage: &mut Storage) -> Result { + table("Meta") + .create_table_if_not_exists() + .add_column("key TEXT PRIMARY KEY") + .add_column("value TEXT NOT NULL") + .add_column("updated_at TIMESTAMP NOT NULL DEFAULT NOW()") + .execute(storage) + .await?; + table("Directory") .create_table_if_not_exists() .add_column("id UUID PRIMARY KEY DEFAULT GENERATE_UUID()") @@ -30,6 +38,26 @@ pub async fn setup(storage: &mut Storage) -> Result { .execute(storage) .await?; + let schema_version_not_exists = table("Meta") + .select() + .filter(col("key").eq(text("schema_version"))) + .project("key") + .execute(storage) + .await? + .select() + .unwrap() + .count() + == 0; + + if schema_version_not_exists { + table("Meta") + .insert() + .columns(vec!["key", "value"]) + .values(vec![vec![text("schema_version"), text("1")]]) + .execute(storage) + .await?; + } + let root_not_exists = table("Directory") .select() .filter(col("parent_id").is_null())