-
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 #4140 from beyondessential/dev
sima create instance
- Loading branch information
Showing
3 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
packages/database/src/migrations/20220901042432-AddFacilitiesPenfaa-modifies-data.js
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,137 @@ | ||
'use strict'; | ||
|
||
const { generateId, nameToId } = require('../utilities'); | ||
|
||
var dbm; | ||
var type; | ||
var seed; | ||
|
||
/** | ||
* We receive the dbmigrate dependency from dbmigrate initially. | ||
* This enables us to not have to rely on NODE_PATH. | ||
*/ | ||
exports.setup = function (options, seedLink) { | ||
dbm = options.dbmigrate; | ||
type = dbm.dataType; | ||
seed = seedLink; | ||
}; | ||
|
||
// Current hierarchy | ||
// country | ||
// |- district | ||
// |- sub_district | ||
// |- ... | ||
|
||
// New hierarchy | ||
// country | ||
// |- district | ||
// |- facility <-- add facilities to alt. hierarchy. All facilities already exist. | ||
// |- sub_district | ||
// |- ... | ||
|
||
const HIERARCHY_CODE = 'penfaa_samoa'; | ||
|
||
// a subset of facilities in Samoa | ||
const FACILITIES = [ | ||
// parent_code code | ||
['WS_Upolu', 'WS_001'], | ||
['WS_Savaii', 'WS_002'], | ||
['WS_Upolu', 'WS_003'], | ||
['WS_Upolu', 'WS_004'], | ||
['WS_Upolu', 'WS_005'], | ||
['WS_Savaii', 'WS_006'], | ||
['WS_Upolu', 'WS_007'], | ||
['WS_Upolu', 'WS_008'], | ||
['WS_Savaii', 'WS_009'], | ||
['WS_Savaii', 'WS_011'], | ||
['WS_Savaii', 'WS_012'], | ||
['WS_Upolu', 'WS_014'], | ||
]; | ||
|
||
const SUB_DISTRICTS = [ | ||
// parent_code code | ||
['WS_004', 'WS_sd01'], | ||
['WS_004', 'WS_sd02'], | ||
['WS_004', 'WS_sd03'], | ||
['WS_004', 'WS_sd04'], | ||
['WS_001', 'WS_sd05'], | ||
['WS_011', 'WS_sd06'], | ||
['WS_003', 'WS_sd07'], | ||
['WS_003', 'WS_sd08'], | ||
['WS_005', 'WS_sd09'], | ||
['WS_005', 'WS_sd10'], | ||
['WS_006', 'WS_sd11'], | ||
['WS_006', 'WS_sd12'], | ||
['WS_006', 'WS_sd13'], | ||
['WS_006', 'WS_sd14'], | ||
['WS_006', 'WS_sd15'], | ||
['WS_007', 'WS_sd16'], | ||
['WS_007', 'WS_sd17'], | ||
['WS_011', 'WS_sd18'], | ||
['WS_014', 'WS_sd19'], | ||
['WS_014', 'WS_sd20'], | ||
['WS_014', 'WS_sd21'], | ||
['WS_014', 'WS_sd22'], | ||
['WS_001', 'WS_sd23'], | ||
['WS_009', 'WS_sd24'], | ||
['WS_009', 'WS_sd25'], | ||
['WS_009', 'WS_sd26'], | ||
['WS_009', 'WS_sd27'], | ||
['WS_009', 'WS_sd28'], | ||
['WS_008', 'WS_sd29'], | ||
['WS_003', 'WS_sd30'], | ||
['WS_003', 'WS_sd31'], | ||
['WS_002', 'WS_sd32'], | ||
['WS_012', 'WS_sd33'], | ||
['WS_012', 'WS_sd34'], | ||
['WS_008', 'WS_sd35'], | ||
['WS_008', 'WS_sd36'], | ||
['WS_004', 'WS_sd37'], | ||
['WS_004', 'WS_sd38'], | ||
['WS_004', 'WS_sd39'], | ||
['WS_004', 'WS_sd40'], | ||
['WS_002', 'WS_sd41'], | ||
['WS_002', 'WS_sd42'], | ||
['WS_012', 'WS_sd43'], | ||
['WS_008', 'WS_sd44'], | ||
['WS_005', 'WS_sd45'], | ||
['WS_014', 'WS_sd46'], | ||
['WS_014', 'WS_sd47'], | ||
['WS_014', 'WS_sd48'], | ||
['WS_014', 'WS_sd49'], | ||
['WS_011', 'WS_sd50'], | ||
['WS_011', 'WS_sd51'], | ||
]; | ||
|
||
exports.up = async function (db) { | ||
const entityHierarchyId = await nameToId(db, 'entity_hierarchy', HIERARCHY_CODE); | ||
|
||
for (const [parentSubDistrictCode, facilityCode] of FACILITIES) { | ||
await db.runSql(` | ||
INSERT INTO entity_relation (id, parent_id, child_id, entity_hierarchy_id) | ||
VALUES ( | ||
'${generateId()}', | ||
(SELECT id FROM entity WHERE code = '${parentSubDistrictCode}'), | ||
(SELECT id FROM entity WHERE code = '${facilityCode}'), | ||
'${entityHierarchyId}' | ||
) | ||
`); | ||
} | ||
|
||
for (const [parentFacilityCode, subDistrictCode] of SUB_DISTRICTS) { | ||
await db.runSql(` | ||
UPDATE entity_relation | ||
SET parent_id = (SELECT id FROM entity WHERE code = '${parentFacilityCode}') | ||
WHERE child_id = (SELECT id FROM entity WHERE code = '${subDistrictCode}') | ||
AND entity_hierarchy_id = '${entityHierarchyId}' | ||
`); | ||
} | ||
}; | ||
|
||
exports.down = async function (db) { | ||
return null; | ||
}; | ||
|
||
exports._meta = { | ||
version: 1, | ||
}; |
103 changes: 103 additions & 0 deletions
103
packages/database/src/migrations/20220901060328-AddProjectTuvaluEhealth-modifies-data.js
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,103 @@ | ||
'use strict'; | ||
|
||
import { insertObject, codeToId, generateId } from '../utilities'; | ||
|
||
var dbm; | ||
var type; | ||
var seed; | ||
|
||
/** | ||
* We receive the dbmigrate dependency from dbmigrate initially. | ||
* This enables us to not have to rely on NODE_PATH. | ||
*/ | ||
exports.setup = function (options, seedLink) { | ||
dbm = options.dbmigrate; | ||
type = dbm.dataType; | ||
seed = seedLink; | ||
}; | ||
|
||
const PROJECT_NAME = 'Tuvalu eHealth'; | ||
const PROJECT_CODE = 'ehealth_tuvalu'; | ||
const DASHBOARD_CODE = 'TV_COVID-19'; | ||
const DASHBOARD_NAME = 'COVID-19 Vaccination'; | ||
const PROJECT = { | ||
code: PROJECT_CODE, | ||
description: 'Aggregate health data for Tuvalu', | ||
sort_order: 8, | ||
image_url: 'https://tupaia.s3.ap-southeast-2.amazonaws.com/uploads/tuvalu-ehealth-image.png', | ||
default_measure: '126', | ||
dashboard_group_name: DASHBOARD_NAME, | ||
permission_groups: '{Tuvalu eHealth Admin,Tuvalu eHealth,Tuvalu COVID-19}', | ||
logo_url: 'https://tupaia.s3.ap-southeast-2.amazonaws.com/uploads/tuvalu-ehealth-logo.png', | ||
}; | ||
const ENTITY = { | ||
code: PROJECT_CODE, | ||
name: PROJECT_NAME, | ||
type: 'project', | ||
}; | ||
|
||
const addDashboard = async db => { | ||
await insertObject(db, 'dashboard', { | ||
id: generateId(), | ||
code: DASHBOARD_CODE, | ||
name: DASHBOARD_NAME, | ||
root_entity_code: PROJECT_CODE, | ||
}); | ||
}; | ||
|
||
const addEntity = async db => { | ||
const id = generateId(); | ||
const parentId = await codeToId(db, 'entity', 'World'); | ||
await insertObject(db, 'entity', { | ||
id, | ||
parent_id: parentId, | ||
...ENTITY, | ||
}); | ||
}; | ||
|
||
const addEntityHierarchy = async db => { | ||
await insertObject(db, 'entity_hierarchy', { | ||
id: generateId(), | ||
name: PROJECT_CODE, | ||
canonical_types: '{country,district,village}', | ||
}); | ||
}; | ||
|
||
const hierarchyNameToId = async (db, name) => { | ||
const record = await db.runSql(`SELECT id FROM entity_hierarchy WHERE name = '${name}'`); | ||
return record.rows[0] && record.rows[0].id; | ||
}; | ||
|
||
const addEntityRelation = async db => { | ||
await insertObject(db, 'entity_relation', { | ||
id: generateId(), | ||
parent_id: await codeToId(db, 'entity', PROJECT_CODE), | ||
child_id: await codeToId(db, 'entity', 'TV'), | ||
entity_hierarchy_id: await hierarchyNameToId(db, PROJECT_CODE), | ||
}); | ||
}; | ||
|
||
const addProject = async db => { | ||
await insertObject(db, 'project', { | ||
id: generateId(), | ||
entity_id: await codeToId(db, 'entity', PROJECT_CODE), | ||
entity_hierarchy_id: await hierarchyNameToId(db, PROJECT_CODE), | ||
...PROJECT, | ||
}); | ||
}; | ||
|
||
exports.up = async function (db) { | ||
await addEntity(db); | ||
await addDashboard(db); | ||
await addEntityHierarchy(db); | ||
await addEntityRelation(db); | ||
await addProject(db); | ||
}; | ||
|
||
exports.down = async function (db) { | ||
return null; | ||
}; | ||
|
||
exports._meta = { | ||
version: 1, | ||
}; |
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 |
---|---|---|
|
@@ -21,6 +21,7 @@ export const PUBLIC_COUNTRY_CODES = [ | |
'TK', | ||
'TL', | ||
'TO', | ||
'TV', | ||
'VE', | ||
'VU', | ||
'WS', | ||
|