diff --git a/backend/scripts/fix-sequence.sql b/backend/scripts/fix-sequence.sql new file mode 100644 index 0000000..4a02628 --- /dev/null +++ b/backend/scripts/fix-sequence.sql @@ -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 $$; diff --git a/backend/scripts/load-retrieved.sh b/backend/scripts/load-retrieved.sh index 5772235..dafd90f 100755 --- a/backend/scripts/load-retrieved.sh +++ b/backend/scripts/load-retrieved.sh @@ -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 @@ -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:" @@ -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