Skip to content

Instantly share code, notes, and snippets.

@docteurklein
Last active January 14, 2020 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save docteurklein/27decb80abdeefe13f76a47564d0cb29 to your computer and use it in GitHub Desktop.
Save docteurklein/27decb80abdeefe13f76a47564d0cb29 to your computer and use it in GitHub Desktop.
psql -v ON_ERROR_STOP=1 -c begin $(find . -name '*.sql' -printf ' -f %f\n' | sort -V | xargs) -c commit
create table if not exists migration (
id bigint primary key,
at timestamptz not null default clock_timestamp()
);
select not exists(select true from migration where id = 1) as should_v1 \gset
\if :should_v1
create table test (id int);
insert into migration (id) values (1);
\endif
select not exists(select true from migration where id = 2) as should_v2 \gset
\if :should_v2
alter table test add col2 text;
insert into migration (id) values (2);
\endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment