Skip to content

Commit

Permalink
school dataset - estabCode missing (#995)
Browse files Browse the repository at this point in the history
* 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
GuyHarwood authored Jan 29, 2019
1 parent b696719 commit baa027b
Show file tree
Hide file tree
Showing 9 changed files with 18,383 additions and 36,890 deletions.
File renamed without changes.
90 changes: 90 additions & 0 deletions admin/bin/generate-pupil-data-for-one-school.js
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}]`)
}
}
2 changes: 1 addition & 1 deletion admin/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = {
}
},
Logging: {
LogLevel: process.env.LOG_LEVEL || 'error',
LogLevel: process.env.LOG_LEVEL || 'info',
LogDna: {
key: process.env.LOGDNA_API_KEY,
hostname: `${os.hostname()}:${process.pid}`,
Expand Down
8 changes: 4 additions & 4 deletions admin/data/sql/migrate-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ const runMigrations = async (version) => {

// subscribe to useful events
postgrator.on('migration-started', migration => logger.info(`executing ${migration.version} ${migration.action}:${migration.name}...`))
postgrator.on('error', error => logger.error(error.message))

// Migrate to 'max' version or user-specified e.g. '008'
logger.info('Migrating to version: ' + version)
Expand All @@ -79,10 +78,11 @@ const runMigrations = async (version) => {
await postgrator.migrate(version)
logger.info('SQL Migrations complete')
} catch (error) {
logger.error('Migration error:', error.message)
logger.error(`${error.appliedMigrations.length} migrations were applied...`)
logger.error('Error executing migration...')
logger.error(error)
logger.error(`${error.appliedMigrations.length} migrations were applied.`)
error.appliedMigrations.forEach(migration => {
logger.error(migration.name)
logger.error(`- ${migration.name}`)
})
throw error
}
Expand Down

This file was deleted.

18,599 changes: 0 additions & 18,599 deletions admin/data/sql/migrations/20190128160154.undo.delete-old-schools.sql

This file was deleted.

5 changes: 4 additions & 1 deletion admin/data/sql/seed-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,17 @@ const processSeed = async (seed) => {
} catch (error) {
/*
We can ignore certain error codes and
assume the migration is being re run
assume the seed is being re run
*/
const ignoreErrorCodes = [
2601, // Cannot insert duplicate key
2627 // Violation of UNIQUE KEY constraint
]
if (!~ignoreErrorCodes.indexOf(error.number)) {
throw error
} else {
logger.warn(`ignoring error: ${error.message}`)
logger.warn('This seed is likely to have been previously applied to this database.')
}
}
}
Expand Down
18,284 changes: 0 additions & 18,284 deletions admin/data/sql/seeds/20190128145958.custom.school-data-jan-2019.sql

This file was deleted.

18,284 changes: 18,284 additions & 0 deletions admin/data/sql/seeds/20190129102415.custom.school-data-jan-2019.sql

Large diffs are not rendered by default.

0 comments on commit baa027b

Please sign in to comment.