From 2c5c2d203462f69e159ddd96428d6198673647c4 Mon Sep 17 00:00:00 2001 From: Rikard Bartholf Date: Wed, 17 Feb 2016 15:11:11 +0100 Subject: [PATCH] Automatically create control table if it doesn't exists --- README.md | 11 ----------- db-upgrade | 11 +++++++++++ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e9dce6b..551d25c 100644 --- a/README.md +++ b/README.md @@ -58,17 +58,6 @@ alter table users 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: ``` # ./db-upgrade [ENVIRONMENT NAME] diff --git a/db-upgrade b/db-upgrade index c88a855..b3deb1b 100755 --- a/db-upgrade +++ b/db-upgrade @@ -8,6 +8,17 @@ fi 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() { 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