Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create table if not exists migrations (
- key text CONSTRAINT pkey PRIMARY KEY
- );
- create or replace function idempotent(migration_name text,code text) returns void as $$
- begin
- if exists (select key from migrations where key=migration_name) then
- raise notice 'Migration already applied: %', migration_name;
- else
- raise notice 'Running migration: %', migration_name;
- execute code;
- insert into migrations (key) VALUES (migration_name);
- end if;
- end;
- $$ language plpgsql strict;
- /* USAGE: V0001__orders.sql
- do $do$ begin perform idempotent('V0001__orders', $$
- CREATE TABLE orders
- (
- id bigint NOT NULL,
- user_id bigint,
- address_id bigint,
- product_id bigint)
- $$); end $do$;
- */
Add Comment
Please, Sign In to add comment