Commit 764f2f95 authored by Matheus Horstmann's avatar Matheus Horstmann 🐴 Committed by Lucas Fernandes de Oliveira

Issue #6: Update sequence after copy

Signed-off-by: Matheus Horstmann's avatarMatheus Horstmann <mch15@inf.ufpr.br>
parent 26649986
#! /bin/bash
#WARNING: This file depends on "utils.sh" and "clean.sh". in order to re-use this file in some other project, you should also source "utils.sh" and "clean.sh".
# Clean and Load dummies datas in the database. The name of each file MUST be <table name>.csv
fixtureDB() {
# Make independent of path
basePath=$(echo $BASH_SOURCE | rev | cut -c 11- | rev)
cd $basePath
source "./clean.sh"
source "./utils.sh"
# Reads workspace
ws=${1:-"./data"}
......
#! /bin/bash
#WARNING: This file depends on "utils.sh". In order to re-use this file in some other project, you should also source "utils.sh".
# 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"
......
......@@ -27,29 +27,41 @@ cpyComand() {
echo "\\copy $table FROM '$file' WITH CSV HEADER ENCODING 'UTF8' DELIMITER ';'"
}
sequence() {
unset -v $(locale | cut -f1 -d'=');
psql -h ${POSTGRES_HOST}\
-d ${POSTGRES_DB}\
-U ${POSTGRES_USER}\
-c "\d $1" | grep "^.*|.*|.*|.*| *nextval('.*_seq'::regclass) *$"
}
# Updates the sequence tables. Used to fix serial data after copy
updateSequences() {
# Get sequences to update
sequences=$(
tables=$(
unset -v $(locale | cut -f1 -d'=');
psql -h ${POSTGRES_HOST}\
-d ${POSTGRES_DB}\
-U ${POSTGRES_USER}\
-c "\d" |\
grep "^.*|.*| *sequence *|.*$" |\
grep "^.*|.*| *table *|.*$" |\
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"
for t in $tables; do
results=$(sequence $t)
readarray -td')' arr <<< "$results"
unset 'arr[-1]'
for result in "${arr[@]}";do
id=$(echo $result| cut -d'|' -f1 | tr -d '[:space:]')
id_seq=$(echo $result | cut -d"'" -f2)
query="SELECT setval('$id_seq', (SELECT MAX($id) FROM $t));"
echo $query >> "$tmpWS/seq-trans.sql"
done
done
echo "COMMIT;" >> "$tmpWS/seq-trans.sql"
......@@ -72,5 +84,4 @@ updateSequences() {
# Utility function that removes the temporary workspace
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