Last active
September 12, 2017 01:51
-
-
Save tjstebbing/bf5e13e65845a3c38bfa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- BASE DB | |
CREATE TABLE IF NOT EXISTS "db_ver" ( | |
version integer unique not null, | |
updated timestamp WITH time zone DEFAULT now() | |
); | |
INSERT INTO db_ver (version) VALUES (1); -- init version 1 | |
-- Migrations are named 1.sql, 2.sql etc. | |
DO $migration$ | |
DECLARE | |
from_version int; | |
to_version int; | |
BEGIN | |
from_version := 1; -- declare the version being migrated from | |
to_version := 2; -- declare the version being migrated to | |
IF (SELECT MAX(version) from db_ver) = from_version THEN | |
-- MIGRATION CODE BEGINS | |
-- stuff goes here! | |
-- MIGRATION CODE ENDS | |
-- UPDATE DB VERSION | |
INSERT INTO db_ver (version) VALUES (to_version); | |
END IF; | |
END $migration$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for FILE in
ls ./migrations/* | sort -g
; do psql ..... ; done