Skip to content

Commit

Permalink
Merge pull request #385 from BaseAdresseNationale/antoineludeau/add-s…
Browse files Browse the repository at this point in the history
…eed-mechanism

[Init DB with data] - Add seed mechanism
  • Loading branch information
antoineludeau authored May 14, 2024
2 parents 25b2dae + b4f179d commit e0f8c0e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
31 changes: 31 additions & 0 deletions db-migrations/seeders/20240417084446-init-districts.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const districts = [
{
id: '55c7f562-4f6b-4dac-a685-19efd9afe42c',
labels: [{isoCode: 'fra', value: 'Thourotte'}],
updateDate: '2023-01-01',
meta: {insee: {cog: '60636'}}
},
]

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface) {
await queryInterface.sequelize.query(`
INSERT INTO ban.district (id, labels, "updateDate", meta)
VALUES
${districts.map(({id, labels, updateDate, meta}) => (`
(
'${id}',
(ARRAY['${labels.map(label => JSON.stringify(label)).join(',')}'])::jsonb[],
'${updateDate}',
'${JSON.stringify(meta)}'::jsonb
)`)).join(',')}
;`)
},

async down(queryInterface) {
return queryInterface.bulkDelete({tableName: 'district', schema: 'ban'}, null, {})
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"toolbox:snapshot": "node toolbox.dev/scripts/db-snapshot.js",
"toolbox:compare": "node toolbox.dev/scripts/compare-data-from-db-snapshot-and-current-db",
"migrate:up": "npx sequelize-cli db:migrate",
"migrate:undo": "npx sequelize-cli db:migrate:undo"
"migrate:undo": "npx sequelize-cli db:migrate:undo",
"seed:up": "npx sequelize-cli db:seed:all",
"seed:undo": "npx sequelize-cli db:seed:undo"
},
"dependencies": {
"@ban-team/fantoir": "^0.15.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const main = async () => {
const filePath = path.join(snapshotFolder, file)
const fileData = JSON.parse(await fs.readFile(filePath))

const dbData = DISTRICT_TO_SNAPSHOT
const dbData = DISTRICT_TO_SNAPSHOT.length > 0
? await collection.find({codeCommune: {$in: DISTRICT_TO_SNAPSHOT}}).toArray()
: await collection.find().toArray()

Expand Down
4 changes: 2 additions & 2 deletions toolbox.dev/scripts/db-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const __dirname = dirname(__filename)
const SNAPSHOT_FOLDER_NAME = 'ban-db-snapshot'

const collectionNotToSnapshot = new Set(
['metrics', 'sources_adresses', 'sources_voies', 'sources_communes', 'sources_parts', 'address_test', 'commonToponym_test', 'district_test', 'job_status']
['metrics', 'sources_adresses', 'sources_voies', 'sources_communes', 'sources_parts', 'address_test', 'commonToponym_test', 'district_test', 'job_status', 'pseudo_codes_voies']
)

const main = async () => {
Expand All @@ -36,7 +36,7 @@ const main = async () => {
}

const collection = mongo.db.collection(collectionName)
const data = DISTRICT_TO_SNAPSHOT
const data = DISTRICT_TO_SNAPSHOT.length > 0
? await collection.find({codeCommune: {$in: DISTRICT_TO_SNAPSHOT}}).toArray()
: await collection.find().toArray()
const filePath = path.join(initDir, `${collectionName}.json`)
Expand Down

0 comments on commit e0f8c0e

Please sign in to comment.