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.
This commit is contained in:
fabianlindfors 2022-01-13 23:56:06 +01:00
parent 7c48974308
commit b1b79e26e5

View File

@ -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,
}
}