diff --git a/.travis.yml b/.travis.yml index 2713e4b..9285b5e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,30 +10,9 @@ addons: packages: - expect-dev # provides unbuffer utility -before_install: - - psql --version - - sudo /etc/init.d/postgresql stop - - sudo apt-get -y --purge remove postgresql libpq-dev libpq5 postgresql-client-common postgresql-common - - sudo rm -rf /var/lib/postgresql - - wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - - - sudo sh -c "echo deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main $PGVERSION >> /etc/apt/sources.list.d/postgresql.list" - - sudo sh -c "echo deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg-testing main $PGVERSION >> /etc/apt/sources.list.d/postgresql.list" - - sudo apt-get update -qq - - sudo apt-get -y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::="--force-confnew" install postgresql-$PGVERSION postgresql-server-dev-$PGVERSION - - sudo chmod 777 /etc/postgresql/$PGVERSION/main/pg_hba.conf - - sudo echo "local all postgres trust" > /etc/postgresql/$PGVERSION/main/pg_hba.conf - - sudo echo "local all all trust" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf - - sudo echo "host all all 128.0.0.1/32 trust" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf - - sudo echo "host all all ::1/128 trust" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf - - sudo /etc/init.d/postgresql restart - - psql --version - -before_script: - - createuser -U postgres -s travis - - psql -c "CREATE USER myflow WITH PASSWORD 'myflow';" -U postgres - - psql -c 'CREATE DATABASE myflow WITH OWNER myflow;' -U postgres - env: + global: + - DBFLOW=myflow matrix: - PGVERSION=9.4 - PGVERSION=9.5 @@ -41,16 +20,42 @@ env: - PGVERSION=10 - PGVERSION=11 +before_install: + - psql --version + - sudo /etc/init.d/postgresql stop + - sudo apt-get -y --purge remove postgresql libpq-dev libpq5 postgresql-client-common postgresql-common + - sudo rm -rf /var/lib/postgresql + - wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - + - sudo sh -c "echo deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main $PGVERSION >> /etc/apt/sources.list.d/postgresql.list" + - sudo sh -c "echo deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg-testing main $PGVERSION >> /etc/apt/sources.list.d/postgresql.list" + - sudo apt-get update -qq + - sudo apt-get -y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::="--force-confnew" install postgresql-$PGVERSION postgresql-server-dev-$PGVERSION postgresql-$PGVERSION-pgtap + - sudo chmod 777 /etc/postgresql/$PGVERSION/main/pg_hba.conf + - sudo echo "local all postgres trust" > /etc/postgresql/$PGVERSION/main/pg_hba.conf + - sudo echo "local all all trust" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf + - sudo echo "host all all 128.0.0.1/32 trust" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf + - sudo echo "host all all ::1/128 trust" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf + - sudo /etc/init.d/postgresql restart + - psql --version + +before_script: + - createuser -U postgres -s travis + - psql -c "CREATE USER myflow WITH PASSWORD 'myflow';" -U postgres + - psql -c 'CREATE DATABASE myflow WITH OWNER myflow;' -U postgres + - psql -c 'CREATE EXTENSION pgtap;' -U postgres -d $DBFLOW + install: - - git clone https://github.com/sql-flow/pg-extension.git $HOME/pg-extension - - export PATH=$HOME/pg-extension/scripts:$PATH - - echo "Install OK" + - git clone https://github.com/sql-flow/pg-extension.git $HOME/pg-extension + - export PATH=$HOME/pg-extension/scripts:$PATH + - echo "Install OK" script: - - psql -v "ON_ERROR_STOP=1" -f ./sql/sqlflow-structure.sql -U postgres myflow - - psql -v "ON_ERROR_STOP=1" -f ./sql/sqlflow-instance.sql -U postgres myflow + - psql -v "ON_ERROR_STOP=1" -f ./sql/sqlflow-structure.sql -U postgres $DBFLOW + - psql -v "ON_ERROR_STOP=1" -f ./sql/sqlflow-instance.sql -U postgres $DBFLOW + - psql -v "ON_ERROR_STOP=1" -f ./sql/sqlflow-functions.sql -U postgres $DBFLOW + - pg_prove -U postgres -d $DBFLOW test/*.sql after_success: - - echo "Success OK" - - psql -f ./sql/uninstall_sqlflow.sql -U postgres myflow + - echo "Success OK" + - psql -f ./sql/uninstall_sqlflow.sql -U postgres $DBFLOW diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..740e11a --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +TESTS = $(wildcard test/sql/*.sql) +REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS)) +REGRESS_OPTS = --inputdir=test --load-language=plpgsql +PG_CONFIG ?= pg_config +PG92 = $(shell $(PG_CONFIG) --version | grep -qE " 8\.| 9\.0| 9\.1" && echo no || echo yes) + +ifeq ($(PG92),no) +$(error $(EXTENSION) requires PostgreSQL 9.2 or higher) +endif + +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) diff --git a/sql/sqlflow-functions.sql b/sql/sqlflow-functions.sql new file mode 100644 index 0000000..dbc664c --- /dev/null +++ b/sql/sqlflow-functions.sql @@ -0,0 +1,31 @@ +/* +function: GET_WORKFLOW_ID() +argument: 1> a_ref: the unique reference of the workflow +return: Primary key of the workflow +*/ +CREATE OR REPLACE FUNCTION get_workflow_id( + a_ref CHARACTER VARYING(36) +) RETURNS integer AS +$BODY$ + +DECLARE + t_workflow_id INTEGER DEFAULT NULL; +BEGIN + + BEGIN + SELECT id INTO STRICT t_workflow_id + FROM sqlflow.workflow + WHERE uref=a_ref; + EXCEPTION + WHEN NO_DATA_FOUND THEN + RAISE EXCEPTION 'Workflow "%" not found', a_ref; + WHEN TOO_MANY_ROWS THEN + RAISE EXCEPTION 'Workflow "%" not unique', a_ref; + END; + + RETURN t_workflow_id; + +END; +$BODY$ +LANGUAGE plpgsql IMMUTABLE; + diff --git a/sql/sqlflow-instance.sql b/sql/sqlflow-instance.sql index 245855c..ec22a3b 100644 --- a/sql/sqlflow-instance.sql +++ b/sql/sqlflow-instance.sql @@ -8,7 +8,6 @@ SET search_path TO sqlflow, public; CREATE TABLE sqlflow.instance ( id bigserial, - -- workflow_id integer REFERENCES sqlflow.workflow ON DELETE CASCADE, version_id integer REFERENCES sqlflow.version ON DELETE CASCADE ON UPDATE CASCADE, rel_table character varying(127) NOT NULL, flow_type flow_type NOT NULL DEFAULT 'row', diff --git a/sql/sqlflow-structure.sql b/sql/sqlflow-structure.sql index bd424a2..3dfd466 100644 --- a/sql/sqlflow-structure.sql +++ b/sql/sqlflow-structure.sql @@ -6,6 +6,7 @@ -- -- Add user to manage sqlflow object s in database -- +DROP ROLE IF EXISTS sqlflow; CREATE ROLE sqlflow WITH NOLOGIN NOSUPERUSER diff --git a/test/base.sql b/test/base.sql new file mode 100644 index 0000000..d2b7ed9 --- /dev/null +++ b/test/base.sql @@ -0,0 +1,21 @@ +-- Start transaction and plan the tests. +BEGIN; +SELECT plan(10); + +-- Run test on sqlflow structure +SELECT has_schema('sqlflow'); + +SELECT has_type('sqlflow', 'flow_type', 'flow_type exists'); +SELECT has_type('sqlflow', 'flow_cond', 'flow_cond exists'); +SELECT has_type('sqlflow', 'flow_state', 'flow_state exists'); + +SELECT has_table('sqlflow', 'workflow', 'workflow table exists'); +SELECT has_column('sqlflow', 'workflow', 'id', 'id column exists'); +SELECT has_column('sqlflow', 'workflow', 'uref', 'uref column exists'); +SELECT has_column('sqlflow', 'workflow', 'title', 'title column exists'); +SELECT has_column('sqlflow', 'workflow', 'rel_table', 'rel_table column exists'); +SELECT has_column('sqlflow', 'workflow', 'flow_type', 'flow_type column exists'); + +-- Finish the tests and clean up. +SELECT * FROM finish(); +ROLLBACK;