diff --git a/insertions/README.2.md b/insertions/README.2.md new file mode 100644 index 0000000000000000000000000000000000000000..4f2faf51e904e25e2aec3c2838d8fda758b056af --- /dev/null +++ b/insertions/README.2.md @@ -0,0 +1,24 @@ +Run containers +```shell +sudo docker compose up +``` + +Create sql insert file +```shell +node script-2.mjs > sql-file.sql +``` + +Copy the sql file in the container of postgres +```shell +sudo docker cp ./sql-file.sql :/sql-file.sql +``` + +Go inside your postgres container +```shell +sudo docker exec -it psql -U app +``` + +Execute the sql insert file +```shell +\i ./sql-file.sql +``` diff --git a/insertions/README.md b/insertions/README.md index 6b164d07bfc8f9959c87c64cec7f3b7a8962fb8c..356980740d979effb876947f216002dbdccfc80c 100644 --- a/insertions/README.md +++ b/insertions/README.md @@ -1,24 +1,24 @@ -Create test database +Run containers ```shell -docker run --name postgres -e POSTGRES_PASSWORD=pwd -p 5000:5432 -d postgres:15-alpine +sudo docker compose up ``` Create sql insert file ```shell -node script.mjs > +node script-2.mjs > sql-file.sql ``` -Copy the sql file in the container +Copy the sql file in the container of postgres ```shell -docker cp ./ postgres:/ +sudo docker cp ./sql-file.sql :/sql-file.sql ``` -Go inside your postgres container +Go inside your postgres container ```shell -docker exec -it postgres psql -U postgres +sudo docker exec -it psql -U app ``` Execute the sql insert file ```shell -\i ./ +\i ./sql-file.sql ``` diff --git a/insertions/script-2.mjs b/insertions/script-2.mjs new file mode 100644 index 0000000000000000000000000000000000000000..abf296dde3c46983aa254bf1cfa00b45a4b736d8 --- /dev/null +++ b/insertions/script-2.mjs @@ -0,0 +1,139 @@ +import { createReadStream,statSync } from 'node:fs'; +import { createInterface } from 'node:readline'; + +const matches = { + "01": "Auvergne-Rhône-Alpes", + "02": "Hauts-de-France", + "03": "Auvergne-Rhône-Alpes", + "04": "Provence-Alpes-Côte d'Azur", + "05": "Provence-Alpes-Côte d'Azur", + "06": "Provence-Alpes-Côte d'Azur", + "07": "Auvergne-Rhône-Alpes", + "08": "Grand Est", + "09": "Occitanie", + "10": "Grand Est", + "11": "Occitanie", + "12": "Occitanie", + "13": "Provence-Alpes-Côte d'Azur", + "14": "Normandie", + "15": "Auvergne-Rhône-Alpes", + "16": "Nouvelle-Aquitaine", + "17": "Nouvelle-Aquitaine", + "18": "Centre-Val de Loire", + "19": "Nouvelle-Aquitaine", + "2A": "Corse", + "2B": "Corse", + "21": "Bourgogne-Franche-Comté", + "22": "Bretagne", + "23": "Nouvelle-Aquitaine", + "24": "Nouvelle-Aquitaine", + "25": "Bourgogne-Franche-Comté", + "26": "Auvergne-Rhône-Alpes", + "27": "Normandie", + "28": "Centre-Val de Loire", + "29": "Bretagne", + "30": "Occitanie", + "31": "Occitanie", + "32": "Occitanie", + "33": "Nouvelle-Aquitaine", + "34": "Occitanie", + "35": "Bretagne", + "36": "Centre-Val de Loire", + "37": "Centre-Val de Loire", + "38": "Auvergne-Rhône-Alpes", + "39": "Bourgogne-Franche-Comté", + "40": "Nouvelle-Aquitaine", + "41": "Centre-Val de Loire", + "42": "Auvergne-Rhône-Alpes", + "43": "Auvergne-Rhône-Alpes", + "44": "Pays de la Loire", + "45": "Centre-Val de Loire", + "46": "Occitanie", + "47": "Nouvelle-Aquitaine", + "48": "Occitanie", + "49": "Pays de la Loire", + "50": "Normandie", + "51": "Grand Est", + "52": "Grand Est", + "53": "Pays de la Loire", + "54": "Grand Est", + "55": "Grand Est", + "56": "Bretagne", + "57": "Grand Est", + "58": "Bourgogne-Franche-Comté", + "59": "Hauts-de-France", + "60": "Hauts-de-France", + "61": "Normandie", + "62": "Hauts-de-France", + "63": "Auvergne-Rhône-Alpes", + "64": "Nouvelle-Aquitaine", + "65": "Occitanie", + "66": "Occitanie", + "67": "Grand Est", + "68": "Grand Est", + "69": "Auvergne-Rhône-Alpes", + "70": "Bourgogne-Franche-Comté", + "71": "Bourgogne-Franche-Comté", + "72": "Pays de la Loire", + "73": "Auvergne-Rhône-Alpes", + "74": "Auvergne-Rhône-Alpes", + "75": "Île-de-France", + "76": "Normandie", + "77": "Île-de-France", + "78": "Île-de-France", + "79": "Nouvelle-Aquitaine", + "80": "Hauts-de-France", + "81": "Occitanie", + "82": "Occitanie", + "83": "Provence-Alpes-Côte d'Azur", + "84": "Provence-Alpes-Côte d'Azur", + "85": "Pays de la Loire", + "86": "Nouvelle-Aquitaine", + "87": "Nouvelle-Aquitaine", + "88": "Grand Est", + "89": "Bourgogne-Franche-Comté", + "90": "Bourgogne-Franche-Comté", + "91": "Île-de-France", + "92": "Île-de-France", + "93": "Île-de-France", + "94": "Île-de-France", + "95": "Île-de-France", + "971": "Guadeloupe", + "972": "Martinique", + "973": "Guyane", + "974": "La Réunion", + "976": "Mayotte" +}; + +const argv = process.argv.slice(2); +if (argv.length !== 1) { + console.error(`Usage: node script.mjs `); + process.exit(1); +} + +const file = argv[0]; +const fileSize = statSync(file).size; + +const readStream = createReadStream(file, { encoding: 'utf8' }); +const rl = createInterface({ + input: readStream, + crlfDelay: Infinity, +}); + +let lines = []; +let idx= 0; +rl.on('line', (line) => { + if (idx !== 0) { + const data = line.split("|"); + const date = data[8].split("/").reverse().join("-"); + if (data[9] === "Vente" && data[10] !== "" && data[38] !== "" && (data[36] === "Maison" || data[36] === "Appartement")) { + lines.push(`('${date}', ${data[10].replace(",", ".")}, '${matches[data[18]].replace("'", "''")}', ${data[38]})`); + } + } + ++idx; +}); + +rl.on('close', () => { + const str = `INSERT INTO vente(date, value, region, surface) VALUES\n${lines.join(",\n")};`; + console.log(str); +});