Automatically create control table if it doesn't exists

This commit is contained in:
Rikard Bartholf
2016-02-17 15:11:11 +01:00
parent 375c521e17
commit 2c5c2d2034
2 changed files with 11 additions and 11 deletions
-11
View File
@@ -58,17 +58,6 @@ alter table users
add email varchar(256) not null; add email varchar(256) not null;
``` ```
#### Prerequisites
One table needs to be created for the database to make this work:
```
CREATE TABLE database_versions (
version varchar(32) not null,
is_active boolean not null default false,
creation_date timestamp not null default current_timestamp
);
```
To do the actual migration, then just run: To do the actual migration, then just run:
``` ```
# ./db-upgrade [ENVIRONMENT NAME] # ./db-upgrade [ENVIRONMENT NAME]
+11
View File
@@ -8,6 +8,17 @@ fi
source env/$DB_ENVIRONMENT.conf source env/$DB_ENVIRONMENT.conf
# Create the control table, if it doesn't exist
cat <<-EOF | psql $PSQL_OPTS -d $DB_NAME
BEGIN;
CREATE TABLE IF NOT EXISTS database_versions (
version varchar(32) not null,
is_active boolean not null default false,
creation_date timestamp not null default current_timestamp
);
END;
EOF
function add-version() { function add-version() {
psql $PSQL_OPTS -c "update database_versions set is_active = false where is_active = true" $DB_NAME psql $PSQL_OPTS -c "update database_versions set is_active = false where is_active = true" $DB_NAME
psql $PSQL_OPTS -c "insert into database_versions (version, is_active) values ('$1', TRUE)" $DB_NAME psql $PSQL_OPTS -c "insert into database_versions (version, is_active) values ('$1', TRUE)" $DB_NAME