Skip to content

Commit 9558572

Browse files
authored
[backend] Add constraint on grant's scenario
1 parent 0586a09 commit 9558572

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.openbas.migration;
2+
3+
import java.sql.Statement;
4+
import org.flywaydb.core.api.migration.BaseJavaMigration;
5+
import org.flywaydb.core.api.migration.Context;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
public class V3_86__Enforce_constraint_grant_scenario extends BaseJavaMigration {
10+
11+
@Override
12+
public void migrate(Context context) throws Exception {
13+
try (Statement statement = context.getConnection().createStatement()) {
14+
// Remove duplicate values
15+
statement.execute(
16+
"""
17+
DELETE FROM grants g
18+
USING grants g2
19+
WHERE g.ctid < g2.ctid
20+
AND g.grant_group = g2.grant_group
21+
AND g.grant_scenario = g2.grant_scenario
22+
AND g.grant_name = g2.grant_name;
23+
""");
24+
// Rename existing index
25+
statement.execute(
26+
"""
27+
ALTER INDEX "grant" RENAME TO grant_exercise;
28+
""");
29+
// Create index for grant X scenario
30+
statement.execute(
31+
"""
32+
CREATE UNIQUE INDEX IF NOT EXISTS grant_scenario
33+
ON grants (grant_group, grant_scenario, grant_name);
34+
""");
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)