Guest User

Untitled

a guest
Oct 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. create table if not exists migrations (
  2. key text CONSTRAINT pkey PRIMARY KEY
  3. );
  4.  
  5. create or replace function idempotent(migration_name text,code text) returns void as $$
  6. begin
  7. if exists (select key from migrations where key=migration_name) then
  8. raise notice 'Migration already applied: %', migration_name;
  9. else
  10. raise notice 'Running migration: %', migration_name;
  11. execute code;
  12. insert into migrations (key) VALUES (migration_name);
  13. end if;
  14. end;
  15. $$ language plpgsql strict;
  16.  
  17. /* USAGE: V0001__orders.sql
  18.  
  19. do $do$ begin perform idempotent('V0001__orders', $$
  20.  
  21. CREATE TABLE orders
  22. (
  23. id bigint NOT NULL,
  24. user_id bigint,
  25. address_id bigint,
  26. product_id bigint)
  27.  
  28. $$); end $do$;
  29. */
Add Comment
Please, Sign In to add comment