on main help sign in
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.

commitb1b79e26e55e951327557ae4432abb32b0cdc106 authorfabianlindfors <fabian@flapplabs.se> dateThu 13 Jan 2022, 23:56 (5 years ago) parent7c48974
1 file changed+15−2
src/state.rs+15−2
src/state.rs+15−2
@@ -6,7 +6,6 @@ use version::version;
66
77#[derive(Serialize, Deserialize, Debug)]
88pub struct State {
9− pub version: String,
109 pub status: Status,
1110}
1211
@@ -139,9 +138,12 @@ impl State {
139138 fn ensure_schema_and_table(db: &mut impl Conn) {
140139 db.run("CREATE SCHEMA IF NOT EXISTS reshape").unwrap();
141140
141+ // Create data table which will be a key-value table containing
142+ // the version and current state.
142143 db.run("CREATE TABLE IF NOT EXISTS reshape.data (key TEXT PRIMARY KEY, value JSONB)")
143144 .unwrap();
144145
146+ // Create migrations table which will store all completed migrations
145147 db.run(
146148 "
147149 CREATE TABLE IF NOT EXISTS reshape.migrations (
@@ -154,13 +156,24 @@ impl State {
154156 ",
155157 )
156158 .unwrap();
159+
160+ // Update the current version
161+ let encoded_version = serde_json::to_value(version!().to_string()).unwrap();
162+ db.query_with_params(
163+ "
164+ INSERT INTO reshape.data (key, value)
165+ VALUES ('version', $1)
166+ ON CONFLICT (key) DO UPDATE SET value = $1
167+ ",
168+ &[&encoded_version],
169+ )
170+ .unwrap();
157171 }
158172}
159173
160174impl Default for State {
161175 fn default() -> Self {
162176 State {
163− version: version!().to_string(),
164177 status: Status::Idle,
165178 }
166179 }