Commit 26649986 authored by Lucas Fernandes de Oliveira's avatar Lucas Fernandes de Oliveira

Merge branch 'issue/6' into 'master'

Issue #6: Fix serial attribute problem and locale problem

See merge request c3sl/psql-manager!7
parents fe2cedf1 30df84e5
Pipeline #19914 passed with stage
in 1 minute and 3 seconds
CREATE TABLE worker(
id INTEGER
id SERIAL
, login TEXT
, password TEXT
);
CREATE TABLE task(
workerId INTEGER
, description TEXT
, identifier SERIAL
);
1;worker1 task1
1;worker2 task1
2;worker2 task2
3;worker3 task1
3;worker3 task2
3;worker3 task3
workerId;description;identifier
1;worker1 task1;1
1;worker2 task1;2
2;worker2 task2;3
3;worker3 task1;4
3;worker3 task2;5
3;worker3 task3;6
id;login;password
1;worker1;pass1
2;worker2;pass2
3;worker3;pass3
1;worker1 task1
1;worker2 task1
2;worker2 task2
3;worker3 task1
3;worker3 task2
3;worker3 task3
workerId;description;identifier
1;worker1 task1;1
1;worker2 task1;2
2;worker2 task2;3
3;worker3 task1;4
3;worker3 task2;5
3;worker3 task3;6
id;login;password
1;worker1;pass1
2;worker2;pass2
3;worker3;pass3
......@@ -4,7 +4,7 @@
if [[ $# -ne 1 && $# -ne 2 ]]; then
echo "Correct use: $0 <task> [directory]"
echo "The allowed tasks are: create"
echo "The allowed tasks are: create, load, fixture, drop and clean"
echo "The directory structure should respect the example struct used in:"
echo "./data directory"
......
......@@ -4,13 +4,14 @@
cleanDB() {
# Get all deletable tables fron DB
tables=$(
unset -v $(locale | cut -f1 -d'=');
psql -h ${POSTGRES_HOST}\
-d ${POSTGRES_DB}\
-U ${POSTGRES_USER}\
-c "\d" |\
tail -n+4 |\
head -n-2 |\
grep "^.*|.*|.*table.*|.*$" |\
grep "^.*|.*| *table *|.*$" |\
cut -d '|' -f2\
)
# Create temporary workspace (utility function)
......
......@@ -46,5 +46,11 @@ fixtureDB() {
# Utility function that removes the temporary workspace
rm -fr $tmpWs
#Update sequences for serial attributes
updateSequences
seqerror=$?
error=$(( $error + $seqerror))
return $error
}
......@@ -3,7 +3,7 @@
# Load csv files in the database. The name of each file MUST be <table name>.csv
loadDB() {
source "./utils.sh"
# Reads workspace
ws=${1:-"./data"}
workspace="$ws/load"
......@@ -40,5 +40,11 @@ loadDB() {
# Utility function that removes the temporary workspace
rm -fr $tmpWs
#Update sequences for serial attributes
updateSequences
seqerror=$?
error=$(( $error + $seqerror))
return $error
}
......@@ -26,3 +26,51 @@ cpyComand() {
file=${2:-""}
echo "\\copy $table FROM '$file' WITH CSV HEADER ENCODING 'UTF8' DELIMITER ';'"
}
# Updates the sequence tables. Used to fix serial data after copy
updateSequences() {
# Get sequences to update
sequences=$(
unset -v $(locale | cut -f1 -d'=');
psql -h ${POSTGRES_HOST}\
-d ${POSTGRES_DB}\
-U ${POSTGRES_USER}\
-c "\d" |\
grep "^.*|.*| *sequence *|.*$" |\
cut -d'|' -f2)
# Create temporary workspace (utility function)
tmpWS=$(createTmpWorkspace)
# Create transactions files
echo "BEGIN;" > "$tmpWS/seq-trans.sql"
i=0
for s in $sequences; do
table=$(echo $s | cut -d'_' -f1)
attr=$(echo $s | cut -d'_' -f2)
query="SELECT setval('$s', (SELECT MAX($attr) FROM $table));"
echo $query >> "$tmpWS/seq-trans.sql"
done
echo "COMMIT;" >> "$tmpWS/seq-trans.sql"
# Performs sequence update. As is in a transaction if fails it will
# NOT leave the database inconsistent.
psql -h ${POSTGRES_HOST}\
-d ${POSTGRES_DB}\
-U ${POSTGRES_USER}\
-f $tmpWS/seq-trans.sql 2> $tmpWS/error.out
# If a error occurs will be sent to error.out
# If error.out is not empty, should return a error
egrep . $tmpWS/error.out
if [[ $? -eq 0 ]]; then
error=1
else
error=0
fi
# Utility function that removes the temporary workspace
rm -fr $tmpWs
return $error
}
......@@ -37,6 +37,26 @@ else
echo -e "\tFail"
fi
echo "Should insert a tuple, with the right sequence"
psql -h ${POSTGRES_HOST}\
-d ${POSTGRES_DB}\
-U ${POSTGRES_USER}\
-c "INSERT INTO worker(login, password) VALUES('worker4', 'pass4')"\
&> /dev/null
value=$(psql -h ${POSTGRES_HOST}\
-d ${POSTGRES_DB}\
-U ${POSTGRES_USER}\
-c "SELECT MAX(id) FROM worker" |\
head -n3 | tail -n1)
if [[ $value -eq 4 ]]; then
echo -e "\tSucess"
else
error=$(($error +1))
echo -e "\tFail"
fi
echo "Should create dummie data in the database"
./manager.sh fixture &> /dev/null
......@@ -98,6 +118,7 @@ fi
# echo -e "\tFail"
# fi
echo "$error error(s) occured."
if [[ $error -gt 0 ]]; then
exit 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment