Skip to content

Commit d60b997

Browse files
Create migration file
1 parent 01d2884 commit d60b997

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { DataType } from "sequelize-typescript";
2+
3+
import { Migration } from "../umzug";
4+
import {
5+
CommunityPositionTitles,
6+
Department,
7+
DesignPositionTitles,
8+
EngineeringPositionTitles,
9+
PositionTitles,
10+
ProductPositionTitles,
11+
} from "../types";
12+
13+
const ADMIN_TABLE_NAME = "admins";
14+
const POSITION_TABLE_NAME = "positions";
15+
const USER_TABLE_NAME = "users";
16+
17+
const ADMIN_SEEDED_DATA = [
18+
{
19+
userId: "1",
20+
authorizedDepartments: [Department.Engineering, Department.Product],
21+
},
22+
{
23+
userId: "2",
24+
authorizedDepartments: [Department.Design, Department.Community],
25+
},
26+
];
27+
const POSITION_SEEDED_DATA = [
28+
...EngineeringPositionTitles.map((title) => ({
29+
title,
30+
department: Department.Engineering,
31+
})),
32+
...DesignPositionTitles.map((title) => ({
33+
title,
34+
department: Department.Design,
35+
})),
36+
...ProductPositionTitles.map((title) => ({
37+
title,
38+
department: Department.Product,
39+
})),
40+
...CommunityPositionTitles.map((title) => ({
41+
title,
42+
department: Department.Community,
43+
})),
44+
];
45+
46+
export const up: Migration = async ({ context: sequelize }) => {
47+
await sequelize.getQueryInterface().createTable(POSITION_TABLE_NAME, {
48+
title: {
49+
type: DataType.ENUM(...PositionTitles),
50+
allowNull: false,
51+
primaryKey: true,
52+
},
53+
department: {
54+
type: DataType.ENUM(...Object.values(Department)),
55+
allowNull: false,
56+
},
57+
});
58+
await sequelize
59+
.getQueryInterface()
60+
.bulkInsert(POSITION_TABLE_NAME, POSITION_SEEDED_DATA);
61+
62+
await sequelize.getQueryInterface().addColumn(USER_TABLE_NAME, "position", {
63+
type: "enum_positions_title",
64+
references: {
65+
model: POSITION_TABLE_NAME,
66+
key: "title",
67+
},
68+
});
69+
70+
await sequelize.getQueryInterface().createTable(ADMIN_TABLE_NAME, {
71+
userId: {
72+
type: DataType.INTEGER,
73+
allowNull: false,
74+
primaryKey: true,
75+
references: {
76+
model: USER_TABLE_NAME,
77+
key: "id",
78+
},
79+
},
80+
authorizedDepartments: {
81+
type: DataType.ARRAY(DataType.STRING),
82+
allowNull: false,
83+
},
84+
});
85+
await sequelize
86+
.getQueryInterface()
87+
.bulkInsert(ADMIN_TABLE_NAME, ADMIN_SEEDED_DATA);
88+
};
89+
90+
export const down: Migration = async ({ context: sequelize }) => {
91+
await sequelize.getQueryInterface().removeColumn(USER_TABLE_NAME, "position");
92+
await sequelize.getQueryInterface().dropTable(ADMIN_TABLE_NAME);
93+
await sequelize.getQueryInterface().dropTable(POSITION_TABLE_NAME);
94+
await sequelize.query('DROP TYPE IF EXISTS "enum_positions_title";');
95+
await sequelize.query('DROP TYPE IF EXISTS "enum_positions_department";');
96+
};

backend/typescript/models/admin.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { Department } from "../types";
1111
@Table({ tableName: "admins" })
1212
export default class Admin extends Model {
1313
@ForeignKey(() => User)
14-
@Column({ type: DataType.STRING, primaryKey: true })
15-
userId!: string;
14+
@Column({ type: DataType.INTEGER, primaryKey: true })
15+
userId!: number;
1616

1717
@Column({ type: DataType.ARRAY(DataType.ENUM(...Object.values(Department))) })
1818
authorizedDepartments!: Department[];

backend/typescript/models/user.model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
Model,
99
Table,
1010
} from "sequelize-typescript";
11-
import { PositionTitle, Role } from "../types";
11+
import { PositionTitle, PositionTitles, Role } from "../types";
1212
import ApplicationDashboardTable from "./applicationDashboard.model";
1313
import Position from "./position.model";
1414

@@ -33,8 +33,8 @@ export default class User extends Model {
3333
role!: Role;
3434

3535
@ForeignKey(() => Position)
36-
@Column({ type: DataType.STRING })
37-
position!: PositionTitle;
36+
@Column({ type: DataType.ENUM(...Object.values(PositionTitles)) })
37+
position?: PositionTitle;
3838

3939
@HasMany(() => ApplicationDashboardTable)
4040
applicationDashboards?: ApplicationDashboardTable[];

0 commit comments

Comments
 (0)