From ba5645b43c5cbb4f30e08b317e29e8d8ab4bb527 Mon Sep 17 00:00:00 2001 From: brennanwebster Date: Thu, 5 Dec 2024 10:29:00 -0800 Subject: [PATCH] add CRD to ipmas and NRRM to regional districts --- database/src/migrations/0030_add_crd_ipmas.ts | 28 +++++++++++++++++++ .../0031_add_nrrm_to_regional_districts.ts | 28 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 database/src/migrations/0030_add_crd_ipmas.ts create mode 100644 database/src/migrations/0031_add_nrrm_to_regional_districts.ts diff --git a/database/src/migrations/0030_add_crd_ipmas.ts b/database/src/migrations/0030_add_crd_ipmas.ts new file mode 100644 index 000000000..0a49e8224 --- /dev/null +++ b/database/src/migrations/0030_add_crd_ipmas.ts @@ -0,0 +1,28 @@ +import { Knex } from 'knex'; +import axios from 'axios'; +import { ungzip } from 'node-gzip'; + +export async function up(knex: Knex): Promise { + try { + const url = 'https://nrs.objectstore.gov.bc.ca/seeds/CRD_IPMAS.sql.gz'; + const { data } = await axios.get(url, { responseType: 'arraybuffer' }); + const sql = await ungzip(data); + + await knex.raw(sql.toString()); + } catch (e) { + console.error('Failed to insert IPMAS data:', e); + throw e; + } +} + +export async function down(knex: Knex): Promise { + try { + await knex.raw(` + DELETE FROM public.invasive_plant_management_areas + WHERE agency_cd = 'CRD'; + `); + } catch (e) { + console.error('Failed to rollback IPMAS data:', e); + throw e; + } +} diff --git a/database/src/migrations/0031_add_nrrm_to_regional_districts.ts b/database/src/migrations/0031_add_nrrm_to_regional_districts.ts new file mode 100644 index 000000000..8ba474e9d --- /dev/null +++ b/database/src/migrations/0031_add_nrrm_to_regional_districts.ts @@ -0,0 +1,28 @@ +import { Knex } from 'knex'; +import axios from 'axios'; +import { ungzip } from 'node-gzip'; + +export async function up(knex: Knex): Promise { + try { + const url = 'https://nrs.objectstore.gov.bc.ca/seeds/NRRM.sql.gz'; + const { data } = await axios.get(url, { responseType: 'arraybuffer' }); + const sql = await ungzip(data); + + await knex.raw(sql.toString()); + } catch (e) { + console.error('Failed to insert NRRM data:', e); + throw e; + } +} + +export async function down(knex: Knex): Promise { + try { + await knex.raw(` + DELETE FROM public.regional_districts + WHERE agency_cd = 'NRRM'; + `); + } catch (e) { + console.error('Failed to rollback NRRM data:', e); + throw e; + } +}