-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sergey Melekhin
committed
Jan 18, 2014
1 parent
eb7203a
commit 982bb00
Showing
21 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pg_skeleton - PostgreSQL project skeleton | ||
========================================== | ||
|
||
This is simple skeleton for postgresql-based projects. | ||
It includes install and uninstall scripts, one example schema (test_user) and | ||
pgTAP tests for it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
create user :db_user with encrypted password :db_pass; | ||
create database :db_name with owner :db_user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
drop database :db_name; | ||
drop user :db_user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
create language plpgsql; | ||
create extension pgtap; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#Example settings file for installation script | ||
db_host=localhost | ||
db_port=5432 | ||
db_user=skel_test | ||
db_name=skel_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
if [ ! -f install.cfg ] ; then | ||
echo 'Error! File install.cfg does not exist. Please create it: | ||
$ cp install.cfg.example install.cfg | ||
$ vi install.cfg' | ||
|
||
exit 1 | ||
fi; | ||
|
||
# Load settings: | ||
. install.cfg | ||
read -p "Please input database schema owner ($db_user) password:" db_pass | ||
|
||
echo "Creating database." | ||
psql -v db_user=$db_user -v db_pass="'$db_pass'" -v db_name=$db_name -h $db_host -p $db_port -U postgres -f create_db.sql &>install.log | ||
|
||
echo "Enabling extensions in database. Enter your postgres user password." | ||
psql -h $db_host -p $db_port -d $db_name -U postgres -f extensions.sql &>>install.log | ||
|
||
echo "Creating schema. Enter your schema user password." | ||
psql -h $db_host -p $db_port -d $db_name -U $db_user -f install.sql &>> install.log | ||
|
||
cd 'test' | ||
./run_tests.sh | ||
|
||
echo "Done!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
\set ECHO all | ||
\qecho 'Installing DB schema' | ||
|
||
/* Creating tables first */ | ||
\i test_user/create_tables.sql | ||
|
||
/* Stored functions */ | ||
\i test_user/functions.sql | ||
|
||
\qecho 'Done.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pgTAP example test suite | ||
======================== | ||
|
||
Test functions should be placed in corresponding functions_<schema>.sql files. | ||
One-liner tests and test function calls should be in tests/<schema>_<test-category>.sql | ||
You should always update number of tests in plan(n) call in run_<schema>.sql file when | ||
you add new test to schema test suite. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
create schema test; | ||
comment on schema test is 'Schema for tests'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
create or replace function test.test_scheme_check_func(p_scheme varchar) | ||
returns setof text as | ||
$$ | ||
declare | ||
v_array text[]; | ||
begin | ||
select array_agg(f_name) | ||
into v_array | ||
from ( | ||
select distinct(n.nspname || '.' || p.proname) as f_name | ||
from pg_proc p, | ||
pg_namespace n | ||
where n.oid = p.pronamespace | ||
and n.nspname = p_scheme | ||
except | ||
select distinct(array_to_string(regexp_matches (descr,'\w+.\w+'),',','*')) as f_name | ||
from __tresults__ | ||
) as r; | ||
return next is(v_array,null,'Проверка наличия тестов для функций cхемы '||p_scheme||'.'); | ||
end; | ||
$$ language plpgsql; | ||
|
||
\i ../test/functions_users.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
create or replace function test.test_user_0010() | ||
returns setof text as | ||
$$ | ||
declare | ||
v_user_id integer; | ||
begin | ||
return next lives_ok('select test_user.add_user(''testuser unique''::varchar);','test_user.add_user doesnt throw exception'); | ||
|
||
v_user_id := test_user.add_user('blah blah'); | ||
return next cmp_ok(v_user_id,'>',0,'test_user.add_user: returns ok'); | ||
|
||
return next is(test_user.alter_user(v_user_id,'new user name blah'),v_user_id,'test_user.alter_user: returns ok'); | ||
|
||
return next is(test_user.delete_user(v_user_id),v_user_id,'test_user.delete_user: returns ok'); | ||
end; | ||
$$ language plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/bash | ||
set -e | ||
if [ ! -f ../install.cfg ] ; | ||
then | ||
echo 'Error! File install.cfg does not exist. Please create it: | ||
$ cp install.cfg.example install.cfg | ||
$ vi install.cfg' | ||
exit 1; | ||
fi; | ||
|
||
# Load settings: | ||
. ../install.cfg | ||
|
||
echo "Enter your schema user password" | ||
pg_prove -h $db_host -p $db_port -d $db_name -U $db_user tests/run_*.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
\set QUIET 1 | ||
|
||
-- Format the output for nice TAP. | ||
\pset format unaligned | ||
\pset tuples_only true | ||
\pset pager | ||
|
||
-- Revert all changes on failure. | ||
\set ON_ERROR_ROLLBACK 1 | ||
\set ON_ERROR_STOP true | ||
|
||
-- Load the TAP functions. | ||
BEGIN; | ||
|
||
\i create_tables.sql | ||
\i test_data.sql | ||
--\i ../test/pgtap.sql | ||
\i functions.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
insert into test_user.users(user_id,user_name) | ||
values(100500,'Test uesr 1'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
\i setup.sql | ||
select plan(4+2+1); | ||
|
||
--4 tests | ||
\i tests/user_crud.sql | ||
--2 tests | ||
\i tests/user_schema.sql | ||
--1 test | ||
select * from test.test_scheme_check_func('test_user'); | ||
|
||
ROLLBACK; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
select * from test.test_user_0010(); --4 tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
select tables_are('test_user', | ||
ARRAY ['users'], | ||
'Schema test_user contains users table'); | ||
select columns_are('test_user', | ||
'users', | ||
ARRAY [ 'user_id', 'user_name', 'ts'], | ||
'test_user.users column check'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
create schema test_user; | ||
comment on schema test_user is 'Схема для примеров pgTAP: пользователи'; | ||
|
||
create table test_user.users | ||
( | ||
user_id serial primary key, | ||
user_name varchar(128) unique not null, | ||
ts timestamp default now() | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
\i test_user/users_crud.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/**Adds user record | ||
*@function test_user.add_user | ||
*@param in varchar p_user_name user name | ||
*@return int user_id | ||
*/ | ||
create or replace function test_user.add_user(p_user_name in varchar) | ||
returns int | ||
as $$ | ||
insert into test_user.users(user_name) | ||
values(p_user_name) | ||
returning user_id; | ||
$$ language sql; | ||
|
||
/**Updates user record | ||
*@function test_user.alter_user | ||
*@param in int p_user_id user id | ||
*@param in varchar p_user_name user name | ||
*@return int p_user_id | ||
*/ | ||
create or replace function test_user.alter_user(p_user_id in integer, | ||
p_user_name in varchar) | ||
returns int | ||
as $$ | ||
update test_user.users | ||
set user_name = p_user_name, | ||
ts = now() | ||
where user_id = p_user_id | ||
returning user_id; | ||
$$ language sql; | ||
|
||
/**Deletes user record | ||
*@function test_user.delete_user | ||
*@param in integer p_user_id | ||
*@return int p_user_id | ||
*/ | ||
create or replace function test_user.delete_user(p_user_id in integer) | ||
returns int | ||
as $$ | ||
delete from test_user.users | ||
where user_id = p_user_id | ||
returning user_id; | ||
$$ language sql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
if [ ! -f install.cfg ] ; then | ||
echo 'Error! File install.cfg does not exist. Please create it: | ||
$ cp install.cfg.example install.cfg | ||
$ vi install.cfg' | ||
|
||
exit 1 | ||
fi; | ||
|
||
# Load settings: | ||
. install.cfg | ||
|
||
echo "Dropping database." | ||
psql -v db_user=$db_user -v db_pass="'$db_pass'" -v db_name=$db_name -h $db_host -p $db_port -U postgres -f drop_db.sql |