Skip to content

Commit 2c204be

Browse files
committed
Move MIGRATIONS list into dedicated migrations module
1 parent 81a3d1e commit 2c204be

File tree

3 files changed

+39
-36
lines changed

3 files changed

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

rust/impls/src/postgres_store.rs

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

0 commit comments

Comments
 (0)