|
| 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 | +}; |
0 commit comments