Files
chill-import-from-canvas/import_all_csv.sh

113 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DB_HOST="${PGHOST:-localhost}"
DB_PORT="${PGPORT:-5432}"
DB_USER="${PGUSER:-postgres}"
DB_NAME="${PGDATABASE:-chill-import}"
PSQL=(psql -v ON_ERROR_STOP=1 -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME")
required_tables=(
"import.personnes"
"import.choix_periodes"
"import.choix_localisations"
"import.tiers"
)
required_files=(
"$ROOT_DIR/csv/choix_usagers.csv"
"$ROOT_DIR/csv/choix_periodes.csv"
"$ROOT_DIR/csv/choix_localisations.csv"
"$ROOT_DIR/csv/choix_tiers.csv"
)
for table in "${required_tables[@]}"; do
exists="$("${PSQL[@]}" -tAc "SELECT to_regclass('${table}') IS NOT NULL;")"
if [[ "$exists" != "t" ]]; then
echo "Table manquante: ${table}" >&2
echo "Lance d'abord: psql -h ${DB_HOST} -p ${DB_PORT} -U ${DB_USER} -d ${DB_NAME} -f sql/prepare-import.sql" >&2
exit 1
fi
done
for csv_file in "${required_files[@]}"; do
if [[ ! -f "$csv_file" ]]; then
echo "Fichier CSV manquant: ${csv_file#$ROOT_DIR/}" >&2
exit 1
fi
done
echo "[0/4] Assouplissement des types texte des tables import..."
"${PSQL[@]}" <<'SQL'
DO $$
DECLARE r record;
BEGIN
FOR r IN
SELECT table_schema, table_name, column_name
FROM information_schema.columns
WHERE table_schema = 'import'
AND table_name IN ('personnes', 'choix_periodes', 'choix_localisations', 'tiers')
AND data_type IN ('character varying', 'character')
LOOP
EXECUTE format(
'ALTER TABLE %I.%I ALTER COLUMN %I TYPE text',
r.table_schema,
r.table_name,
r.column_name
);
END LOOP;
END $$;
SQL
echo "[1/4] Vidage des tables d'import..."
"${PSQL[@]}" <<'SQL'
TRUNCATE TABLE
import.personnes,
import.choix_periodes,
import.choix_localisations,
import.tiers
RESTART IDENTITY;
SQL
echo "[2/4] Import des CSV du dossier csv/..."
copy_table_from_file() {
local table="$1"
local file_path="$2"
local columns="${3:-}"
local target="$table"
if [[ -n "$columns" ]]; then
target="${table}(${columns})"
fi
echo " - import ${table} <= ${file_path#$ROOT_DIR/}"
"${PSQL[@]}" -c "\\copy ${target} FROM STDIN WITH (FORMAT csv, HEADER true, DELIMITER ',')" < "$file_path"
}
copy_table_from_file "import.personnes" "$ROOT_DIR/csv/choix_usagers.csv" "civility,lastname,firstname,gender,gendercomment,nationality,memo,birthdate,place_of_birth,countryofbirth,deathdate,email,phonenumber,mobilenumber,contactinfo,street,extra,streetnumber,postcode,country"
copy_table_from_file "import.choix_periodes" "$ROOT_DIR/csv/choix_periodes.csv" "closingmotive,origin,acp_scopes,job,referrer,parent,enfant,acp_social_issues,work_social_action,street,extra,streetnumber,postcode,country"
copy_table_from_file "import.choix_localisations" "$ROOT_DIR/csv/choix_localisations.csv" "title,addressRequired,availableForUsers,contactData,defaultFor,editableByUsers"
echo "[3/4] Import de tiers via table staging..."
"${PSQL[@]}" -c "\copy import.tiers_stage(coordonnees,nom,categorie) FROM STDIN WITH (FORMAT csv, HEADER true, DELIMITER ',')" < "$ROOT_DIR/csv/choix_tiers.csv"
"${PSQL[@]}" <<'SQL'
INSERT INTO import.tiers (id, coordonnees, nom, categorie)
SELECT
row_number() OVER (ORDER BY nom) AS id,
NULLIF(trim(coordonnees), ''),
NULLIF(trim(nom), ''),
NULLIF(trim(categorie), '')
FROM import.tiers_stage
WHERE COALESCE(trim(nom), '') <> '';
DROP TABLE import.tiers_stage;
SQL
echo "[4/4] Verification des volumes importes..."
"${PSQL[@]}" -c "SELECT 'personnes' AS table_name, count(*) AS rows FROM import.personnes UNION ALL SELECT 'choix_periodes', count(*) FROM import.choix_periodes UNION ALL SELECT 'choix_localisations', count(*) FROM import.choix_localisations UNION ALL SELECT 'tiers', count(*) FROM import.tiers;"
echo "Import termine sans doublons (tables videes au debut)."