Skip to content

Instantly share code, notes, and snippets.

@docteurklein
Created June 9, 2020 09:11
Show Gist options
  • Save docteurklein/6eac7e8e31af97005a5437c17edf9ee6 to your computer and use it in GitHub Desktop.
Save docteurklein/6eac7e8e31af97005a5437c17edf9ee6 to your computer and use it in GitHub Desktop.

declarative DDL

what?

Define your database schema in a declarative way, by providing the wanted state, and this tool will calculate a diff to reconcile it with reality.

how?

Let's imagine a scenario:

  • we start with an empty database
  • edit the schema.sql a first time
  • apply the changes
  • edit the schema.sql a second time
  • apply the changes

first

  • given an empty database
  • given this schema:
table test1 (
    id int,
    col1 int,
    col2 int,
);

test.table test2 (id int);
test.table test3 (id int);
  • then the resulting diff is:
create table test1 (
    id int,
    col1 int,
    col2 int,
);

create schema test;

create table test.test2 (id int);
create table test.test3 (id int);

second

  • given the database is as stated in first step
  • given this new schema:
table test1 (
    id int,
    col3 int rename of col2,
    col4 int inital 0 on add (col3::text),
);

test.table test2 (id int);
test.table test4 (id int);
  • then the resulting diff is:
alter table test1 drop col1;
alter table test1 rename col2 to col3;
alter table test1 add col4 default 0;
alter table test1 alter col4 drop default;
update test1 set col4 = col3::text;
drop table test.test3;
create table test.test4 (id int);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment