on main help sign in
Store version under separate key in reshape.data table
b1b79e2fabianlindfors
src/state.rs+15−2
@@ -6,7 +6,6 @@ use version::version;
6
6
7#[derive(Serialize, Deserialize, Debug)]
7#[derive(Serialize, Deserialize, Debug)]
8pub struct State {
8pub struct State {
9− pub version: String,
10 pub status: Status,
9 pub status: Status,
11}
10}
12
11
@@ -139,9 +138,12 @@ impl State {
139 fn ensure_schema_and_table(db: &mut impl Conn) {
138 fn ensure_schema_and_table(db: &mut impl Conn) {
140 db.run("CREATE SCHEMA IF NOT EXISTS reshape").unwrap();
139 db.run("CREATE SCHEMA IF NOT EXISTS reshape").unwrap();
141
140
141+ // Create data table which will be a key-value table containing
142+ // the version and current state.
142 db.run("CREATE TABLE IF NOT EXISTS reshape.data (key TEXT PRIMARY KEY, value JSONB)")
143 db.run("CREATE TABLE IF NOT EXISTS reshape.data (key TEXT PRIMARY KEY, value JSONB)")
143 .unwrap();
144 .unwrap();
144
145
146+ // Create migrations table which will store all completed migrations
145 db.run(
147 db.run(
146 "
148 "
147 CREATE TABLE IF NOT EXISTS reshape.migrations (
149 CREATE TABLE IF NOT EXISTS reshape.migrations (
@@ -154,13 +156,24 @@ impl State {
154 ",
156 ",
155 )
157 )
156 .unwrap();
158 .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();
157 }
171 }
158}
172}
159
173
160impl Default for State {
174impl Default for State {
161 fn default() -> Self {
175 fn default() -> Self {
162 State {
176 State {
163− version: version!().to_string(),
164 status: Status::Idle,
177 status: Status::Idle,
165 }
178 }
166 }
179 }