From b1b79e26e55e951327557ae4432abb32b0cdc106 Mon Sep 17 00:00:00 2001 From: fabianlindfors Date: Thu, 13 Jan 2022 23:56:06 +0100 Subject: [PATCH] Store version under separate key in reshape.data table The version is not currently in used but will probably be useful later when we want Reshape to be backwards compatbility. Having the version stored in the database will then allow us to perform schema migrations on the metadata of Reshape. --- src/state.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/state.rs b/src/state.rs index bfd2954..e33d3ac 100644 --- a/src/state.rs +++ b/src/state.rs @@ -6,7 +6,6 @@ use version::version; #[derive(Serialize, Deserialize, Debug)] pub struct State { - pub version: String, pub status: Status, } @@ -139,9 +138,12 @@ impl State { fn ensure_schema_and_table(db: &mut impl Conn) { db.run("CREATE SCHEMA IF NOT EXISTS reshape").unwrap(); + // Create data table which will be a key-value table containing + // the version and current state. db.run("CREATE TABLE IF NOT EXISTS reshape.data (key TEXT PRIMARY KEY, value JSONB)") .unwrap(); + // Create migrations table which will store all completed migrations db.run( " CREATE TABLE IF NOT EXISTS reshape.migrations ( @@ -154,13 +156,24 @@ impl State { ", ) .unwrap(); + + // Update the current version + let encoded_version = serde_json::to_value(version!().to_string()).unwrap(); + db.query_with_params( + " + INSERT INTO reshape.data (key, value) + VALUES ('version', $1) + ON CONFLICT (key) DO UPDATE SET value = $1 + ", + &[&encoded_version], + ) + .unwrap(); } } impl Default for State { fn default() -> Self { State { - version: version!().to_string(), status: Status::Idle, } }