Compare commits
68 Commits
0d68d28448
...
master
Author | SHA1 | Date | |
---|---|---|---|
45755bf797 | |||
08e87503f0 | |||
20517a109b | |||
c17b2ae524 | |||
5bfc7f4a02 | |||
6b8ca0caa3 | |||
|
c5f892c27d | ||
|
82f69f9845 | ||
|
fd4e68faa8 | ||
|
3651b1a09b | ||
|
db52c72d22 | ||
|
9c90e5ac07 | ||
543a4569fd | |||
36e3bf3b4b | |||
ddfe9b53b3 | |||
cea5190174 | |||
ca3ce608c0 | |||
f3bda2a22e | |||
4e866c2d8e | |||
6b11efd18e | |||
290a9b917f | |||
b68e0399cc | |||
07df6c4292 | |||
d9152fc090 | |||
12f0ec0ae4 | |||
264fdb5888 | |||
7893eb3c81 | |||
1adf47e63e | |||
314e625c91 | |||
59daee4790 | |||
6900d66206 | |||
f513dd1b36 | |||
67d020d322 | |||
477565f6e1 | |||
e569d77bd2 | |||
0ab77b77ed | |||
307cfcbe74 | |||
081ce76085 | |||
cadcddddb5 | |||
dd3769e2a0 | |||
1cc1b83435 | |||
9ac2553eb0 | |||
0c7ea76178 | |||
5f78276762 | |||
5a6daae5a4 | |||
3978ee8b1a | |||
838bcb78ff | |||
f3170d0c87 | |||
1a6f3953ca | |||
139dd6209a | |||
25510c1efe | |||
a36ee22be6 | |||
b6f26900a7 | |||
dd89aa6ff1 | |||
4dc09c559a | |||
cb12f3000b | |||
e14f6896e3 | |||
7b4f04ef40 | |||
6d64060965 | |||
396444790f | |||
44d24155a1 | |||
32da5540fe | |||
de8450267d | |||
27ef1034be | |||
40ebabf3f6 | |||
9cd1f19f89 | |||
06b0aab237 | |||
f018a5d115 |
0
.gitignore
vendored
Normal file → Executable file
0
.gitignore
vendored
Normal file → Executable file
153
README.md
Executable file
153
README.md
Executable file
@@ -0,0 +1,153 @@
|
||||
CHILL - Reprise de données
|
||||
==========================
|
||||
|
||||
Ce dépôt contient un script d'import qui s'applique à un canevas excel présenté au client. Le client remplit le fichier excel, puis le script insère les données dans la base de donnée.
|
||||
|
||||
L'opération est semi-automatique et réduit considérablement le temps dédié à l'import en structurant le format des données en entrée. Par contre il y a toujours une série de manipulations, pour préparer et insérer les données correctement.
|
||||
|
||||
Ces manipulations sont décrites ici.
|
||||
|
||||
Le client a rempli le canevas. Une relecture du fichier est toujours nécessaire afin de repérer les éventuelles irrégularités.
|
||||
|
||||
## 1. Préparer les fichiers csv
|
||||
|
||||
Le fichier se compose de plusieurs feuilles, chacune doit être sauvée au format csv.
|
||||
|
||||
Pour préparer les fichiers on va:
|
||||
- nettoyer le fichier pour ne laisser en étiquette que les noms de colonnes en anglais. Il faut donc supprimer les 4 premières lignes de chaque fichier + la 6ème ligne.
|
||||
(- ajouter une colonne de contôle en fin de ligne, par sécurité. Par exemple une colonne 'endcol' qui contient pour chaque cellule 'endrow'.)
|
||||
- ajouter les doublequote lors de la sauvegarde du csv,
|
||||
- enlever tous les line breaks et caractères spéciaux.
|
||||
|
||||
```bash
|
||||
# Exemple de remplacements exécutés sur les fichiers csv pour un import spécifique :
|
||||
$ sed -e :1 -e '$q' -e "/$CR\$/b" -e 'N;s/\n//;b1' < file.2.csv > file.3.csv
|
||||
$ sed -e 's#"end"#"end"\n#g' < file.3.csv > file.4.csv
|
||||
|
||||
# Exemple pour un autre import:
|
||||
$ cat file2.csv | sed -e 'N; s#_x000D_##g; s#\n##g; s/$CR//g' | tr "\n" " " > file3.csv
|
||||
$ sed -e 's#"endcol"#"endcol"\n#g; s#"endrow"#"endrow"\n#g' < file3.csv > file4.csv
|
||||
$ sed -e 's#^,##g; s#^ ##g' < file4.csv > file5.csv
|
||||
```
|
||||
|
||||
## 2. Insérer les csv dans la base de donnée
|
||||
|
||||
On va insérer chaque feuille csv comme table à part entière d'un nouveau schéma `import`. On aura:
|
||||
- import.choix_personnes
|
||||
- import.personnes
|
||||
- import.choix_periodes
|
||||
- import.periodes
|
||||
|
||||
Pour réaliser cet import, on peut utiliser des outils tels que `pgfutter`, mais celui-ci peut s'avérer capricieux selon le fichier.
|
||||
La meilleure méthode pour moi est de réaliser cette étape en local avec phpstorm, puis d'exporter le schéma `import` avec pg_dump avant de le transférer sur le serveur.
|
||||
|
||||
### 2.a Manipulations dans phpstorm
|
||||
|
||||
- S'il n'existe pas, créer le schéma `import`; s'il existe, s'assurer qu'il ne contient pas de tables ni de données.
|
||||
#### Importer le csv dans la db
|
||||
- ouvre le fichier csv > passe en onglet text > edit as table > set options:
|
||||
- cocher 'first row is header'
|
||||
- 'null value text': undefined (pas de champs null dans la table, mais un texte vide)
|
||||
- then > open table
|
||||
- import to database > set options:
|
||||
- régler target/schema: import
|
||||
- et table: même nom que le csv
|
||||
- DDL: TEXT pour tous les champs
|
||||
- then > import
|
||||
#### Exporter en sql
|
||||
- créer un fichier `<client>-data.sql` vide
|
||||
- depuis chaque table du schéma `import`:
|
||||
- copier le DDL de la table dans le fichier (s'assurer d'ajouter le préfixe `import.` sur chaque requête)
|
||||
- export data > extractor: SQL-insert-multirow > copy to clipboard
|
||||
- coller les données dans `<client>-data.sql`
|
||||
|
||||
### 2.b Avec des fonctions Postgresql
|
||||
|
||||
On peut aussi utiliser la fonction `\copy` de psql pour charger un fichier csv dans les tables temporaires du schéma "import".
|
||||
|
||||
Tout d'abord, les tables du schéma "import" doivent être préparées avec le script `prepare-import.sql`.
|
||||
|
||||
Puis on importe les fichiers csv:
|
||||
|
||||
```bash
|
||||
psql chill-import
|
||||
chill-import=# \copy "import".choix_personnes FROM 'choix_personnes.csv' DELIMITER ',' CSV HEADER
|
||||
chill-import=# \copy "import".personnes FROM 'personnes.csv' DELIMITER ',' CSV HEADER
|
||||
chill-import=# \copy "import".choix_periodes FROM 'choix_periodes.csv' DELIMITER ',' CSV HEADER
|
||||
chill-import=# \copy "import".periodes FROM 'periodes.csv' DELIMITER ',' CSV HEADER
|
||||
chill-import=# \copy "import".choix_localisations FROM 'choix_localisations.csv' DELIMITER ',' CSV HEADER
|
||||
chill-import=# \copy "import".localisations FROM 'localisations.csv' DELIMITER ',' CSV HEADER
|
||||
```
|
||||
|
||||
Enfin, on exporte la base de données en sql:
|
||||
|
||||
```bash
|
||||
pg_dump chill-import --no-owner > <client>-data.sql
|
||||
```
|
||||
|
||||
|
||||
## 3. Import du schéma 'import' sur le serveur (safran)
|
||||
|
||||
- transférer le fichier `<client>-data.sql` sur le serveur (avec scp):
|
||||
```bash
|
||||
$ scp cyclo-data.sql debian@safran:~/data/tmp/
|
||||
```
|
||||
|
||||
- faire une sauvegarde de la base sur laquelle on va réaliser l'insertion
|
||||
```bash
|
||||
debian@safran:~/bin$ bash backup_now_db.sh 5436 cycloprod
|
||||
debian@safran:~/bin$ ls -l dump/ | tail -1
|
||||
-rw-r--r-- 1 postgres postgres 234954230 Mar 15 10:40 20230315-104003_cycloprod.sql
|
||||
```
|
||||
|
||||
ou bien simplement:
|
||||
|
||||
```bash
|
||||
sudo su postgres
|
||||
cd
|
||||
pg_dump -p 5436 laplateformereunionprod > laplateformereunionprod.sql
|
||||
```
|
||||
|
||||
- importer le fichier sql sur la base cible: `$ sudo su postgres -c 'psql -p5436'`
|
||||
```sql
|
||||
postgres=# \c cycloprod
|
||||
You are now connected to database "cycloprod" as user "postgres".
|
||||
|
||||
cycloprod=# \dt import.*
|
||||
Did not find any relation named "import.*".
|
||||
|
||||
cycloprod=# CREATE SCHEMA import;
|
||||
|
||||
-- insertion
|
||||
cycloprod=# \i '/home/debian/data/tmp/cyclo-data.sql'
|
||||
|
||||
-- vérifier que le schéma import est en place
|
||||
cycloprod=# \dt import.*
|
||||
List of relations
|
||||
Schema | Name | Type | Owner
|
||||
--------+-----------------+-------+----------
|
||||
import | choix_periodes | table | postgres
|
||||
import | choix_personnes | table | postgres
|
||||
import | periodes | table | postgres
|
||||
import | personnes | table | postgres
|
||||
(4 rows)
|
||||
```
|
||||
|
||||
ou bien simplement:
|
||||
|
||||
```bash
|
||||
sudo su postgres
|
||||
psql -p 5436 laplateformereunionprod < /tmp/chill-import-lpreunion.sql
|
||||
```
|
||||
|
||||
|
||||
## 4. Exécution du script de migration
|
||||
|
||||
Se fait dans la console postgresql, en tant que user postgres, en étant connecté à la base de donnée cible.
|
||||
|
||||
On joue pas-à-pas les blocs de la section 'Up' du script `sql/import.sql`
|
||||
|
||||
## Tips
|
||||
|
||||
- Dans phpstorm, si on veut renommer le schéma pour ne pas tout mélanger, il vaut mieux faire 'Modify schema', car 'Rename' va faire des remplacements partout
|
||||
|
BIN
canvas/chill_canevas_reprise-de-données.xlsx
Executable file
BIN
canvas/chill_canevas_reprise-de-données.xlsx
Executable file
Binary file not shown.
0
csv/choix_periodes.dist.csv
Normal file → Executable file
0
csv/choix_periodes.dist.csv
Normal file → Executable file
0
csv/choix_personnes.dist.csv
Normal file → Executable file
0
csv/choix_personnes.dist.csv
Normal file → Executable file
0
csv/periodes.dist.csv
Normal file → Executable file
0
csv/periodes.dist.csv
Normal file → Executable file
0
csv/personnes.dist.csv
Normal file → Executable file
0
csv/personnes.dist.csv
Normal file → Executable file
1048
sql/import.sql
Normal file → Executable file
1048
sql/import.sql
Normal file → Executable file
File diff suppressed because it is too large
Load Diff
141
sql/prepare-import.sql
Executable file
141
sql/prepare-import.sql
Executable file
@@ -0,0 +1,141 @@
|
||||
CREATE SCHEMA "import";
|
||||
|
||||
CREATE TABLE "import".choix_personnes (
|
||||
civility varchar(50) NULL,
|
||||
gender varchar(50) NULL,
|
||||
maritalstatus varchar(50) NULL,
|
||||
country varchar(50) NULL,
|
||||
household_composition_type varchar(50) NULL,
|
||||
household_position varchar(50) NULL
|
||||
);
|
||||
|
||||
CREATE TABLE "import".personnes (
|
||||
id varchar(50) NULL,
|
||||
civility varchar(50) NULL,
|
||||
lastname varchar(50) NULL,
|
||||
firstname varchar(50) NULL,
|
||||
gender varchar(50) NULL,
|
||||
gendercomment varchar(50) NULL,
|
||||
nationality varchar(50) NULL,
|
||||
memo varchar(50) NULL,
|
||||
birthdate varchar(50) NULL,
|
||||
place_of_birth varchar(50) NULL,
|
||||
countryofbirth varchar(50) NULL,
|
||||
deathdate varchar(50) NULL,
|
||||
email varchar(50) NULL,
|
||||
phonenumber varchar(50) NULL,
|
||||
mobilenumber varchar(50) NULL,
|
||||
contactinfo varchar(50) NULL,
|
||||
street varchar(50) NULL,
|
||||
extra varchar(50) NULL,
|
||||
streetnumber varchar(50) NULL,
|
||||
postcode varchar(50) NULL,
|
||||
country varchar(50) NULL,
|
||||
validfrom varchar(50) NULL,
|
||||
maritalstatus varchar(50) NULL,
|
||||
maritalstatuscomment varchar(50) NULL,
|
||||
numberofchildren integer NULL,
|
||||
household_composition_type varchar(50) NULL,
|
||||
household_position varchar(50) NULL,
|
||||
household_startdate varchar(50) NULL
|
||||
);
|
||||
|
||||
CREATE TABLE "import".choix_periodes (
|
||||
closingmotive varchar(50) NULL,
|
||||
origin varchar(50) NULL,
|
||||
acp_scopes varchar(50) NULL,
|
||||
job varchar(50) NULL,
|
||||
referrer varchar(50) NULL,
|
||||
parent varchar(50) NULL,
|
||||
enfant varchar(50) NULL,
|
||||
acp_social_issues varchar(50) NULL,
|
||||
work_social_action varchar(50) NULL,
|
||||
street varchar(128) NULL,
|
||||
extra varchar(50) NULL,
|
||||
streetnumber integer NULL,
|
||||
postcode integer NULL,
|
||||
country varchar(50) NULL
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE "import".periodes (
|
||||
id varchar(50) NULL,
|
||||
nom varchar(50) NULL,
|
||||
openingdate varchar(50) NULL,
|
||||
closingdate varchar(50) NULL,
|
||||
closingmotive varchar(50) NULL,
|
||||
origin varchar(50) NULL,
|
||||
remark integer NULL,
|
||||
intensity varchar(50) NULL,
|
||||
referrer varchar(50) NULL,
|
||||
job varchar(50) NULL,
|
||||
acp_scopes varchar(50) NULL,
|
||||
"address" varchar(50) NULL,
|
||||
personlocation varchar(50) NULL,
|
||||
addresslocation varchar(50) NULL,
|
||||
acp_socialissues varchar(50) NULL,
|
||||
work_socialaction varchar(50) NULL,
|
||||
comment1_content varchar(50) NULL,
|
||||
comment2_content varchar(50) NULL,
|
||||
comment3_content varchar(50) NULL,
|
||||
comment4_content varchar(50) NULL,
|
||||
comment5_content varchar(50) NULL
|
||||
);
|
||||
|
||||
CREATE TABLE "import".choix_localisations (
|
||||
title varchar(50) NULL,
|
||||
"addressRequired" varchar(32) NULL,
|
||||
"availableForUsers" boolean DEFAULT TRUE,
|
||||
"contactData" varchar(32) NULL,
|
||||
"defaultFor" varchar(32) NULL,
|
||||
"editableByUsers" boolean DEFAULT TRUE
|
||||
);
|
||||
|
||||
CREATE TABLE "import".localisations (
|
||||
id varchar(50) NULL,
|
||||
locname varchar(50) NULL,
|
||||
phonenumber varchar(50) NULL,
|
||||
phonenumber1 varchar(50) NULL,
|
||||
email varchar(50) NULL,
|
||||
loctype varchar(50) NULL,
|
||||
street varchar(50) NULL,
|
||||
extra varchar(50) NULL,
|
||||
streetnumber varchar(50) NULL,
|
||||
postcode varchar(50) NULL,
|
||||
country varchar(50) NULL
|
||||
);
|
||||
|
||||
CREATE TABLE "import".users (
|
||||
id varchar(50) NULL,
|
||||
"login" varchar(50) NULL,
|
||||
civility varchar(50) NULL,
|
||||
nom varchar(50) NULL,
|
||||
prenom varchar(50) NULL,
|
||||
libl varchar(50) NULL,
|
||||
nom_prenom varchar(50) NULL,
|
||||
phonenumber varchar(50) NULL,
|
||||
email varchar(50) NULL,
|
||||
metier varchar(50) NULL
|
||||
);
|
||||
|
||||
CREATE TABLE "import".tiers (
|
||||
ID INT PRIMARY KEY,
|
||||
CATEGORIE VARCHAR(255),
|
||||
SECTEUR_AS VARCHAR(255),
|
||||
COMMUNE VARCHAR(255),
|
||||
NOM VARCHAR(255),
|
||||
PHONENUMBER VARCHAR(20),
|
||||
PHONENUMBER_2 VARCHAR(20),
|
||||
EMAIL VARCHAR(255),
|
||||
PERSONNE_NOM VARCHAR(255),
|
||||
PERSONNE_prenom VARCHAR(255),
|
||||
PERSONNE_CIVILITE VARCHAR(20),
|
||||
adresse TEXT,
|
||||
CP VARCHAR(10),
|
||||
TIERS_PARENT VARCHAR(255),
|
||||
TIERS_PHYSIQUE_VS_MORALE VARCHAR(50),
|
||||
POINT_DE_CONTACT_SUR_LE_TERRITOIRE TEXT,
|
||||
COORDONNEES TEXT,
|
||||
HORAIRES TEXT,
|
||||
OBSERVATIONS TEXT
|
||||
);
|
14
third_party/README.md
vendored
Executable file
14
third_party/README.md
vendored
Executable file
@@ -0,0 +1,14 @@
|
||||
Import Third parties into Chill from the canvas
|
||||
===============================================
|
||||
|
||||
This folder contains a single Python script that reads a csv file with Third party and print to the console some SQL to execute on the database.
|
||||
|
||||
The csv file should be simply exported from the sheet "Tiers" from the xls canvas.
|
||||
|
||||
Then, you can run the python script with adapting the following:
|
||||
|
||||
- name of the csv file to open
|
||||
- change the mapping of third party categories, civilities and third party kinds according to your Chill database
|
||||
|
||||
|
||||
So far, the addresses are not treated.
|
118
third_party/import_third_party_to_sql.py
vendored
Executable file
118
third_party/import_third_party_to_sql.py
vendored
Executable file
@@ -0,0 +1,118 @@
|
||||
import csv
|
||||
|
||||
print_to_screen = "INSERT INTO chill_3party.third_party (id, civility_id, name, firstname, name_company, acronym, kind, parent_id, profession, email, telephone, comment, contact_data_anonymous, active, created_at) VALUES"
|
||||
|
||||
|
||||
def get_parent_id(arg):
|
||||
return int(arg) + 1000 if arg else "NULL"
|
||||
|
||||
|
||||
def get_third_party_civility(arg):
|
||||
if arg == "Madame":
|
||||
return 1
|
||||
elif arg == "Monsieur":
|
||||
return 2
|
||||
elif arg == "Docteur":
|
||||
return 3
|
||||
else:
|
||||
return "NULL"
|
||||
|
||||
|
||||
def get_third_party_kind(arg):
|
||||
if arg == "Tiers institutionnel":
|
||||
return "company"
|
||||
elif arg == "Personne de contact":
|
||||
return "child"
|
||||
elif arg == "Personne morale":
|
||||
return "contact"
|
||||
else:
|
||||
return "company"
|
||||
|
||||
|
||||
def make_bool_with_default_false(arg):
|
||||
if arg == "oui":
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def make_telephone(arg):
|
||||
if arg:
|
||||
return f"'+32{arg}'"
|
||||
else:
|
||||
return "NULL"
|
||||
|
||||
|
||||
with open("HalleDeHan_tiers.csv", newline="") as csv_file:
|
||||
csv_reader = csv.reader(csv_file)
|
||||
|
||||
for skip in range(7):
|
||||
next(csv_file)
|
||||
|
||||
for row in csv_reader:
|
||||
third_party_id = int(row[0]) + 1000
|
||||
civility = get_third_party_civility(row[1])
|
||||
name = row[2]
|
||||
firstname = row[3]
|
||||
name_company = row[4]
|
||||
acronym = row[5]
|
||||
kind = get_third_party_kind(row[6])
|
||||
parent_id = get_parent_id(row[7])
|
||||
# row[8] is the name of the parent in the csv
|
||||
profession = row[9]
|
||||
# category = row[10] # category -> see below
|
||||
email = row[11]
|
||||
telephone = make_telephone(row[12])
|
||||
# street = row[13] # TODO address
|
||||
# extra = row[14]
|
||||
# streetnumber = row[15]
|
||||
# postcode = row[16]
|
||||
# country = row[17]
|
||||
# validfrom = row[18]
|
||||
comment = row[19]
|
||||
contact_data_anonymous = make_bool_with_default_false(row[20])
|
||||
print_to_screen += f"({third_party_id}, {civility}, '{name}', '{firstname}', '{name_company}', '{acronym}', '{kind}', {parent_id}, '{profession}', '{email}', {telephone}, '{comment}', {contact_data_anonymous}, True, NOW()),"
|
||||
|
||||
print_to_screen = print_to_screen[:-1]
|
||||
|
||||
print(print_to_screen)
|
||||
|
||||
## categories
|
||||
|
||||
print_to_screen_cat = (
|
||||
"INSERT INTO chill_3party.thirdparty_category (thirdparty_id, category_id) VALUES"
|
||||
)
|
||||
|
||||
|
||||
def get_third_party_category(arg):
|
||||
if arg == "Stage vente":
|
||||
return 2
|
||||
elif arg == "Stage reassort":
|
||||
return 8
|
||||
elif arg == "Stage social / educ":
|
||||
return 9
|
||||
elif arg == "Stage logistique":
|
||||
return 10
|
||||
elif arg == "Stage bureautique":
|
||||
return 11
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
with open("HalleDeHan_tiers.csv", newline="") as csv_file:
|
||||
csv_reader = csv.reader(csv_file)
|
||||
|
||||
for skip in range(7):
|
||||
next(csv_file)
|
||||
|
||||
for row in csv_reader:
|
||||
third_party_id = int(row[0]) + 1000
|
||||
category_id = get_third_party_category(row[10])
|
||||
if category_id:
|
||||
print_to_screen_cat += f"({third_party_id}, {category_id}),"
|
||||
|
||||
print_to_screen_cat = print_to_screen_cat[:-1]
|
||||
|
||||
print(print_to_screen_cat)
|
||||
|
||||
# TODO addresses
|
Reference in New Issue
Block a user