All pastes #794462 Raw Edit

Makefile for database

public text v1 · immutable
#794462 ·published 2007-11-23 04:10 UTC
rendered paste body
#! /usr/bin/make -f

# Copyright  2007 Cybersource Pty. Ltd.
# Part of Hydraulics, a monitoring and analysis database system.
#
# This is free software; you may copy, modify and/or distribute this work
# under the terms of the GNU General Public License, version 2 or later.
# No warranty expressed or implied. See the file LICENSE for details.

# Makefile for Hydraulics database

DB_NAME ?= hydraulics
PG_ROOT_DIR ?= ./postgres-tmp
PG_SOCK_DIR ?= $(abspath ${PG_ROOT_DIR}/run)
PG_LOG_DIR ?= ${PG_ROOT_DIR}/log
PG_DATA_DIR ?= ${PG_ROOT_DIR}/data
PG_LOG_NAME ?= postgresql.log
pg_log_file = ${PG_LOG_DIR}/${PG_LOG_NAME}
pg_pid_file = ${PG_DATA_DIR}/postmaster.pid

SQL_SUFFIX = .sql
revision_setup_files = revisions-setup-tables.sql revisions-setup-functions.sql
revision_setup_targets = $(addsuffix -apply,${revision_setup_files})
REVISIONS_DIR = ./revisions
revision_files = $(wildcard ${REVISIONS_DIR}/*${SQL_SUFFIX})
revision_targets = $(addsuffix -apply,${revision_files})

export PGDATABASE = ${DB_NAME}
export PGDATA = ${PG_DATA_DIR}
export PGHOST = ${PG_SOCK_DIR}

INSTALL = install
RM = rm

PG_BIN_DIR = /usr/lib/postgresql/8.2/bin
INITDB = ${PG_BIN_DIR}/initdb
INITDB_OPTS = -A "ident sameuser"
PG_CTL = ${PG_BIN_DIR}/pg_ctl
PG_CTL_START_OPTS = -w -o "-h \"\" -k ${PG_SOCK_DIR}" -l ${pg_log_file}
PG_CTL_STOP_OPTS = -w
CREATEDB = ${PG_BIN_DIR}/createdb
CREATELANG = ${PG_BIN_DIR}/createlang
PSQL = ${PG_BIN_DIR}/psql
APPLY_REVISION = ../bin/apply-schema-revision


.PHONY: all
all: test-build

.PHONY: test-build
test-build: setup build teardown


.PHONY: init
init: ${PG_DATA_DIR}

${PG_DATA_DIR}:
	$(INITDB) ${INITDB_OPTS}

.PHONY: setup
setup: setup-stamp
setup-stamp: ${PG_DATA_DIR} start
	touch $@

.PHONY: start
start: ${PG_DATA_DIR} ${pg_pid_file}
${pg_pid_file}: PGHOST =
${pg_pid_file}: ${PG_SOCK_DIR} ${PG_LOG_DIR}
	$(PG_CTL) ${PG_CTL_START_OPTS} start
	sleep 2
	[ -e ${pg_pid_file} ] || exit 1

${PG_SOCK_DIR}:
	$(INSTALL) -d $@

${PG_LOG_DIR}:
	$(INSTALL) -d $@


.PHONY: build
build: build-stamp
build-stamp: setup create ${revision_setup_targets} ${revision_targets}
	touch $@

.PHONY: create
create: create-stamp
create-stamp:
	$(CREATEDB) ${DB_NAME}
	$(CREATELANG) plpgsql ${DB_NAME}
	touch $@

revisions-setup-%${SQL_SUFFIX}-apply: revisions-setup-%${SQL_SUFFIX}
	$(PSQL) -f $<

${REVISIONS_DIR}/%${SQL_SUFFIX}-apply: ${REVISIONS_DIR}/%${SQL_SUFFIX}
	$(APPLY_REVISION) ${DB_NAME} $<


.PHONY: clean
clean: teardown
	$(RM) -f *-stamp

.PHONY: teardown
teardown: stop
	$(RM) -rf ${PG_ROOT_DIR}

.PHONY: stop
stop:
	if [ -e "${pg_pid_file}" ] ; then \
		$(PG_CTL) ${PG_CTL_STOP_OPTS} stop ; \
	fi