Skip to content

Commit 6c05c26

Browse files
authored
tech(api): add migration for unique index on knowledge-element-snapshot
1 parent 86fbf3b commit 6c05c26

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const TABLE_NAME = 'knowledge-element-snapshots';
2+
const COLUMN_NAME = 'campaignParticipationId';
3+
const CONSTRAINT_NAME = 'one_snapshot_by_campaignParticipationId';
4+
5+
/**
6+
* @param { import("knex").Knex } knex
7+
* @returns { Promise<void> }
8+
*/
9+
const up = async function (knex) {
10+
const result = await knex.raw(
11+
`select constraint_name
12+
from information_schema.constraint_column_usage
13+
where table_name = :tableName and constraint_name = :constraintName`,
14+
{
15+
tableName: TABLE_NAME,
16+
constraintName: CONSTRAINT_NAME,
17+
},
18+
);
19+
20+
if (result.rowCount === 1) {
21+
return;
22+
}
23+
await knex.raw('ALTER TABLE :tableName: ADD CONSTRAINT :constraintName: UNIQUE (:columnName:)', {
24+
tableName: TABLE_NAME,
25+
constraintName: CONSTRAINT_NAME,
26+
columnName: COLUMN_NAME,
27+
});
28+
};
29+
30+
/**
31+
* @param { import("knex").Knex } knex
32+
* @returns { Promise<void> }
33+
*/
34+
const down = async function (knex) {
35+
await knex.schema.table(TABLE_NAME, function (table) {
36+
table.dropUnique(COLUMN_NAME, CONSTRAINT_NAME);
37+
});
38+
};
39+
40+
export { down, up };

0 commit comments

Comments
 (0)