# Semana 11: procesamiento de consultas (costo, selección, ordenamiento
# externo, joins) y operadores con la interfaz de iteradores, en Node.js.
# Se usa dentro del contenedor del curso.
#
#   make          crea la base de datos chasqui_w11 con los datos de ejemplo,
#                 hace VACUUM (FREEZE, ANALYZE) para que las páginas queden en
#                 un estado conocido, crea los índices de la semana 10 sobre
#                 shipment(sender_id) y shipment(created_at) e instala
#                 pageinspect
#   make test     la vuelve a crear y corre las pruebas (*.test.mjs); las de
#                 iterators.test.mjs fallan hasta que terminen el laboratorio
#   make big      crea chasqui_w11_big, una copia de chasqui_big con el índice
#                 en shipment(sender_id), para big.sql (solo funciona si nadie
#                 está conectado a chasqui_big)
#   make clean    borra chasqui_w11 y chasqui_w11_big
#
# Los .sql se corren con psql -d chasqui_w11 -f selection.sql (o sort.sql,
# joins.sql, other.sql); big.sql con psql -d chasqui_w11_big -f big.sql.

DB = chasqui_w11
BIG = chasqui_w11_big
PSQL = psql -X -q -v ON_ERROR_STOP=1

.PHONY: all test big clean

all:
	chasqui-load $(DB)
	$(PSQL) -d $(DB) -c "VACUUM (FREEZE, ANALYZE)" \
	  -c "CREATE INDEX shipment_sender_idx ON shipment (sender_id)" \
	  -c "CREATE INDEX shipment_created_at_idx ON shipment (created_at)" \
	  -c "CREATE EXTENSION IF NOT EXISTS pageinspect" \
	  -c "ANALYZE"

test: all
	PGDATABASE=$(DB) node --test --test-concurrency=1 --test-reporter=spec

big:
	dropdb --if-exists $(BIG)
	createdb -T chasqui_big $(BIG)
	$(PSQL) -d $(BIG) -c "SET client_min_messages = warning" \
	  -c "CREATE INDEX shipment_sender_idx ON shipment (sender_id)" \
	  -c "CREATE EXTENSION IF NOT EXISTS pageinspect" \
	  -c "VACUUM (ANALYZE)"

clean:
	dropdb --if-exists $(DB)
	dropdb --if-exists $(BIG)
