70 lines
2.0 KiB
Makefile
70 lines
2.0 KiB
Makefile
# This Makefile is for (re)doing some database operations.
|
|
|
|
.PHONY: \
|
|
clean \
|
|
dangerous \
|
|
export-all \
|
|
export-data \
|
|
load \
|
|
rebuild \
|
|
redo-func \
|
|
restore-all \
|
|
restore-data
|
|
|
|
# Safe targets first.
|
|
|
|
# Rebuild triggers and functions, restore grants.
|
|
redo-func:
|
|
psql --dbname="meter_reading" --username="mr_owner" \
|
|
--command="\set ON_ERROR_STOP" \
|
|
--command="BEGIN TRANSACTION;" \
|
|
--file="func.sql" \
|
|
--command="COMMIT;"
|
|
|
|
# Export the entire database.
|
|
export-all:
|
|
pg_dump --username="mr_owner" meter_reading > export-all.sql
|
|
|
|
# Export only the data.
|
|
export-data:
|
|
pg_dump --data-only meter_reading > export-data.sql
|
|
|
|
################################################################################
|
|
#
|
|
# Destructive targets follow, with a small precautionary barrier in
|
|
# front of them.
|
|
#
|
|
################################################################################
|
|
|
|
dangerous:
|
|
ifndef MR_DANGEROUS
|
|
$(error Refusing to continue without MR_DANGEROUS set.)
|
|
endif
|
|
|
|
# Drop the database.
|
|
clean: dangerous
|
|
dropdb --if-exists --username="mr_owner" meter_reading
|
|
|
|
# Drop and recreate *the entire database* with no rows yet.
|
|
rebuild: dangerous clean
|
|
createdb --username="mr_owner" meter_reading \
|
|
"For my domestic utility meter readings."
|
|
psql --dbname="meter_reading" --username="mr_owner" \
|
|
--command="\set ON_ERROR_STOP" \
|
|
--command="BEGIN TRANSACTION;" \
|
|
--file="table.sql" --file="func.sql" \
|
|
--command="COMMIT;"
|
|
|
|
# Restore *the entire database* from an export file.
|
|
restore-all: dangerous clean
|
|
createdb --username="mr_owner" --template="template0" meter_reading
|
|
psql --username="mr_owner" meter_reading < export-all.sql
|
|
|
|
# Replace existing data, if any, with previously exported data.
|
|
restore-data: dangerous
|
|
psql --dbname="meter_reading" --username="mr_owner" \
|
|
--command="\set ON_ERROR_STOP" \
|
|
--command="BEGIN TRANSACTION;" \
|
|
--command="TRUNCATE TABLE meter_reading;" --file="export-data.sql" \
|
|
--command="COMMIT;"
|