Skip to content

Commit

Permalink
fix sequences on retrieved data ( script by @lanaparadinha )
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Oct 19, 2024
1 parent 5417029 commit fabe8a5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
41 changes: 41 additions & 0 deletions backend/scripts/fix-sequence.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
DO $$
DECLARE
rec RECORD;
table_name TEXT;
seq_name TEXT; -- Nome da sequence
max_id BIGINT;
setval_query TEXT;
BEGIN
-- Loop através de todas as sequences no schema public
FOR rec IN
SELECT sequence_schema,
sequence_name AS seq_name,
REPLACE(sequence_name, '_id_seq', '') AS table_name
FROM information_schema.sequences
WHERE sequence_schema = 'public'
LOOP
-- Atribuir valores
seq_name := rec.seq_name;
table_name := rec.table_name;

-- Bloco para tratar erros ao tentar acessar a tabela ou a coluna 'id'
BEGIN
-- Tenta obter o valor máximo do campo 'id'
EXECUTE format('SELECT COALESCE(MAX(id), 0) FROM %I.%I', rec.sequence_schema, table_name) INTO max_id;

-- Verifica se o valor de max_id é 0 ou nulo, e define para 1 se necessário
IF max_id = 0 THEN
max_id := 1;
END IF;

-- Constrói a string do comando setval com 'id_serial'
setval_query := format('SELECT setval(''%I.%I'', %s)', rec.sequence_schema, seq_name, max_id);

-- Executa o comando setval
EXECUTE setval_query;

-- Mostrar o comando setval na tela com schema qualificado
RAISE NOTICE 'SELECT setval(''%s'', %s);', format('%I.%I', rec.sequence_schema, seq_name), max_id;
END;
END LOOP;
END $$;
16 changes: 13 additions & 3 deletions backend/scripts/load-retrieved.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ for input_file in "$INPUT_DIR"/*.csv; do
fi
done

export PGPASSWORD="${DB_PASS}"

# Loop over each CSV file and generate \COPY command for each
for input_file in "$INPUT_DIR"/*.csv; do
# Get the table name from the CSV file name, ignoring the prefix
Expand All @@ -56,7 +58,7 @@ for input_file in "$INPUT_DIR"/*.csv; do

# Generate the \COPY command to import the CSV file into the table
echo "Importing data from '$input_file'"
PGPASSWORD="${DB_PASS}" psql -h "${DB_HOST}" -U "${DB_USER}" -d "${DB_NAME}" -c "\COPY \"$table\"(${columnList}) FROM '$input_file' WITH CSV HEADER" &> "${input_file}.log"
psql -h "${DB_HOST}" -U "${DB_USER}" -d "${DB_NAME}" -c "\COPY \"$table\"(${columnList}) FROM '$input_file' WITH CSV HEADER" &> "${input_file}.log"

if [ $? -gt 0 ]; then
echo "Error importing $input_file:"
Expand All @@ -73,13 +75,21 @@ for input_file in "$INPUT_DIR"/*.csv; do
done

echo "Clearing Whatsapp Sessions"
PGPASSWORD="${DB_PASS}" psql -h "${DB_HOST}" -U "${DB_USER}" -d "${DB_NAME}" -c "UPDATE \"Whatsapps\" SET session='', status='DISCONNECTED'" &> /tmp/clearwhatsapps.log
psql -h "${DB_HOST}" -U "${DB_USER}" -d "${DB_NAME}" -c "UPDATE \"Whatsapps\" SET session='', status='DISCONNECTED'" &> /tmp/clearwhatsapps.log

if [ $? -gt 0 ]; then
echo "Error clearing Whatsapp sessions:"
cat /tmp/clearwhatsapps.log
exit 100
fi


echo "Fixing sequences"
cat ./scripts/fix-sequence.sql | psql -h "${DB_HOST}" -U "${DB_USER}" -d "${DB_NAME}" &> /tmp/fixsequences.log

if [ $? -gt 0 ]; then
echo "Error fixing sequences:"
cat /tmp/fixsequences.log
exit 100
fi

exit 1

0 comments on commit fabe8a5

Please sign in to comment.