@@ -41,12 +41,18 @@ const LOG_MIGRATION_STMT: &str = "INSERT INTO vss_db_upgrades VALUES($1);";
4141#[ cfg( test) ]
4242const GET_MIGRATION_LOG_STMT : & str = "SELECT upgrade_from FROM vss_db_upgrades;" ;
4343
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.
4450const MIGRATIONS : & [ & str ] = & [
4551 "CREATE TABLE vss_db_version (db_version INTEGER);" ,
4652 "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
4754 "CREATE TABLE vss_db_upgrades (upgrade_from INTEGER);" ,
48- // We do not complain if the table already exists, as a previous version of VSS could have already created
49- // this table
55+ // We do not complain if the table already exists, as users of VSS could have already created this table
5056 "CREATE TABLE IF NOT EXISTS vss_db (
5157 user_token character varying(120) NOT NULL CHECK (user_token <> ''),
5258 store_id character varying(120) NOT NULL CHECK (store_id <> ''),
@@ -100,7 +106,7 @@ async fn initialize_vss_database(postgres_endpoint: &str, db_name: &str) -> Resu
100106 } ) ?;
101107
102108 if num_rows == 0 {
103- let stmt = format ! ( "{} {}" , INIT_DB_CMD , db_name) ;
109+ let stmt = format ! ( "{} {}; " , INIT_DB_CMD , db_name) ;
104110 client. execute ( & stmt, & [ ] ) . await . map_err ( |e| {
105111 Error :: new ( ErrorKind :: Other , format ! ( "Failed to create database {}: {}" , db_name, e) )
106112 } ) ?;
0 commit comments