Skip to content

Commit

Permalink
[FEATURE] Ajout d'une contrainte d'unicité sur campaignParticipationI…
Browse files Browse the repository at this point in the history
…d dans les Ke Snapshots (PIX-16258)

 #11229
  • Loading branch information
pix-service-auto-merge authored Jan 29, 2025
2 parents c0af085 + 535e2b3 commit 4899b0f
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
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 };
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;
});
});
});

0 comments on commit 4899b0f

Please sign in to comment.