Commit a945a148 authored by Matheus Horstmann's avatar Matheus Horstmann 🐴

Issue #3: Create clean and fixture tasks

Signed-off-by: Matheus Horstmann's avatarMatheus Horstmann <mch15@inf.ufpr.br>
parent ed857932
Pipeline #19456 passed with stage
in 49 seconds
1;worker1 task1
1;worker2 task1
2;worker2 task2
3;worker3 task1
3;worker3 task2
3;worker3 task3
1;worker1;pass1
2;worker2;pass2
3;worker3;pass3
......@@ -30,6 +30,8 @@ declare -A TASKS=(
[create]=createDB
[load]=loadDB
[drop]=dropDB
[clean]=cleanDB
[fixture]=fixtureDB
)
# Source script files
......
#! /bin/bash
# Clean all data from database
cleanDB() {
# Get all deletable tables fron DB
tables=$(
psql -h ${POSTGRES_HOST}\
-d ${POSTGRES_DB}\
-U ${POSTGRES_USER}\
-c "\d" |\
tail -n+4 |\
head -n-2 |\
cut -d '|' -f2\
)
# Create temporary workspace (utility function)
tmpWS=$(createTmpWorkspace)
# Create transactions files
echo "BEGIN;" > "$tmpWS/trans.sql"
i=0
for t in $tables; do
echo "DELETE FROM $t;">> "$tmpWS/trans.sql"
done
echo "COMMIT;" >> "$tmpWS/trans.sql"
# Performs schema creation. 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/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
}
#! /bin/bash
# Clean and Load dummies datas in the database. The name of each file MUST be <table name>.csv
fixtureDB() {
source "./clean.sh"
source "./utils.sh"
# Reads workspace
ws=${1:-"./data"}
workspace="$ws/fixture"
cleanDB
# Get load files, and sort it
tables=$(ls $workspace | sort | cut -d'.' -f1)
# Create temporary workspace (utility function)
tmpWS=$(createTmpWorkspace)
# Create transactions files
echo "BEGIN;" > "$tmpWS/trans.sql"
i=0
for t in $tables; do
cpyComand $t $workspace/$t.csv >> "$tmpWS/trans.sql"
done
echo "COMMIT;" >> "$tmpWS/trans.sql"
# Performs schema creation. 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/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
}
......@@ -2,6 +2,8 @@
# 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,10 +42,3 @@ loadDB() {
rm -fr $tmpWs
return $error
}
# Creates the COPY command used to insert data
cpyComand() {
table=${1:-""}
file=${2:-""}
echo "\\copy $table FROM '$file' WITH CSV HEADER ENCODING 'UTF8' DELIMITER ';'"
}
......@@ -19,3 +19,10 @@ correctEnv() {
done
return $error
}
# Creates the COPY command used to insert data
cpyComand() {
table=${1:-""}
file=${2:-""}
echo "\\copy $table FROM '$file' WITH CSV HEADER ENCODING 'UTF8' DELIMITER ';'"
}
......@@ -37,8 +37,28 @@ else
echo -e "\tFail"
fi
echo "Should create dummie data in the database"
./manager.sh fixture &> /dev/null
if [[ $? -eq 0 ]]; then
echo -e "\tSucess"
else
error=$(($error +1))
echo -e "\tFail"
fi
echo "Should clean the database"
./manager.sh clean &> /dev/null
if [[ $? -eq 0 ]]; then
echo -e "\tSucess"
else
error=$(($error +1))
echo -e "\tFail"
fi
echo "Should drop the database"
./manager.sh load &> /dev/null
./manager.sh drop &> /dev/null
if [[ $? -eq 0 ]]; then
echo -e "\tSucess"
......
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