Issue #17: Fix MonetDB compatibility issues

Signed-off-by: Lucas Fernandes de Oliveira's avatarLucas Fernandes de Oliveira <lfoliveira@inf.ufpr.br>
parent 013d4be6
Pipeline #21089 passed with stage
in 1 minute and 6 seconds
CREATE TABLE job(
description TEXT
);
description
"job1"
"job2"
"job3"
......@@ -2,7 +2,7 @@
# Script used as interface to manage a PostgreSQL database
if [[ $# -ne 1 && $# -ne 2 ]]; then
if [[ $# -ne 2 && $# -ne 3 ]]; then
echo "Correct use: $0 <dbms> <task> [directory]"
echo "The allowed dbms's are: psql and monet"
echo "The allowed tasks are: create, load, fixture, drop and clean"
......@@ -19,7 +19,7 @@ workspace=${3:-"./data"}
# transform relative path into absolute path,
# only when the path is indicated
if [[ $# -eq "2" ]]; then
if [[ $# -eq "3" ]]; then
workspace="$(cd $workspace && pwd)"
fi
......
......@@ -3,22 +3,14 @@
# Clean all data from database
cleanDB() {
# Get all deletable tables fron DB
tables=$(
unset -v $(locale | cut -f1 -d'=');
execStatement "\d" |\
tail -n+4 |\
head -n-2 |\
grep "^.*|.*| *table *|.*$" |\
cut -d '|' -f2\
)
tables=$(getTables)
# Create temporary workspace (utility function)
tmpWS=$(createTmpWorkspace)
tmpWS=$(mktemp -d)
# Create transactions files
beginTrans > "$tmpWS/trans.sql"
i=0
for t in $tables; do
echo "DELETE FROM $t;">> "$tmpWS/trans.sql"
cleanCommand $t >> "$tmpWS/trans.sql"
done
commitTrans >> "$tmpWS/trans.sql"
......@@ -36,6 +28,12 @@ cleanDB() {
fi
# Utility function that removes the temporary workspace
rm -fr $tmpWs
rm -fr $tmpWS
return $error
}
# Created a DELETE TABLE statement
cleanCommand() {
table=${1:-""}
echo "DELETE FROM \"$table\";"
}
......@@ -15,20 +15,20 @@ createDB() {
schemaFiles="$schemaFiles $workspace/$f"
done
# Create temporary workspace (utility function)
tmpWS=$(createTmpWorkspace)
# Create temporary workspace
tmpWS=$(mktemp -d)
# Create transactions files
beginTrans > "$tmpWS/begin.sql"
commitTrans > "$tmpWS/commit.sql"
mkfifo $tmpWS/trans.fifo
mkfifo "$tmpWS/trans.fifo"
touch $tmpWS/error.out
# Concats the transaction and schema files
cat "$tmpWS/begin.sql"\
$schemaFiles\
"$tmpWS/commit.sql"\
> "$tmpWS/trans.fifo" &
> "$tmpWS/trans.fifo"&
# Performs schema creation. As is in a transaction if fails it will
# NOT leave the database inconsistent.
......
......@@ -7,7 +7,7 @@ execFile() {
if [[ ${DBMS} == "psql" ]]; then
PGPASSFILE=$PGPASSFILE psql -d ${DB_NAME} -U ${DB_USER} -h ${DB_HOST} -f $file 2> $error
elif [[ ${DBMS} == "monet" ]]; then
DOTMONETDBFILE=$DOTMONETDBFILE mclient < $file 2> $error
DOTMONETDBFILE=$DOTMONETDBFILE mclient -E "UTF8" < $file 2> $error
fi
}
......@@ -18,7 +18,7 @@ execStatement() {
if [[ ${DBMS} == "psql" ]]; then
PGPASSFILE=$PGPASSFILE psql -d ${DB_NAME} -U ${DB_USER} -h ${DB_HOST} -c "$statement"
elif [[ ${DBMS} == "monet" ]]; then
echo "$statement" | DOTMONETDBFILE=$DOTMONETDBFILE mclient -i
echo "$statement" | DOTMONETDBFILE=$DOTMONETDBFILE mclient -i -E "UTF8"
fi
}
......@@ -90,27 +90,31 @@ getSequences() {
tables=$(getTables)
for t in $tables; do
seq=$(\
execStatement "\d $t" |\
execStatement "\d \"$t\"" |\
grep "nextval(" |\
sed -e "s/nextval('\|'::regclass)//g"
)
attr=$(echo "$seq" | cut -d'|' -f1)
seq_name=$(echo "$seq" | cut -d'|' -f5)
echo "$seq_name,$attr,$t" | sed -e 's/ //g'
if [[ -n $seq ]]; then
attr=$(echo "$seq" | cut -d'|' -f1)
seq_name=$(echo "$seq" | cut -d'|' -f5)
echo "$seq_name,$attr,$t" | sed -e 's/ //g'
fi
done
elif [[ $DBMS == "monet" ]]; then
tables=$(getTables)
for t in $tables; do
seq=$(\
execStatement "\d $t" |\
execStatement "\d \"$t\"" |\
grep "next value" |\
tr -s ' ' |\
sed -e 's/\t//g' |\
sed -e 's/,//g'\
)
attr=$(echo "$seq" | cut -d' ' -f1)
seq_name=$(echo "$seq" | cut -d'.' -f2)
echo "$seq_name,$attr,$t" | sed -e 's/ //g'
if [[ -n $seq ]]; then
attr=$(echo "$seq" | cut -d' ' -f1)
seq_name=$(echo "$seq" | cut -d'.' -f2)
echo "$seq_name,$attr,$t" | sed -e 's/ //g'
fi
done
fi
}
......@@ -121,9 +125,9 @@ sequenceComand() {
attr=$2
table=$3
if [[ $DBMS == "psql" ]]; then
echo "SELECT setval('$seq_name', COALESCE((SELECT MAX($attr) FROM $table)+1,1),false);"
echo "SELECT setval('$seq_name', COALESCE((SELECT MAX($attr) FROM \"$table\")+1,1),false);"
elif [[ $DBMS == "monet" ]]; then
echo "ALTER SEQUENCE "$seq_name" RESTART WITH (SELECT MAX($attr) + 1 FROM $table);"
echo "ALTER SEQUENCE "$seq_name" RESTART WITH (SELECT MAX($attr) + 1 FROM \"$table\");"
fi
}
......@@ -132,8 +136,18 @@ cpyComand() {
table=${1:-""}
file=${2:-""}
if [[ $DBMS == "psql" ]]; then
echo "\\copy $table FROM '$file' WITH CSV HEADER ENCODING 'UTF8' DELIMITER ';'"
echo "\\copy \"$table\" FROM '$file' WITH CSV HEADER ENCODING 'UTF8' DELIMITER ';'"
elif [[ $DBMS == "monet" ]]; then
echo "COPY OFFSET 2 INTO $table FROM '$file' ON CLIENT USING DELIMITERS ';','\\n','\"' NULL AS '';"
echo "COPY OFFSET 2 INTO \"$table\" FROM '$file' ON CLIENT USING DELIMITERS ';','\\n','\"' NULL AS '';"
fi
}
# Creates the DROP TABLE statement
dropComand() {
table=${1:-""}
if [[ $DBMS == "psql" ]]; then
echo "DROP TABLE IF EXISTS \"$table\" CASCADE;"
elif [[ $DBMS == "monet" ]]; then
echo "DROP TABLE \"$table\" CASCADE;"
fi
}
......@@ -4,11 +4,10 @@
dropDB() {
tables=$(getTables)
# Create temporary workspace (utility function)
tmpWS=$(createTmpWorkspace)
tmpWS=$(mktemp -d)
# Create transactions files
beginTrans > "$tmpWS/trans.sql"
i=0
for t in $tables; do
dropComand $t >> "$tmpWS/trans.sql"
done
......@@ -21,7 +20,8 @@ dropDB() {
# If a error occurs will be sent to error.out
# If error.out is not empty, should return a error
egrep . $tmpWS/error.out
cat $tmpWS/error.out | grep -v "drop cascades to" > $tmpWS/error2.out
egrep . $tmpWS/error2.out
if [[ $? -eq 0 ]]; then
error=1
else
......@@ -29,12 +29,7 @@ dropDB() {
fi
# Utility function that removes the temporary workspace
rm -fr $tmpWs
rm -fr $tmpWS
return $error
}
# Creates the DROP TABLE command for each table
dropComand() {
table=${1:-""}
echo "DROP TABLE $table;"
}
......@@ -11,8 +11,8 @@ fixtureDB() {
cleanDB
# Create temporary workspace (utility function)
tmpWS=$(createTmpWorkspace)
# Create temporary workspace
tmpWS=$(mktemp -d)
# Create transactions files
beginTrans > "$tmpWS/trans.sql"
i=0
......@@ -42,7 +42,7 @@ fixtureDB() {
fi
# Utility function that removes the temporary workspace
rm -fr $tmpWs
rm -fr $tmpWS
#Update sequences for serial attributes
updateSequences
......
......@@ -6,15 +6,15 @@ loadDB() {
ws=${1:-"./data"}
workspace="$ws/load"
# Create temporary workspace (utility function)
tmpWS=$(createTmpWorkspace)
# Create temporary workspace
tmpWS=$(mktemp -d)
# Create transactions files
beginTrans > "$tmpWS/trans.sql"
tables=$(ls -1 $workspace | grep ".bz2" | rev | cut -f4- -d\. | rev)
for t in $tables; do
mkfifo $tmpWs/$t.csv
tar xjOf $workspace/$t.csv.tar.bz2 > $tmpWs/$t.csv &
cpyComand $t $tmpWs/$t.csv >> "$tmpWS/trans.sql"
mkfifo $tmpWS/$t.csv
tar xjOf $workspace/$t.csv.tar.bz2 > $tmpWS/$t.csv &
cpyComand $t $tmpWS/$t.csv >> "$tmpWS/trans.sql"
done
tables=$(ls -1 $workspace | grep -v ".bz2" | rev | cut -f2- -d\. | rev)
for t in $tables; do
......@@ -37,7 +37,7 @@ loadDB() {
fi
# Utility function that removes the temporary workspace
rm -fr $tmpWs
rm -fr $tmpWS
#Update sequences for serial attributes
updateSequences
......
#! /bin/bash
# Creates a temporary workspace directory
createTmpWorkspace() {
tmpWS='/tmp/psql-manager'
mkdir -p $tmpWS
echo $tmpWS
}
# Check if the environment vars are set
correctEnv() {
vars="DB_HOST DB_NAME DB_USER DB_PASSWORD"
......@@ -22,8 +15,8 @@ correctEnv() {
# Updates the sequence tables. Used to fix serial data after copy
updateSequences() {
# Create temporary workspace (utility function)
tmpWS=$(createTmpWorkspace)
# Create temporary workspace
tmpWS=$(mktemp -d)
# Create transactions files
beginTrans > "$tmpWS/seq-trans.sql"
......@@ -49,7 +42,7 @@ updateSequences() {
fi
# Utility function that removes the temporary workspace
rm -fr $tmpWs
rm -fr $tmpWS
return $error
}
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