-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
202 lines (173 loc) · 7.86 KB
/
Makefile
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Taken from cas-cif and modified. [Source code](https://github.com/bcgov/cas-cif)
# cff321ce09b65098ebd9541c0a37eb6970ef4263
SHELL := /usr/bin/env bash
__FILENAME := $(lastword $(MAKEFILE_LIST))
__DIRNAME := $(abspath $(realpath $(lastword $(MAKEFILE_LIST)))/../)
PERL=perl
PERL_VERSION=${shell ${PERL} -e 'print substr($$^V, 1)'}
PERL_MIN_VERSION=5.10
PSQL=psql -h localhost
CPAN=cpan
# CPANM home has to be in the current directory, so that it can find the
# pg_config executable, installed via asdf
CPANM=PERL_CPANM_HOME=$(__DIRNAME)/.cpanm cpanm --notest
SQITCH=sqitch
SQITCH_VERSION=${word 3,${shell ${SQITCH} --version}}
SQITCH_MIN_VERSION=1.1.0
DB_NAME=eed
PG_PROVE=pg_prove -h localhost
PGTAP_VERSION=1.2.0
help: ## Show this help.
@sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST)
.PHONY: install_asdf_tools
install_asdf_tools: ## install languages runtimes and tools specified in .tool-versions
install_asdf_tools:
@cat .tool-versions | cut -f 1 -d ' ' | xargs -n 1 asdf plugin-add || true
@asdf plugin-update --all
@#MAKELEVEL=0 is required because of https://www.postgresql.org/message-id/1118.1538056039%40sss.pgh.pa.us
@MAKELEVEL=0 POSTGRES_EXTRA_CONFIGURE_OPTIONS='--with-libxml' asdf install
@asdf reshim
@pip install -r requirements.txt
@asdf reshim
.PHONY: install_pgtap
install_pgtap: ## install pgTAP extension into postgres
install_pgtap: start_pg
install_pgtap:
@$(PSQL) -d postgres -tc "select count(*) from pg_available_extensions where name='pgtap' and default_version='$(PGTAP_VERSION)';" | \
grep -q 1 || \
(git clone https://github.com/theory/pgtap.git --depth 1 --branch v$(PGTAP_VERSION) && \
$(MAKE) -C pgtap && \
$(MAKE) -C pgtap install && \
$(MAKE) -C pgtap installcheck && \
rm -rf pgtap)
.PHONY: install_cpanm
install_cpanm: ## install the cpanm tool
install_cpanm:
ifeq ($(shell which $(word 2,$(CPANM))),)
# install cpanm
@$(CPAN) App::cpanminus
endif
.PHONY: install_cpandeps
install_cpandeps: ## install Perl dependencies from cpanfile
install_cpandeps:
@$(CPANM) --installdeps .
@rm -rf $(__DIRNAME)/.cpanm
.PHONY: postinstall_check
postinstall_check: ## check that the installation was successful and that the correct sqitch version is available in the PATH
postinstall_check:
@printf '%s\n%s\n' "${SQITCH_MIN_VERSION}" "${SQITCH_VERSION}" | sort -CV ||\
(echo "FATAL: sqitch version should be at least ${SQITCH_MIN_VERSION}. Make sure the sqitch executable installed by cpanminus is available has the highest priority in the PATH" && exit 1);
.PHONY: install_perl_tools
install_perl_tools: ## install cpanm and sqitch
install_perl_tools: install_cpanm install_cpandeps postinstall_check
.PHONY: install_dev_tools
install_dev_tools: ## install development tools
install_dev_tools: stop_pg install_asdf_tools install_perl_tools install_pgtap
.PHONY: start_pg
start_pg: ## start the database server if it is not running
start_pg:
@pg_ctl status || pg_ctl start
.PHONY: stop_pg
stop_pg: ## stop the database server. Always exits with 0
stop_pg:
@pg_ctl stop; true
.PHONY: create_db
create_db: ## Ensure that the $(DB_NAME) database exists
create_db:
@$(PSQL) -d postgres -tc "SELECT count(*) FROM pg_database WHERE datname = '$(DB_NAME)'" | \
grep -q 1 || \
$(PSQL) -d postgres -c "CREATE DATABASE $(DB_NAME)" && \
$(PSQL) -d $(DB_NAME) -c "create extension if not exists pgtap";
.PHONY: drop_db
drop_db: ## Drop the $(DB_NAME) database if it exists
drop_db:
@$(PSQL) -d postgres -tc "SELECT count(*) FROM pg_database WHERE datname = '$(DB_NAME)'" | \
grep -q 0 || \
$(PSQL) -d postgres -c "DROP DATABASE $(DB_NAME)";
.PHONY: create_test_db
create_test_db: ## Ensure that the $(DB_NAME)_test database exists
create_test_db:
@$(PSQL) -d postgres -tc "SELECT count(*) FROM pg_database WHERE datname = '$(DB_NAME)_test'" | \
grep -q 1 || \
$(PSQL) -d postgres -c "CREATE DATABASE $(DB_NAME)_test" &&\
$(PSQL) -d $(DB_NAME)_test -c "create extension if not exists pgtap";
.PHONY: drop_foreign_test_db
drop_foreign_test_db: ## Drop the $(DB_NAME) database if it exists
drop_foreign_test_db:
@$(PSQL) -d postgres -tc "SELECT count(*) FROM pg_database WHERE datname = 'foreign_test_db'" | \
grep -q 0 || \
$(PSQL) -d postgres -c "DROP DATABASE foreign_test_db" && \
$(PSQL) -d postgres -c "DROP USER IF EXISTS foreign_user";
.PHONY: create_foreign_test_db
create_foreign_test_db: ## Ensure that the $(DB_NAME)_test database exists
create_foreign_test_db:
@$(PSQL) -d postgres -tc "SELECT count(*) FROM pg_database WHERE datname = 'foreign_test_db'" | \
grep -q 1 || \
$(PSQL) -d postgres -c "CREATE DATABASE foreign_test_db" &&\
$(PSQL) -d foreign_test_db -f "./schema/data/test_setup/external_database_setup.sql" &&\
$(PSQL) -d $(DB_NAME)_test -c "create extension if not exists postgres_fdw";
.PHONY: drop_test_db
drop_test_db: ## Drop the $(DB_NAME)_test database if it exists
drop_test_db:
@$(PSQL) -d postgres -tc "SELECT count(*) FROM pg_database WHERE datname = '$(DB_NAME)_test'" | \
grep -q 0 || \
$(PSQL) -d postgres -c "DROP DATABASE $(DB_NAME)_test";
.PHONY: deploy_db_migrations
deploy_db_migrations: ## deploy the database migrations with sqitch
deploy_db_migrations: start_pg create_db
deploy_db_migrations:
@$(SQITCH) --chdir schema deploy
@$(SQITCH) --chdir mocks_schema deploy
deploy_dev_data: ## deploy the database migrations with sqitch and load the data for local development
deploy_dev_data: deploy_db_migrations
deploy_dev_data:
@for file in $(__DIRNAME)/schema/data/dev/*; do \
$(PSQL) -d $(DB_NAME) -f "$${file}"; \
done;
deploy_prod_data: ## deploy the database migrations with sqitch and load the production data
deploy_prod_data: deploy_db_migrations
deploy_prod_data:
@for file in $(__DIRNAME)/schema/data/prod/*; do \
$(PSQL) -d $(DB_NAME) -f "$${file}"; \
done;
.PHONY: revert_db_migrations
revert_db_migrations: ## revert the database migrations with sqitch
revert_db_migrations: start_pg
revert_db_migrations:
@$(SQITCH) --chdir schema revert
@$(SQITCH) --chdir mocks_schema revert
.PHONY: verify_db_migrations
verify_db_migrations: ## verify the database migrations with sqitch
verify_db_migrations: start_pg
verify_db_migrations:
@$(SQITCH) --chdir schema verify
@$(SQITCH) --chdir mocks_schema verify
.PHONY: deploy_test_db_migrations
deploy_test_db_migrations: ## deploy the test database migrations with sqitch
deploy_test_db_migrations: start_pg create_test_db
deploy_test_db_migrations:
@SQITCH_TARGET="db:pg:" PGHOST=localhost PGDATABASE=$(DB_NAME)_test $(SQITCH) --chdir schema deploy
@SQITCH_TARGET="db:pg:" PGHOST=localhost PGDATABASE=$(DB_NAME)_test $(SQITCH) --chdir mocks_schema deploy
.PHONY: revert_test_db_migrations
revert_test_db_migrations: ## revert the test database migrations with sqitch
revert_test_db_migrations: start_pg
revert_test_db_migrations:
@SQITCH_TARGET="db:pg:" PGHOST=localhost PGDATABASE=$(DB_NAME)_test $(SQITCH) --chdir schema revert
@SQITCH_TARGET="db:pg:" PGHOST=localhost PGDATABASE=$(DB_NAME)_test $(SQITCH) --chdir mocks_schema revert
.PHONY: verify_test_db_migrations
verify_test_db_migrations: ## verify the test database migrations with sqitch
verify_test_db_migrations: start_pg
verify_test_db_migrations:
@SQITCH_TARGET="db:pg:" PGHOST=localhost PGDATABASE=$(DB_NAME)_test $(SQITCH) --chdir schema verify
@SQITCH_TARGET="db:pg:" PGHOST=localhost PGDATABASE=$(DB_NAME)_test $(SQITCH) --chdir mocks_schema verify
.PHONY: db_unit_tests
db_unit_tests: ## run the database unit tests
db_unit_tests: | start_pg drop_test_db create_test_db drop_foreign_test_db create_foreign_test_db deploy_test_db_migrations
db_unit_tests:
@$(PG_PROVE) --failures -d $(DB_NAME)_test schema/test/unit/**/*_test.sql
@$(PG_PROVE) --failures -d $(DB_NAME)_test mocks_schema/test/**/*_test.sql
.PHONY: db_style_tests
db_style_tests: ## run the database style tests
db_style_tests: | start_pg deploy_test_db_migrations
db_style_tests:
@$(PG_PROVE) --failures -d $(DB_NAME)_test schema/test/style/*_test.sql --set schemas_to_test=eed,eed_private