-
Notifications
You must be signed in to change notification settings - Fork 2
/
migrations.sql
69 lines (62 loc) · 2.58 KB
/
migrations.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
-- This file provides a method for applying incremental schema changes
-- to a PostgreSQL database.
-- Add your migrations at the end of the file, and run "psql -v ON_ERROR_STOP=1 -1f
-- migrations.sql yourdbname" to apply all pending migrations. The
-- "-1" causes all the changes to be applied atomically
-- Most Rails (ie. ActiveRecord) migrations are run by a user with
-- full read-write access to both the schema and its contents, which
-- isn't ideal. You'd generally run this file as a database owner, and
-- the contained migrations would grant access to less-privileged
-- application-level users as appropriate.
-- Refer to https://github.com/purcell/postgresql-migrations for info and updates
--------------------------------------------------------------------------------
-- A function that will apply an individual migration
--------------------------------------------------------------------------------
DO
$body$
BEGIN
IF NOT EXISTS (SELECT FROM pg_catalog.pg_proc WHERE proname = 'apply_migration') THEN
CREATE FUNCTION apply_migration (migration_name TEXT, ddl TEXT) RETURNS BOOLEAN
AS $$
BEGIN
IF NOT EXISTS (SELECT FROM pg_catalog.pg_tables WHERE tablename = 'applied_migrations') THEN
CREATE TABLE applied_migrations (
identifier TEXT NOT NULL PRIMARY KEY
, ddl TEXT NOT NULL
, applied_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);
END IF;
LOCK TABLE applied_migrations IN EXCLUSIVE MODE;
IF NOT EXISTS (SELECT 1 FROM applied_migrations m WHERE m.identifier = migration_name)
THEN
RAISE NOTICE 'Applying migration: %', migration_name;
EXECUTE ddl;
INSERT INTO applied_migrations (identifier, ddl) VALUES (migration_name, ddl);
RETURN TRUE;
END IF;
RETURN FALSE;
END;
$$ LANGUAGE plpgsql;
END IF;
END
$body$;
--------------------------------------------------------------------------------
-- Example migrations follow, commented out
--------------------------------------------------------------------------------
-- -- Give each migration a unique name:
-- SELECT apply_migration('create_things_table',
-- $$
-- -- SQL to apply goes here
-- CREATE TABLE things (
-- name TEXT
-- );
-- $$);
-- -- Add more migrations in the order you'd like them to be applied:
-- SELECT apply_migration('alter_things_table',
-- $$
-- -- You can place not just one statement...
-- ALTER TABLE things ADD number INTEGER;
-- -- ...but multiple in here.
-- ALTER TABLE things ALTER name SET NOT NULL;
-- -- All statements will be run in a transaction.
-- $$);