# NOTE: if you run this Makefile on the VM, you want to run it as the
# chef-pgsql user (sudo -iu chef-pgsql)

TEST_DB = delivery_test

# See the load_data target
DATA_DUMP = t/sanitized_shd.dump
DATA_FILE = t/delivery_test_data.sql

HELPER   = t/delivery_pgtap_functions.sql
STARTUP  = $(wildcard t/startup_*.sql)
SETUP    = $(wildcard t/setup_*.sql)
TEARDOWN = $(wildcard t/teardown_*.sql)
SHUTDOWN = $(wildcard t/shutdown_*.sql)
TEST     = $(wildcard t/test_*.sql)

ALL_FUNCTIONS = $(HELPER) $(STARTUP) $(SETUP) $(TEARDOWN) $(SHUTDOWN) $(TEST)

# Lists all the pgSQL extensions we use
EXTENSIONS = pgtap chef_pgtap uuid-ossp

all: recreate_database setup_extensions setup_schema load_dump load_data test

recreate_database:
	@echo "Dropping and recreating database '$(TEST_DB)'"
	@psql --dbname template1 --single-transaction --command 'DROP DATABASE IF EXISTS $(TEST_DB)'
	@psql --dbname template1 --single-transaction --command 'CREATE DATABASE $(TEST_DB)'

setup_schema:
	@PGOPTIONS='-c client_min_messages=WARNING' sqitch --verbose --db-name $(TEST_DB) --db-host localhost deploy --verify @HEAD

setup_extensions:
	@$(foreach EXT,$(EXTENSIONS), psql --dbname $(TEST_DB) --command 'CREATE EXTENSION IF NOT EXISTS "$(EXT)";';)

$(ALL_FUNCTIONS):
	@PGOPTIONS='-c client_min_messages=WARNING' psql --dbname $(TEST_DB) --single-transaction --set ON_ERROR_STOP=1 --file $@

# With more complicated views and functions in the database, bringing
# in realistic data for testing is more important. To that end, we've
# included a binary dump file taken from our self-hosted
# deployment. It is "real" except that the phase run logs and status
# fields have been dramatically truncated (saving us orders of
# magnitude in space).
#
# We'll then layer on the manually-created data that we've been using
# for other tests, just so everything doesn't break at once.
#
# (Note that we restore only the "public" schema from the SHD dump,
# since it includes the sqitch schema. We don't want that messing up
# this testing database!)
load_dump:
	pg_restore --create --dbname=$(TEST_DB) --data-only --disable-triggers --schema=public $(DATA_DUMP)

load_data: $(ALL_FUNCTIONS)
	psql --dbname $(TEST_DB) --set ON_ERROR_STOP=1 --file $(DATA_FILE)

test: $(ALL_FUNCTIONS)
	@echo "Executing xUnit-style pgTAP tests in database '$(TEST_DB)'"
	@pg_prove --dbname $(TEST_DB) --verbose --runtests

# Useful for testing your revert scripts; just reverts everything
revert:
	@PGOPTIONS='-c client_min_messages=WARNING' sqitch --db-name $(TEST_DB) --db-host localhost revert -y

# Tests all deploy, verify, pgTAP, and revert scripts
round_trip: all revert

# Convenience target for getting into the test database to poke around
psql:
	psql -d $(TEST_DB)

# So, the test function files are actually real files, but we want to
# ensure that they always get installed using their target.
# Previously, we used a foreach loop to execute the psql command, but
# if any of the installations failed (e.g., due to a syntax error in a
# test function), makefile processing still continued, despite the
# fact that psql was returning a non-zero return code.
#
# An alternative would be to use FORCE, as detailed in
# https://www.gnu.org/software/make/manual/html_node/Force-Targets.html.
.PHONY: $(ALL_FUNCTIONS) revert
