-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.mk
73 lines (58 loc) · 1.5 KB
/
database.mk
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
TIME != date +'%Y-%m-%d--%T'
DUMP = ${DB_NAME}-${TIME}-${env}.dump
SQL_FILES != cat sql-files.txt
.ifndef dump
dump = dumps/latest-data
.endif
list:
@ psql ${DB} \
--pset="pager=off" \
--command="\dt[+]"
console:
echo ${DB}
@ PAGER="less -S" psql ${DB}
dumpschema:
@ pg_dump ${DB} \
--verbose \
--format=p \
--no-owner \
--schema-only \
> dumps/${DUMP}-schema
@ (cd dumps && ln -sf ${DUMP}-schema latest-schema)
dumpdata:
@ pg_dump ${DB} \
--verbose \
--format=p \
--no-owner \
--data-only \
> dumps/${DUMP}-data
@ (cd dumps && ln -sf ${DUMP}-data latest-data)
drop:
@ psql ${PG}/template1 --quiet \
--command "update pg_database set datallowconn = 'false' where datname = '${DB_NAME}';" \
--command "select pg_terminate_backend(pid) from pg_stat_activity where datname = '${DB_NAME}';" \
--command "drop database ${DB_NAME};" \
> /dev/null
create:
@ psql ${PG}/template1 --quiet --command "create database ${DB_NAME};"
build:
@ for file in ${SQL_FILES}; do \
psql ${DB} \
--set="ON_ERROR_STOP=on" \
--file="$$file.sql" >/dev/null; \
done
restore:
@ psql ${DB} \
--set="ON_ERROR_STOP=on" \
--pset="tuples_only=on" \
--command="set session_replication_role = replica;" \
--file="${dump}"
@ echo "Restored from dumpfile: ${dump}"
reload:
@ psql ${DB} --quiet --command "NOTIFY pgrst, 'reload schema';"
.if ${env} == "production"
rebuild:
@ echo "PRODUCTION. Do it by hand: drop create build restore reload"
.else
rebuild: drop create build restore reload
.endif