-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Ajout d'une contrainte d'unicité sur campaignParticipationI…
- Loading branch information
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
api/src/prescription/scripts/add-unicity-constraint-knwoledge-element-snapshots.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,51 @@ | ||
import { knex } from '../../../db/knex-database-connection.js'; | ||
import { Script } from '../../shared/application/scripts/script.js'; | ||
import { ScriptRunner } from '../../shared/application/scripts/script-runner.js'; | ||
|
||
class AddUnicityConstraintKnowledgeElementSnapshots extends Script { | ||
constructor() { | ||
super({ | ||
description: 'Script to add campaignParticipationId unicity constraint on knowledge-element-snapshots', | ||
permanent: false, | ||
options: { | ||
dryRun: { | ||
type: 'boolean', | ||
describe: 'execute script without commit', | ||
demandOption: false, | ||
default: true, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
async handle({ options, logger }) { | ||
logger.info(`add-unicity-constraint-knowledge-element-snapshots | START`); | ||
logger.info(`add-unicity-constraint-knowledge-element-snapshots | dryRun ${options.dryRun}`); | ||
|
||
const trx = await knex.transaction(); | ||
try { | ||
await trx.schema.table('knowledge-element-snapshots', function (table) { | ||
table.unique('campaignParticipationId', { | ||
indexName: 'one_snapshot_by_campaignParticipationId', | ||
}); | ||
}); | ||
|
||
if (options.dryRun) { | ||
logger.info(`add-unicity-constraint-knowledge-element-snapshots | rollback`); | ||
await trx.rollback(); | ||
} else { | ||
logger.info(`add-unicity-constraint-knowledge-element-snapshots | commit`); | ||
await trx.commit(); | ||
} | ||
} catch (err) { | ||
logger.error(`add-unicity-constraint-knowledge-element-snapshots | FAIL | Reason : ${err}`); | ||
await trx.rollback(); | ||
} finally { | ||
logger.info(`add-unicity-constraint-knowledge-element-snapshots | END`); | ||
} | ||
} | ||
} | ||
|
||
await ScriptRunner.execute(import.meta.url, AddUnicityConstraintKnowledgeElementSnapshots); | ||
|
||
export { AddUnicityConstraintKnowledgeElementSnapshots }; |
63 changes: 63 additions & 0 deletions
63
api/tests/prescription/scripts/add-unicity-constraint-knwoledge-element-snapshots_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,63 @@ | ||
import { AddUnicityConstraintKnowledgeElementSnapshots } from '../../../src/prescription/scripts/add-unicity-constraint-knwoledge-element-snapshots.js'; | ||
import { expect, knex, sinon } from '../../test-helper.js'; | ||
|
||
describe('Integration | Prescription | Scripts | add-unicity-constraint-knwoledge-element-snapshots', function () { | ||
let script; | ||
let loggerStub; | ||
const TABLE_NAME = 'knowledge-element-snapshots'; | ||
const CONSTRAINT_NAME = 'one_snapshot_by_campaignParticipationId'; | ||
|
||
before(async function () { | ||
script = new AddUnicityConstraintKnowledgeElementSnapshots(); | ||
loggerStub = { info: sinon.stub(), error: sinon.stub() }; | ||
}); | ||
|
||
afterEach(async function () { | ||
const constraint = await knex.select('constraint_name').from('information_schema.table_constraints').where({ | ||
table_name: TABLE_NAME, | ||
constraint_name: CONSTRAINT_NAME, | ||
}); | ||
|
||
if (constraint.length > 0) { | ||
await knex.schema.table(TABLE_NAME, function (table) { | ||
table.dropUnique('campaignParticipationId', CONSTRAINT_NAME); | ||
}); | ||
} | ||
}); | ||
|
||
describe('Options', function () { | ||
it('has the correct options', function () { | ||
const { options } = script.metaInfo; | ||
expect(options.dryRun).to.deep.include({ | ||
type: 'boolean', | ||
describe: 'execute script without commit', | ||
demandOption: false, | ||
default: true, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#handle', function () { | ||
it('should not add unicity contraint on dryRun mode', async function () { | ||
await script.handle({ options: { dryRun: true }, logger: loggerStub }); | ||
|
||
const constraint = await knex.select('constraint_name').from('information_schema.table_constraints').where({ | ||
table_name: TABLE_NAME, | ||
constraint_name: CONSTRAINT_NAME, | ||
}); | ||
|
||
expect(constraint).to.be.empty; | ||
}); | ||
|
||
it('should add unicity contraint without dryRun mode', async function () { | ||
await script.handle({ options: { dryRun: false }, logger: loggerStub }); | ||
|
||
const constraint = await knex.select('constraint_name').from('information_schema.table_constraints').where({ | ||
table_name: TABLE_NAME, | ||
constraint_name: CONSTRAINT_NAME, | ||
}); | ||
|
||
expect(constraint).to.be.not.empty; | ||
}); | ||
}); | ||
}); |