From c6b1435875594ad568116da1c6ef1b27cb2f2ffe Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Fri, 21 Apr 2017 14:50:34 +0200 Subject: [PATCH] Add goose database migrations --- .gitignore | 2 +- database.sql | 31 ---- db/dbconf.yml | 6 + .../20170421134856_0.1.0_original.sql | 15 ++ .../20170421143052_fix_duplicates.sql | 15 ++ .../20170421143114_add_constraints.sql | 169 ++++++++++++++++++ 6 files changed, 206 insertions(+), 32 deletions(-) delete mode 100644 database.sql create mode 100644 db/dbconf.yml create mode 100644 db/migrations/20170421134856_0.1.0_original.sql create mode 100644 db/migrations/20170421143052_fix_duplicates.sql create mode 100644 db/migrations/20170421143114_add_constraints.sql diff --git a/.gitignore b/.gitignore index 79c64c5..848c586 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ *.crt *.key *.pem +*.sqlite .*.swp .idea/ cacert-boardvoting config.yaml -database.sqlite diff --git a/database.sql b/database.sql deleted file mode 100644 index 9c4bb34..0000000 --- a/database.sql +++ /dev/null @@ -1,31 +0,0 @@ -CREATE TABLE decisions ( - id INTEGER PRIMARY KEY, - proposed DATETIME, - proponent INTEGER, - title VARCHAR(255), - content TEXT, - quorum INTEGER, - majority INTEGER, - status INTEGER, - due DATETIME, - modified DATETIME, - tag VARCHAR(255), - votetype INT4 DEFAULT 0 NOT NULL -); -CREATE TABLE emails ( - voter INT4, - address VARCHAR(255) -); -CREATE TABLE voters ( - id INTEGER PRIMARY KEY, - name VARCHAR(255), - enabled INTEGER DEFAULT 0, - reminder VARCHAR(255) -); -CREATE TABLE votes ( - decision INT4, - voter INT4, - vote INT4, - voted DATETIME, - notes TEXT DEFAULT '' -); diff --git a/db/dbconf.yml b/db/dbconf.yml new file mode 100644 index 0000000..002a871 --- /dev/null +++ b/db/dbconf.yml @@ -0,0 +1,6 @@ +development: + driver: sqlite3 + open: database.sqlite +production: + driver: sqlite3 + open: boardvoting.sqlite diff --git a/db/migrations/20170421134856_0.1.0_original.sql b/db/migrations/20170421134856_0.1.0_original.sql new file mode 100644 index 0000000..ae32f38 --- /dev/null +++ b/db/migrations/20170421134856_0.1.0_original.sql @@ -0,0 +1,15 @@ + +-- +goose Up +-- SQL in section 'Up' is executed when this migration is applied +CREATE TABLE IF NOT EXISTS votes (decision INT4, voter INT4, vote INT4, voted DATETIME, notes text default ''); +CREATE TABLE IF NOT EXISTS emails (voter INT4, address VARCHAR(255)); +CREATE TABLE IF NOT EXISTS voters (id INTEGER PRIMARY KEY, name VARCHAR(255), enabled INTEGER default 0, reminder VARCHAR(255)); +CREATE TABLE IF NOT EXISTS decisions (id INTEGER PRIMARY KEY, proposed DATETIME, proponent INTEGER, title VARCHAR(255), content TEXT, quorum INTEGER, majority INTEGER, status INTEGER, due DATETIME, modified DATETIME, tag varchar(255), votetype INT4 DEFAULT 0 NOT NULL); + + +-- +goose Down +-- SQL section 'Down' is executed when this migration is rolled back +DROP TABLE votes; +DROP TABLE decisions; +DROP TABLE emails; +DROP TABLE voters; diff --git a/db/migrations/20170421143052_fix_duplicates.sql b/db/migrations/20170421143052_fix_duplicates.sql new file mode 100644 index 0000000..a0ffbf0 --- /dev/null +++ b/db/migrations/20170421143052_fix_duplicates.sql @@ -0,0 +1,15 @@ + +-- +goose Up +-- SQL in section 'Up' is executed when this migration is applied + +-- Peter Yuill has a duplicate entry in the voters table he has ids 17 and 31 + +UPDATE votes SET voter=17 WHERE voter=31; +UPDATE decisions SET proponent=17 WHERE proponent=31; +DELETE FROM emails WHERE voter=31; +DELETE FROM voters WHERE id=31; + +-- +goose Down +-- SQL section 'Down' is executed when this migration is rolled back + +-- There is no useful backward migration \ No newline at end of file diff --git a/db/migrations/20170421143114_add_constraints.sql b/db/migrations/20170421143114_add_constraints.sql new file mode 100644 index 0000000..c1ddfbe --- /dev/null +++ b/db/migrations/20170421143114_add_constraints.sql @@ -0,0 +1,169 @@ +-- +goose Up +-- SQL in section 'Up' is executed when this migration is applied +CREATE TABLE voters_new ( + id INTEGER PRIMARY KEY, + name VARCHAR(255) NOT NULL, + enabled BOOLEAN NOT NULL, + reminder VARCHAR(255) +); +INSERT INTO voters_new (id, name, enabled, reminder) + SELECT + id, + name, + enabled, + reminder + FROM voters; +DROP TABLE voters; +ALTER TABLE voters_new + RENAME TO voters; + +CREATE TABLE emails_new ( + voter INTEGER NOT NULL REFERENCES voters (id), + address VARCHAR(255) UNIQUE NOT NULL +); +INSERT INTO emails_new (voter, address) + SELECT + voter, + address + FROM emails; +DROP TABLE emails; +ALTER TABLE emails_new + RENAME TO emails; + +CREATE TABLE decisions_new ( + id INTEGER PRIMARY KEY, + proposed DATETIME NOT NULL, + proponent INTEGER NOT NULL REFERENCES voters (id), + title VARCHAR(255) NOT NULL, + content TEXT NOT NULL, + status INTEGER NOT NULL CHECK (status IN (-2, -1, 0, 1)), + due DATETIME NOT NULL, + modified DATETIME NOT NULL, + tag VARCHAR(255) UNIQUE NOT NULL, + votetype INTEGER DEFAULT 0 NOT NULL CHECK (votetype IN (0, 1)) +); +INSERT INTO decisions_new ( + id, proposed, proponent, title, content, status, due, modified, tag, votetype +) + SELECT + id, + proposed, + proponent, + title, + content, + status, + due, + modified, + tag, + votetype + FROM decisions; +DROP TABLE decisions; +ALTER TABLE decisions_new + RENAME TO decisions; +CREATE INDEX decisions_proposed_idx + ON decisions (proposed); + +CREATE TABLE votes_new ( + decision INTEGER REFERENCES decisions (id), + voter INTEGER REFERENCES voters (id), + vote INTEGER NOT NULL CHECK (vote IN (-1, 0, 1)), + voted DATETIME NOT NULL, + notes TEXT NOT NULL DEFAULT '', + PRIMARY KEY (decision, voter) +); +INSERT INTO votes_new (decision, voter, vote, voted, notes) + SELECT + decision, + voter, + vote, + voted, + notes + FROM votes; +DROP TABLE votes; +ALTER TABLE votes_new + RENAME TO votes; + +-- +goose Down +-- SQL section 'Down' is executed when this migration is rolled back + +CREATE TABLE votes_orig ( + decision INT4, + voter INT4, + vote INT4, + voted DATETIME, + notes TEXT DEFAULT '' +); +INSERT INTO votes_orig (decision, voter, vote, voted, notes) + SELECT + decision, + voter, + vote, + voted, + notes + FROM votes; +DROP TABLE votes; +ALTER TABLE votes_orig + RENAME TO votes; + +CREATE TABLE decisions_orig ( + id INTEGER PRIMARY KEY, + proposed DATETIME, + proponent INTEGER, + title VARCHAR(255), + content TEXT, + quorum INTEGER, + majority INTEGER, + status INTEGER, + due DATETIME, + modified DATETIME, + tag VARCHAR(255), + votetype INT4 DEFAULT 0 NOT NULL +); +INSERT INTO decisions_orig (id, proposed, proponent, title, content, status, due, modified, tag, votetype) + SELECT + id, + proposed, + proponent, + title, + content, + status, + due, + modified, + tag, + votetype + FROM + decisions; +DROP INDEX decisions_proposed_idx; +DROP TABLE decisions; +ALTER TABLE decisions_orig + RENAME TO decisions; + +CREATE TABLE emails_orig ( + voter INT4, + address VARCHAR(255) +); +INSERT INTO emails_orig (voter, address) + SELECT + voter, + address + FROM emails; +DROP TABLE emails; +ALTER TABLE emails_orig + RENAME TO emails; + +CREATE TABLE voters_orig ( + id INTEGER PRIMARY KEY, + name VARCHAR(255), + enabled INTEGER DEFAULT 0, + reminder VARCHAR(255) +); +INSERT INTO voters_orig (id, name, enabled, reminder) + SELECT + id, + name, + enabled, + reminder + FROM voters; +DROP TABLE voters; +ALTER TABLE voters_orig + RENAME TO voters; \ No newline at end of file