Skip to content

Instantly share code, notes, and snippets.

@tomaszalusky
Last active September 3, 2019 12:39
Show Gist options
  • Save tomaszalusky/4b953c678c806408025d05d984d30ed3 to your computer and use it in GitHub Desktop.
Save tomaszalusky/4b953c678c806408025d05d984d30ed3 to your computer and use it in GitHub Desktop.
# docker build -t tomaszalusky/trig-example .
# docker run -d --name trig-example tomaszalusky/trig-example
# docker exec -it trig-example /bin/bash psql -U postgres
FROM postgres:9.6
WORKDIR /docker-entrypoint-initdb.d
RUN apt-get update && \
apt-get install vim -y && \
echo "create table detail (id bigint not null, code varchar(255) not null, primary key (id));" >> createdb.sql && \
echo "create table master (id bigint not null, name varchar(255), detail_id bigint, primary key (id), unique (detail_id), foreign key (detail_id) references detail(id));" >> createdb.sql && \
echo "create table detail_candidate (master_id bigint not null, detail_id bigint not null, primary key (master_id, detail_id), foreign key (detail_id) references detail(id), foreign key (master_id) references master(id));" >> createdb.sql && \
echo "create or replace function trgf() returns trigger as \$\$ begin return NEW; end; \$\$ language plpgsql;" >> createdb.sql && \
echo "create trigger trg before insert or update on master for each row execute procedure trgf();" >> createdb.sql && \
echo "create extension pgrowlocks;" >> createdb.sql && \
echo "insert into master (id, name) values (1000, 'x');" >> createdb.sql && \
echo "insert into detail (code, id) values ('a', 1);" >> createdb.sql
WORKDIR /
RUN echo "rollback;" >> cleanup.sql && \
echo "delete from detail_candidate;" >> cleanup.sql && \
echo "delete from master;" >> cleanup.sql && \
echo "delete from detail;" >> cleanup.sql && \
echo "insert into master (id, name) values (1000, 'x');" >> cleanup.sql && \
echo "insert into detail (code, id) values ('a', 1);" >> cleanup.sql
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["postgres"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment