Skip to content

Commit 5536ff8

Browse files
committed
migration files for removing behaviour+related tables
1 parent 8bc4b03 commit 5536ff8

5 files changed

+243
-1
lines changed

backend/typescript/migrations/2024.11.21T16.49.50.add-constraint-behaviour-level-details-table.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { DataType } from "sequelize-typescript";
21
import { Migration } from "../umzug";
32
import { MAX_BEHAVIOUR_LEVEL, MIN_BEHAVIOUR_LEVEL } from "../constants";
43

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { DataType } from "sequelize-typescript";
2+
3+
import { Migration } from "../umzug";
4+
5+
// hard coded so that we can remove the constants from the constants.ts file
6+
const MIN_BEHAVIOUR_LEVEL = 1;
7+
const MAX_BEHAVIOUR_LEVEL = 4;
8+
9+
const TABLE_NAME = "behaviour_level_details";
10+
const CONSTRAINT_NAME = "unique_behaviour_level";
11+
const CONSTRAINT_NAME_2 = "level_interval";
12+
13+
export const up: Migration = async ({ context: sequelize }) => {
14+
await sequelize.query(
15+
`ALTER TABLE ${TABLE_NAME} DROP CONSTRAINT ${CONSTRAINT_NAME_2};`,
16+
);
17+
18+
await sequelize
19+
.getQueryInterface()
20+
.removeConstraint(TABLE_NAME, CONSTRAINT_NAME);
21+
22+
await sequelize.getQueryInterface().dropTable(TABLE_NAME);
23+
};
24+
25+
export const down: Migration = async ({ context: sequelize }) => {
26+
await sequelize.getQueryInterface().createTable(TABLE_NAME, {
27+
id: {
28+
type: DataType.INTEGER,
29+
allowNull: false,
30+
primaryKey: true,
31+
autoIncrement: true,
32+
},
33+
behaviour_id: {
34+
type: DataType.INTEGER,
35+
allowNull: false,
36+
references: {
37+
model: "behaviours",
38+
key: "id",
39+
},
40+
},
41+
level: {
42+
type: DataType.INTEGER,
43+
allowNull: false,
44+
},
45+
description: {
46+
type: DataType.STRING,
47+
allowNull: true,
48+
},
49+
training_instructions: {
50+
type: DataType.STRING,
51+
allowNull: true,
52+
},
53+
});
54+
55+
await sequelize.getQueryInterface().addConstraint(TABLE_NAME, {
56+
fields: ["behaviour_id", "level"],
57+
type: "unique",
58+
name: CONSTRAINT_NAME,
59+
});
60+
61+
await sequelize.query(
62+
`ALTER TABLE ${TABLE_NAME} ADD CONSTRAINT ${CONSTRAINT_NAME_2}
63+
CHECK (level BETWEEN ${MIN_BEHAVIOUR_LEVEL} AND ${MAX_BEHAVIOUR_LEVEL});`,
64+
);
65+
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { DataType } from "sequelize-typescript";
2+
3+
import { Migration } from "../umzug";
4+
5+
// hard coded so that we can remove the constants from the constants.ts file
6+
const MIN_BEHAVIOUR_LEVEL = 1;
7+
const MAX_BEHAVIOUR_LEVEL = 4;
8+
9+
const TABLE_NAME = "pet_behaviours";
10+
const CONSTRAINT_NAME = "unique_pet_behaviour";
11+
const CONSTRAINT_NAME_2 = "skill_level_interval";
12+
13+
export const up: Migration = async ({ context: sequelize }) => {
14+
await sequelize
15+
.getQueryInterface()
16+
.removeConstraint(TABLE_NAME, CONSTRAINT_NAME);
17+
18+
await sequelize
19+
.getQueryInterface()
20+
.removeColumn(TABLE_NAME, "is_highlighted");
21+
22+
await sequelize.query(
23+
`ALTER TABLE ${TABLE_NAME} DROP CONSTRAINT ${CONSTRAINT_NAME_2};`,
24+
);
25+
26+
await sequelize.getQueryInterface().dropTable(TABLE_NAME);
27+
};
28+
29+
export const down: Migration = async ({ context: sequelize }) => {
30+
await sequelize.getQueryInterface().createTable(TABLE_NAME, {
31+
id: {
32+
type: DataType.INTEGER,
33+
autoIncrement: true,
34+
primaryKey: true,
35+
allowNull: false,
36+
},
37+
pet_id: {
38+
type: DataType.INTEGER,
39+
allowNull: false,
40+
references: {
41+
model: "pets",
42+
key: "id",
43+
},
44+
},
45+
behaviour_id: {
46+
type: DataType.INTEGER,
47+
allowNull: false,
48+
references: {
49+
model: "behaviours",
50+
key: "id",
51+
},
52+
},
53+
skill_level: {
54+
type: DataType.INTEGER,
55+
allowNull: false,
56+
},
57+
});
58+
59+
await sequelize.getQueryInterface().addColumn(TABLE_NAME, "is_highlighted", {
60+
type: DataType.BOOLEAN,
61+
allowNull: true,
62+
defaultValue: false,
63+
});
64+
65+
await sequelize.getQueryInterface().addConstraint(TABLE_NAME, {
66+
fields: ["behaviour_id", "pet_id"],
67+
type: "unique",
68+
name: CONSTRAINT_NAME,
69+
});
70+
71+
await sequelize.query(
72+
`ALTER TABLE ${TABLE_NAME} ADD CONSTRAINT ${CONSTRAINT_NAME_2}
73+
CHECK (skill_level BETWEEN ${MIN_BEHAVIOUR_LEVEL} AND ${MAX_BEHAVIOUR_LEVEL});`,
74+
);
75+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { DataType } from "sequelize-typescript";
2+
3+
import { Migration } from "../umzug";
4+
5+
// hard coded so that we can remove the constants from the constants.ts file
6+
const MIN_BEHAVIOUR_LEVEL = 1;
7+
const MAX_BEHAVIOUR_LEVEL = 4;
8+
9+
const TABLE_NAME = "user_behaviours";
10+
const CONSTRAINT_NAME = "unique_user_behaviour_skill";
11+
const CONSTRAINT_NAME_2 = "max_level_interval";
12+
13+
export const up: Migration = async ({ context: sequelize }) => {
14+
await sequelize
15+
.getQueryInterface()
16+
.removeConstraint(TABLE_NAME, CONSTRAINT_NAME);
17+
18+
await sequelize.query(
19+
`ALTER TABLE ${TABLE_NAME} DROP CONSTRAINT ${CONSTRAINT_NAME_2};`,
20+
);
21+
22+
await sequelize.getQueryInterface().dropTable(TABLE_NAME);
23+
};
24+
25+
export const down: Migration = async ({ context: sequelize }) => {
26+
await sequelize.getQueryInterface().createTable(TABLE_NAME, {
27+
id: {
28+
type: DataType.INTEGER,
29+
allowNull: false,
30+
primaryKey: true,
31+
autoIncrement: true,
32+
},
33+
user_id: {
34+
type: DataType.INTEGER,
35+
allowNull: false,
36+
references: {
37+
model: "users",
38+
key: "id",
39+
},
40+
},
41+
behaviour_id: {
42+
type: DataType.INTEGER,
43+
allowNull: false,
44+
references: {
45+
model: "behaviours",
46+
key: "id",
47+
},
48+
},
49+
max_level: {
50+
type: DataType.INTEGER,
51+
allowNull: false,
52+
},
53+
});
54+
55+
await sequelize.getQueryInterface().addConstraint(TABLE_NAME, {
56+
fields: ["behaviour_id", "user_id"],
57+
type: "unique",
58+
name: CONSTRAINT_NAME,
59+
});
60+
61+
await sequelize.query(
62+
`ALTER TABLE ${TABLE_NAME} ADD CONSTRAINT ${CONSTRAINT_NAME_2}
63+
CHECK (max_level BETWEEN ${MIN_BEHAVIOUR_LEVEL} AND ${MAX_BEHAVIOUR_LEVEL});`,
64+
);
65+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { DataType } from "sequelize-typescript";
2+
3+
import { Migration } from "../umzug";
4+
5+
const TABLE_NAME = "behaviours";
6+
7+
export const up: Migration = async ({ context: sequelize }) => {
8+
await sequelize
9+
.getQueryInterface()
10+
.removeColumn(TABLE_NAME, "parent_behaviour_id");
11+
await sequelize.getQueryInterface().dropTable("behaviours");
12+
};
13+
14+
export const down: Migration = async ({ context: sequelize }) => {
15+
await sequelize.getQueryInterface().createTable(TABLE_NAME, {
16+
id: {
17+
type: DataType.INTEGER,
18+
allowNull: false,
19+
primaryKey: true,
20+
autoIncrement: true,
21+
},
22+
behaviour_name: {
23+
type: DataType.STRING,
24+
allowNull: false,
25+
},
26+
});
27+
28+
await sequelize
29+
.getQueryInterface()
30+
.addColumn(TABLE_NAME, "parent_behaviour_id", {
31+
type: DataType.INTEGER,
32+
allowNull: true,
33+
references: {
34+
model: "behaviours",
35+
key: "id",
36+
},
37+
});
38+
};

0 commit comments

Comments
 (0)