-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
school dataset - estabCode missing (#995)
* estabCode missing from previous seed * fix school seeding. set LOG_LEVEL default to 'info' * update migrator error handling, remove obsolete seeding migrations
- Loading branch information
1 parent
b696719
commit baa027b
Showing
9 changed files
with
18,383 additions
and
36,890 deletions.
There are no files selected for viewing
File renamed without changes.
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,90 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict' | ||
|
||
require('dotenv').config() | ||
const winston = require('winston') | ||
const upnService = require('../services/upn.service') | ||
winston.level = 'info' | ||
const moment = require('moment') | ||
|
||
const poolService = require('../services/data-access/sql.pool.service') | ||
const sqlService = require('../services/data-access/sql.service') | ||
const pupilCountPerSchool = 30 | ||
|
||
const schoolIdParam = process.argv[2] | ||
let schoolId | ||
|
||
schoolId = parseInt(schoolIdParam) | ||
if (isNaN(schoolId)) { | ||
console.error('target school id (int) is a required argument') | ||
process.exitCode = -1 | ||
process.exit() | ||
} | ||
|
||
async function main() { | ||
try { | ||
const school = await sqlService.query(`SELECT | ||
id, | ||
estabCode, | ||
leaCode, | ||
[name] | ||
from [mtc_admin].[school] WHERE id=${schoolId} AND leaCode IS NOT NULL AND estabCode IS NOT NULL`) | ||
console.log(`Generating ${pupilCountPerSchool} pupils for ${school.name}`) | ||
await insertPupils(school, pupilCountPerSchool) | ||
} catch (error) { | ||
console.error(error.message) | ||
throw error | ||
} | ||
} | ||
|
||
main() | ||
.then(() => { | ||
poolService.drain() | ||
}) | ||
.catch(e => { | ||
console.warn(e) | ||
poolService.drain() | ||
}) | ||
|
||
async function insertPupils(school, count) { | ||
const insert = `INSERT INTO [mtc_admin].[pupil] ( | ||
dateOfBirth, | ||
foreName, | ||
gender, | ||
lastName, | ||
school_id, | ||
upn | ||
) VALUES` | ||
const pupilData = [] | ||
for (let i = 0; i < count; i++) { | ||
pupilData.push([ | ||
`( '${randomDob()}'`, | ||
`'Pupil'`, | ||
`'M'`, | ||
`'${count.toString()}'`, | ||
school.id, | ||
`'${genUPN(school.leaCode, school.estabCode, i)}')` | ||
].join(' , ')) | ||
} | ||
const sql = `${insert} ${pupilData.join(', \n')}` | ||
return sqlService.modify(sql) | ||
} | ||
|
||
function randomDob() { | ||
const rnd = Math.floor(Math.random() * (365 * 2) + 1) | ||
const dob = moment().utc().subtract(9, 'years').subtract(rnd, 'days') | ||
dob.hours(0).minute(0).second(0) | ||
return dob.toISOString() | ||
} | ||
|
||
function genUPN(leaCode, estabCode, serial) { | ||
try { | ||
const upn = '' + leaCode.toString() + estabCode + (new Date().getFullYear().toString().substr(-2)) + | ||
serial.toString().padStart(3, '0') | ||
const checkLetter = upnService.calculateCheckLetter(upn) | ||
return checkLetter + upn | ||
} catch (error) { | ||
console.error(`Failed on: leaCode [${leaCode}] estab: [${estabCode}] serial: [${serial}]`) | ||
} | ||
} |
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
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
1 change: 0 additions & 1 deletion
1
admin/data/sql/migrations/20190128160154.do.delete-old-schools.sql
This file was deleted.
Oops, something went wrong.
18,599 changes: 0 additions & 18,599 deletions
18,599
admin/data/sql/migrations/20190128160154.undo.delete-old-schools.sql
This file was deleted.
Oops, something went wrong.
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
18,284 changes: 0 additions & 18,284 deletions
18,284
admin/data/sql/seeds/20190128145958.custom.school-data-jan-2019.sql
This file was deleted.
Oops, something went wrong.
18,284 changes: 18,284 additions & 0 deletions
18,284
admin/data/sql/seeds/20190129102415.custom.school-data-jan-2019.sql
Large diffs are not rendered by default.
Oops, something went wrong.