-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #436 from BaseAdresseNationale/Jugurtha/Initialise…
…r-les-vues-dadresses-et-de-toponymes-communs Initialiser les vues d adresses et de toponymes communs
- Loading branch information
Showing
3 changed files
with
91 additions
and
43 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
db-migrations/migrations/20240618155810-init-common-toponym-view.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict' | ||
|
||
const {POSTGRES_BAN_USER} = process.env | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface) { | ||
const addressBboxBuffer = 200 | ||
const bboxBuffer = 100 | ||
try { | ||
// Execute the view creation | ||
await queryInterface.sequelize.query(` | ||
CREATE VIEW ban."common_toponym_view" AS | ||
SELECT | ||
CT.id, CT."districtID", CT.labels, CT.geometry, CT."updateDate", CT.meta, CT.range_validity, CT."isActive", | ||
ST_Centroid(ST_Collect(ST_SetSRID(ST_GeomFromGeoJSON((A.positions[1])->'geometry'), 4326))) AS centroid, | ||
ST_Transform(ST_Buffer(ST_Transform(ST_Envelope(ST_Collect(ST_SetSRID(ST_GeomFromGeoJSON((A.positions[1])->'geometry'), 4326))), 2154), ${addressBboxBuffer}, 'join=mitre endcap=square'), 4326) AS "addressBbox", | ||
ST_Transform(ST_Buffer(ST_Transform(ST_Envelope(ST_SetSRID(ST_GeomFromGeoJSON(CT.geometry), 4326)), 2154), ${bboxBuffer}, 'join=mitre endcap=square'), 4326) AS "bbox", | ||
COUNT(A.id) AS "addressCount", | ||
COUNT(DISTINCT CASE WHEN A.certified = true THEN A.id ELSE NULL END) AS "certifiedAddressCount" | ||
FROM | ||
ban.common_toponym AS CT | ||
LEFT JOIN | ||
ban.address AS A | ||
ON | ||
(CT.id = A."mainCommonToponymID" | ||
OR CT.id = ANY(A."secondaryCommonToponymIDs")) AND A."isActive" = true | ||
WHERE CT."isActive" = true | ||
GROUP BY CT.id | ||
ORDER BY CT.id ASC ` | ||
) | ||
// Grant permissions to ban user | ||
await queryInterface.sequelize.query(`GRANT SELECT ON ban."common_toponym_view" TO "${POSTGRES_BAN_USER}";`) | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
}, | ||
|
||
async down(queryInterface) { | ||
try { | ||
// Drop the view if it exists | ||
await queryInterface.sequelize.query('DROP VIEW IF EXISTS ban."common_toponym_view" ;') | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
db-migrations/migrations/20240618155829-init-address-view.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict' | ||
|
||
const {POSTGRES_BAN_USER} = process.env | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface) { | ||
const bboxBuffer = 50 | ||
try { | ||
// Create the address_view | ||
await queryInterface.sequelize.query(` | ||
CREATE VIEW ban."address_view" AS | ||
SELECT | ||
A.*, | ||
ST_Transform(ST_Buffer(ST_Transform(ST_Envelope(ST_SetSRID(ST_GeomFromGeoJSON((A.positions[1])->'geometry'), 4326)), 2154), ${bboxBuffer}, 'join=mitre endcap=square'), 4326) AS bbox | ||
FROM | ||
ban.address AS A | ||
WHERE A."isActive" = true | ||
ORDER BY A.id ASC | ||
`) | ||
await queryInterface.sequelize.query(`GRANT SELECT ON ban."address_view" TO "${POSTGRES_BAN_USER}";`) | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
}, | ||
|
||
async down(queryInterface) { | ||
try { | ||
// Drop the address_view if it exists | ||
await queryInterface.sequelize.query('DROP VIEW IF EXISTS ban."address_view";') | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters