Forked from pesterhazy/minimalist-migration-framerwork.sql
Created
September 24, 2024 10:40
-
-
Save dejanr/78517d81de148a3d018c4a0d7e3d9428 to your computer and use it in GitHub Desktop.
Minimalist migration framework for PostgreSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment