Skip to content

Commit 95f2009

Browse files
committed
Move MIGRATIONS list into dedicated migrations module
1 parent 296ee43 commit 95f2009

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

rust/impls/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![deny(rustdoc::private_intra_doc_links)]
1212
#![deny(missing_docs)]
1313

14+
mod migrations;
1415
/// Contains [PostgreSQL](https://www.postgresql.org/) based backend implementation for VSS.
1516
pub mod postgres_store;
1617

rust/impls/src/migrations.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
pub(crate) const DB_VERSION_COLUMN: &str = "db_version";
2+
#[cfg(test)]
3+
pub(crate) const MIGRATION_LOG_COLUMN: &str = "upgrade_from";
4+
5+
pub(crate) const CHECK_DB_STMT: &str = "SELECT 1 FROM pg_database WHERE datname = $1";
6+
pub(crate) const INIT_DB_CMD: &str = "CREATE DATABASE";
7+
#[cfg(test)]
8+
const DROP_DB_CMD: &str = "DROP DATABASE";
9+
pub(crate) const GET_VERSION_STMT: &str = "SELECT db_version FROM vss_db_version;";
10+
pub(crate) const UPDATE_VERSION_STMT: &str = "UPDATE vss_db_version SET db_version=$1;";
11+
pub(crate) const LOG_MIGRATION_STMT: &str = "INSERT INTO vss_db_upgrades VALUES($1);";
12+
#[cfg(test)]
13+
pub(crate) const GET_MIGRATION_LOG_STMT: &str = "SELECT upgrade_from FROM vss_db_upgrades;";
14+
15+
// APPEND-ONLY list of migration statements
16+
//
17+
// Each statement MUST be applied in-order, and only once per database.
18+
//
19+
// We make an exception for the vss_db table creation statement, as users of VSS could have initialized the table
20+
// themselves.
21+
pub(crate) const MIGRATIONS: &[&str] = &[
22+
"CREATE TABLE vss_db_version (db_version INTEGER);",
23+
"INSERT INTO vss_db_version VALUES(1);",
24+
// A write-only log of all the migrations performed on this database, useful for debugging and testing
25+
"CREATE TABLE vss_db_upgrades (upgrade_from INTEGER);",
26+
// We do not complain if the table already exists, as users of VSS could have already created this table
27+
"CREATE TABLE IF NOT EXISTS vss_db (
28+
user_token character varying(120) NOT NULL CHECK (user_token <> ''),
29+
store_id character varying(120) NOT NULL CHECK (store_id <> ''),
30+
key character varying(600) NOT NULL,
31+
value bytea NULL,
32+
version bigint NOT NULL,
33+
created_at TIMESTAMP WITH TIME ZONE,
34+
last_updated_at TIMESTAMP WITH TIME ZONE,
35+
PRIMARY KEY (user_token, store_id, key)
36+
);",
37+
];
38+
#[cfg(test)]
39+
const DUMMY_MIGRATION: &str = "SELECT 1 WHERE FALSE;";

rust/impls/src/postgres_store.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::migrations::*;
2+
13
use api::error::VssError;
24
use api::kv_store::{KvStore, GLOBAL_VERSION_KEY, INITIAL_RECORD_VERSION};
35
use api::types::{
@@ -27,46 +29,6 @@ const KEY_COLUMN: &str = "key";
2729
const VALUE_COLUMN: &str = "value";
2830
const VERSION_COLUMN: &str = "version";
2931

30-
const DB_VERSION_COLUMN: &str = "db_version";
31-
#[cfg(test)]
32-
const MIGRATION_LOG_COLUMN: &str = "upgrade_from";
33-
34-
const CHECK_DB_STMT: &str = "SELECT 1 FROM pg_database WHERE datname = $1";
35-
const INIT_DB_CMD: &str = "CREATE DATABASE";
36-
#[cfg(test)]
37-
const DROP_DB_CMD: &str = "DROP DATABASE";
38-
const GET_VERSION_STMT: &str = "SELECT db_version FROM vss_db_version;";
39-
const UPDATE_VERSION_STMT: &str = "UPDATE vss_db_version SET db_version=$1;";
40-
const LOG_MIGRATION_STMT: &str = "INSERT INTO vss_db_upgrades VALUES($1);";
41-
#[cfg(test)]
42-
const GET_MIGRATION_LOG_STMT: &str = "SELECT upgrade_from FROM vss_db_upgrades;";
43-
44-
// APPEND-ONLY list of migration statements
45-
//
46-
// Each statement MUST be applied in-order, and only once per database.
47-
//
48-
// We make an exception for the vss_db table creation statement, as users of VSS could have initialized the table
49-
// themselves.
50-
const MIGRATIONS: &[&str] = &[
51-
"CREATE TABLE vss_db_version (db_version INTEGER);",
52-
"INSERT INTO vss_db_version VALUES(1);",
53-
// A write-only log of all the migrations performed on this database, useful for debugging and testing
54-
"CREATE TABLE vss_db_upgrades (upgrade_from INTEGER);",
55-
// We do not complain if the table already exists, as users of VSS could have already created this table
56-
"CREATE TABLE IF NOT EXISTS vss_db (
57-
user_token character varying(120) NOT NULL CHECK (user_token <> ''),
58-
store_id character varying(120) NOT NULL CHECK (store_id <> ''),
59-
key character varying(600) NOT NULL,
60-
value bytea NULL,
61-
version bigint NOT NULL,
62-
created_at TIMESTAMP WITH TIME ZONE,
63-
last_updated_at TIMESTAMP WITH TIME ZONE,
64-
PRIMARY KEY (user_token, store_id, key)
65-
);",
66-
];
67-
#[cfg(test)]
68-
const DUMMY_MIGRATION: &str = "SELECT 1 WHERE FALSE;";
69-
7032
/// The maximum number of key versions that can be returned in a single page.
7133
///
7234
/// This constant helps control memory and bandwidth usage for list operations,

0 commit comments

Comments
 (0)