-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy answers ke data to temporary tables
Co-authored-by: Pierre Top <ptop@octo.com>
- Loading branch information
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
api/scripts/bigint/answer/copy-answers-ke-data-to-temporary-tables-with-bigint.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,60 @@ | ||
require('dotenv').config(); | ||
const { performance } = require('perf_hooks'); | ||
const { knex } = require('../../../db/knex-database-connection'); | ||
const logger = require('../../../lib/infrastructure/logger'); | ||
|
||
const copyAnswerKeDataToTemporaryTables = async () => { | ||
logger.debug('Start copy answers and knowledge-elements data'); | ||
const startTime = performance.now(); | ||
|
||
await knex.transaction(async (trx) => { | ||
await knex | ||
.raw( | ||
`INSERT INTO "answers_bigint" | ||
(id, value, result, "assessmentId", "challengeId", timeout, "resultDetails", "timeSpent", "isFocusedOut") | ||
SELECT | ||
a.id, a.value, a.result, a."assessmentId", a."challengeId", a.timeout, a."resultDetails", a."timeSpent", a."isFocusedOut" | ||
FROM "answers" a` | ||
) | ||
.transacting(trx); | ||
|
||
await knex | ||
.raw( | ||
`INSERT INTO "knowledge-elements_bigint" | ||
( id, source, status, "createdAt", "answerId", "assessmentId", "skillId", "earnedPix", "userId", "competenceId") | ||
SELECT | ||
ke.id, ke.source, ke.status, ke."createdAt", ke."answerId", ke."assessmentId", ke."skillId", ke."earnedPix", ke."userId", ke."competenceId" | ||
FROM "knowledge-elements" ke` | ||
) | ||
.transacting(trx); | ||
}); | ||
const endTime = performance.now(); | ||
|
||
logger.debug(`Call to copyAnswerKeDataToTemporaryTables took ${endTime - startTime} milliseconds`); | ||
logger.debug('Finish copy'); | ||
}; | ||
|
||
const isLaunchedFromCommandLine = require.main === module; | ||
|
||
async function main() { | ||
try { | ||
logger.debug('Start script... '); | ||
await copyAnswerKeDataToTemporaryTables(); | ||
logger.debug('End script: copy done successfully.'); | ||
} catch (error) { | ||
logger.error(error); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
if (isLaunchedFromCommandLine) { | ||
main().then( | ||
() => process.exit(0), | ||
(err) => { | ||
logger.error(err); | ||
process.exit(1); | ||
} | ||
); | ||
} | ||
|
||
module.exports = { copyAnswerKeDataToTemporaryTables }; |
68 changes: 68 additions & 0 deletions
68
...ptance/scripts/bigint/answer/copy-answers-ke-data-to-temporary-tables-with-bigint_test.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,68 @@ | ||
const { | ||
createAnswersBigintMigrationDatabaseStructures, | ||
} = require('../../../../../scripts/bigint/answer/create-migration-database-structure'); | ||
const { | ||
copyAnswerKeDataToTemporaryTables, | ||
} = require('../../../../../scripts/bigint/answer/copy-answers-ke-data-to-temporary-tables-with-bigint'); | ||
const { expect, knex } = require('../../../../test-helper'); | ||
|
||
const DatabaseBuilder = require('../../../../../db/database-builder/database-builder'); | ||
const databaseBuilder = new DatabaseBuilder({ knex }); | ||
|
||
describe('#copyAnswerKeDataToTemporaryTables', function () { | ||
it('should copy data from tables to temporary tables', async function () { | ||
// given | ||
await knex.raw('DROP TABLE IF EXISTS "bigint-migration-settings"'); | ||
await knex.raw('DROP TABLE IF EXISTS "knowledge-elements_bigint"'); | ||
await knex.raw('DROP TABLE IF EXISTS "answers_bigint"'); | ||
await createAnswersBigintMigrationDatabaseStructures(knex); | ||
|
||
const { id: assessmentId } = databaseBuilder.factory.buildAssessment(); | ||
const { id: firstAnswerId } = databaseBuilder.factory.buildAnswer({ assessmentId }); | ||
databaseBuilder.factory.buildKnowledgeElement({ assessmentId, answerId: firstAnswerId }); | ||
const { id: secondAnswerId } = databaseBuilder.factory.buildAnswer({ assessmentId }); | ||
databaseBuilder.factory.buildKnowledgeElement({ assessmentId, answerId: secondAnswerId }); | ||
|
||
await databaseBuilder.commit(); | ||
|
||
// when | ||
await copyAnswerKeDataToTemporaryTables(); | ||
|
||
// then | ||
const { rows: answersBigintCount } = await knex.raw(`SELECT COUNT(1) FROM "answers_bigint"`); | ||
expect(answersBigintCount[0].count).to.equal(2); | ||
const { rows: knowledgeElementsBigintCount } = await knex.raw(`SELECT COUNT(1) FROM "knowledge-elements_bigint"`); | ||
expect(knowledgeElementsBigintCount[0].count).to.equal(2); | ||
}); | ||
it('should copy both tables or nothing', async function () { | ||
// given | ||
await knex.raw('DROP TABLE IF EXISTS "bigint-migration-settings"'); | ||
await knex.raw('DROP TABLE IF EXISTS "knowledge-elements_bigint"'); | ||
await knex.raw('DROP TABLE IF EXISTS "answers_bigint"'); | ||
await createAnswersBigintMigrationDatabaseStructures(knex); | ||
await knex.raw('DROP TABLE IF EXISTS "knowledge-elements_bigint"'); | ||
|
||
const { id: assessmentId } = databaseBuilder.factory.buildAssessment(); | ||
databaseBuilder.factory.buildAnswer({ assessmentId }); | ||
await databaseBuilder.commit(); | ||
|
||
// when | ||
let error; | ||
try { | ||
await copyAnswerKeDataToTemporaryTables(); | ||
} catch (err) { | ||
error = err; | ||
} | ||
|
||
// then | ||
expect(error.message).to.have.string('relation "knowledge-elements_bigint" does not exist'); | ||
const { rows: answersBigintCount } = await knex.raw(`SELECT COUNT(1) FROM "answers_bigint"`); | ||
expect(answersBigintCount[0].count).to.equal(0); | ||
}); | ||
|
||
afterEach(async function () { | ||
await knex('knowledge-elements').delete(); | ||
await knex('answers').delete(); | ||
await knex('assessments').delete(); | ||
}); | ||
}); |