-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmigrate.sql
More file actions
61 lines (49 loc) · 1.38 KB
/
migrate.sql
File metadata and controls
61 lines (49 loc) · 1.38 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
-- Factory+ / AMRC Connectivity Stack (ACS) Config Store component
-- Database migration script.
-- Copyright 2022 AMRC.
--
-- This script needs to be run as a database superuser. It creates the
-- database if it doesn't exist, so start it connected to the
-- 'postgres' database.
\set ON_ERROR_STOP
-- Read our config from the environment
\set db `echo $SRV_DATABASE`
\set role `echo $SRV_USER`
\echo Starting migration...
-- Check what exists already (\gset selects into psql variables).
select
exists(select 1 from pg_database where datname = :'db') has_db,
exists(select 1 from pg_roles where rolname = :'role') has_role
\gset
-- Create and connect to the database.
\if :has_db
\else
\echo Creating database :"db"
create database :"db" allow_connections false;
revoke all on database :"db" from public;
alter database :"db" allow_connections true;
\endif
\c :"db"
BEGIN;
-- If the role exists, drop all its permissions. This will also drop any
-- objects owned by that user, but there should be none.
\if :has_role
\echo Dropping existing permissions for :"role"
drop owned by :"role";
\else
\echo Creating database role :"role"
create user :"role";
\endif
\ir migration.sql
-- Version 6 was the first to use this setup.
\ir v6.sql
\ir v7.sql
\ir v8.sql
\ir v9.sql
\ir v10.sql
\ir v11.sql
\ir v12.sql
\ir v13.sql
\ir grant.sql
COMMIT;
\echo Migration complete.