-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrations.sql
More file actions
91 lines (79 loc) · 2.96 KB
/
migrations.sql
File metadata and controls
91 lines (79 loc) · 2.96 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
-- 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$;
--------------------------------------------------------------------------------
-- Migrations
--------------------------------------------------------------------------------
SELECT apply_migration('create_users_table',
$$
CREATE EXTENSION citext;
CREATE DOMAIN email AS citext
CHECK ( value ~ '^[a-zA-Z0-9.!#$%&''*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$' );
CREATE DOMAIN username AS citext
CHECK ( value ~ '^[a-zA-Z0-9_]{3,15}$' );
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username username unique,
email email,
password text,
created_at timestamp,
updated_at timestamp
);
$$);
SELECT apply_migration('create_posts_table',
$$
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
content varchar(500),
created_at timestamp,
updated_at timestamp
);
$$);
SELECT apply_migration('add_post_length_constraint',
$$
delete from posts where length(content) = 0;
ALTER TABLE
posts
ADD CONSTRAINT
post_length_ck CHECK (LENGTH(content) >= 1);
$$);