-
Notifications
You must be signed in to change notification settings - Fork 1
[INTF25] - Drop enums from database columns #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
backend/typescript/migrations/20251111222215-drop-enums-from-database.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* eslint-disable @typescript-eslint/naming-convention */ | ||
| import { DataType } from "sequelize-typescript"; | ||
| import { Migration } from "../umzug"; | ||
|
|
||
| const USER_TABLE = "users"; | ||
| const APPLICANT_RECORDS_TABLE = "applicant_records"; | ||
|
|
||
| const POSITION_TABLE = "positions"; | ||
|
|
||
| const POSITION_DATA = [ | ||
| { title: "Project Lead", department: "Engineering" }, | ||
| { title: "Developer", department: "Engineering" }, | ||
| { title: "VP Engineering", department: "Engineering" }, | ||
| { title: "Designer", department: "Design" }, | ||
| { title: "VP Design", department: "Design" }, | ||
| { title: "Product Manager", department: "Product" }, | ||
| { title: "VP Product", department: "Product" }, | ||
| { title: "President", department: "Community" }, | ||
| { title: "VP Scoping", department: "Community" }, | ||
| { title: "VP Talent", department: "Community" }, | ||
| { title: "VP Finance", department: "Community" }, | ||
| { title: "Director Lead", department: "Community" }, | ||
| { title: "Internal Director", department: "Community" }, | ||
| { title: "External Director", department: "Community" }, | ||
| { title: "Content Strategist", department: "Community" }, | ||
| { title: "Graphic Designer", department: "Community" }, | ||
| ]; | ||
|
|
||
| const POSITION_TITLES = [ | ||
| "Project Lead", | ||
| "Developer", | ||
| "VP Engineering", | ||
| "Designer", | ||
| "VP Design", | ||
| "Product Manager", | ||
| "VP Product", | ||
| "President", | ||
| "VP Scoping", | ||
| "VP Talent", | ||
| "VP Finance", | ||
| "Director Lead", | ||
| "Internal Director", | ||
| "External Director", | ||
| "Content Strategist", | ||
| "Graphic Designer", | ||
| ]; | ||
|
|
||
| const DEPARTMENT_TITLES = ["Engineering", "Design", "Product", "Community"]; | ||
|
|
||
| const position = "position"; | ||
| const enum_positions_title = "enum_positions_title"; | ||
| const enum_applicant_records_position = "enum_applicant_records_position"; | ||
| const enum_positions_department = "enum_positions_department"; | ||
|
|
||
| export const up: Migration = async ({ context: sequelize }) => { | ||
| // Drop FK constraints referencing positions | ||
| await sequelize.getQueryInterface().sequelize.query(` | ||
| ALTER TABLE "${USER_TABLE}" DROP CONSTRAINT IF EXISTS "${USER_TABLE}_${position}_fkey"; | ||
| ALTER TABLE "${APPLICANT_RECORDS_TABLE}" DROP CONSTRAINT IF EXISTS "${APPLICANT_RECORDS_TABLE}_${position}_fkey"; | ||
| `); | ||
|
|
||
| // Drop the enum-based positions table | ||
| await sequelize.getQueryInterface().dropTable(POSITION_TABLE); | ||
|
|
||
| // Recreate positions table with string columns | ||
| await sequelize.getQueryInterface().createTable(POSITION_TABLE, { | ||
| title: { | ||
| type: DataType.STRING, | ||
| allowNull: false, | ||
| primaryKey: true, | ||
| }, | ||
| department: { | ||
| type: DataType.STRING, | ||
| allowNull: false, | ||
| }, | ||
| }); | ||
|
|
||
| // Re seed positions data | ||
| await sequelize.getQueryInterface().bulkInsert(POSITION_TABLE, POSITION_DATA); | ||
|
|
||
| // Convert enum columns in users and applicant_records to type string | ||
| await sequelize.getQueryInterface().sequelize.query(` | ||
| ALTER TABLE "${USER_TABLE}" | ||
| ALTER COLUMN "${position}" TYPE VARCHAR(255) | ||
| USING ("${position}"::text); | ||
| `); | ||
| await sequelize.getQueryInterface().sequelize.query(` | ||
| ALTER TABLE "${APPLICANT_RECORDS_TABLE}" | ||
| ALTER COLUMN "${position}" TYPE VARCHAR(255) | ||
| USING ("${position}"::text); | ||
| `); | ||
|
|
||
| // Re add FKs referencing string positions.title | ||
| await sequelize.getQueryInterface().sequelize.query(` | ||
| ALTER TABLE "${USER_TABLE}" | ||
| ADD CONSTRAINT "${USER_TABLE}_${position}_fkey" | ||
| FOREIGN KEY ("${position}") REFERENCES "${POSITION_TABLE}" ("title") | ||
| ON DELETE SET NULL ON UPDATE CASCADE; | ||
| `); | ||
| await sequelize.getQueryInterface().sequelize.query(` | ||
| ALTER TABLE "${APPLICANT_RECORDS_TABLE}" | ||
| ADD CONSTRAINT "${APPLICANT_RECORDS_TABLE}_${position}_fkey" | ||
| FOREIGN KEY ("${position}") REFERENCES "${POSITION_TABLE}" ("title") | ||
| ON DELETE SET NULL ON UPDATE CASCADE; | ||
| `); | ||
|
|
||
| // Drop old enum types | ||
| await sequelize | ||
| .getQueryInterface() | ||
| .sequelize.query(`DROP TYPE IF EXISTS "${enum_positions_title}" CASCADE;`); | ||
| await sequelize | ||
| .getQueryInterface() | ||
| .sequelize.query( | ||
| `DROP TYPE IF EXISTS "${enum_positions_department}}" CASCADE;`, | ||
| ); | ||
| await sequelize | ||
| .getQueryInterface() | ||
| .sequelize.query( | ||
| `DROP TYPE IF EXISTS "${enum_applicant_records_position}" CASCADE;`, | ||
| ); | ||
| }; | ||
|
|
||
| export const down: Migration = async ({ context: sequelize }) => { | ||
| // Drop foreign key constraints referencing positions table | ||
| await sequelize.getQueryInterface().sequelize.query(` | ||
| ALTER TABLE "${USER_TABLE}" DROP CONSTRAINT IF EXISTS "${USER_TABLE}_${position}_fkey"; | ||
| ALTER TABLE "${APPLICANT_RECORDS_TABLE}" DROP CONSTRAINT IF EXISTS "${APPLICANT_RECORDS_TABLE}_${position}_fkey"; | ||
| `); | ||
|
|
||
| // Drop the string based positions table | ||
| await sequelize.getQueryInterface().dropTable(POSITION_TABLE); | ||
|
|
||
| // Recreate table with enums | ||
| await sequelize.getQueryInterface().createTable(POSITION_TABLE, { | ||
| title: { | ||
| type: DataType.ENUM(...POSITION_TITLES), | ||
| allowNull: false, | ||
| primaryKey: true, | ||
| }, | ||
| department: { | ||
| type: DataType.ENUM(...DEPARTMENT_TITLES), | ||
| allowNull: false, | ||
| }, | ||
| }); | ||
|
|
||
| // Seed ENUM based positions table | ||
| await sequelize.getQueryInterface().bulkInsert(POSITION_TABLE, POSITION_DATA); | ||
|
|
||
| // change column type from string to enum | ||
| await sequelize.getQueryInterface().sequelize.query(` | ||
| ALTER TABLE "users" | ||
| ALTER COLUMN "position" TYPE "${enum_positions_title}" | ||
| USING ("position"::text::"${enum_positions_title}"); | ||
| `); | ||
|
|
||
| // change column type from string to enum | ||
| await sequelize.getQueryInterface().sequelize.query(` | ||
| ALTER TABLE "applicant_records" | ||
| ALTER COLUMN "position" TYPE "${enum_positions_title}" | ||
| USING ("position"::text::"${enum_positions_title}"); | ||
| `); | ||
|
|
||
| // add back foreign keys | ||
| await sequelize.getQueryInterface().sequelize.query(` | ||
| ALTER TABLE "${USER_TABLE}" | ||
| ADD CONSTRAINT "${USER_TABLE}_${position}_fkey" | ||
| FOREIGN KEY ("${position}") REFERENCES "${POSITION_TABLE}" ("title") | ||
| ON DELETE SET NULL ON UPDATE CASCADE; | ||
| `); | ||
| await sequelize.getQueryInterface().sequelize.query(` | ||
| ALTER TABLE "${APPLICANT_RECORDS_TABLE}" | ||
| ADD CONSTRAINT "${APPLICANT_RECORDS_TABLE}_${position}_fkey" | ||
| FOREIGN KEY ("${position}") REFERENCES "${POSITION_TABLE}" ("title") | ||
| ON DELETE SET NULL ON UPDATE CASCADE; | ||
| `); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,10 @@ | ||
| import { Column, DataType, Model, Table } from "sequelize-typescript"; | ||
| import { Department, PositionTitle, PositionTitles } from "../types"; | ||
|
|
||
| @Table({ tableName: "positions" }) | ||
| export default class Position extends Model { | ||
| @Column({ type: DataType.ENUM(...PositionTitles), primaryKey: true }) | ||
| title!: PositionTitle; | ||
| @Column({ type: DataType.STRING, primaryKey: true }) | ||
| title!: string; | ||
|
|
||
| @Column({ type: DataType.ENUM(...Object.values(Department)) }) | ||
| department!: Department; | ||
| @Column({ type: DataType.STRING }) | ||
| department!: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to jump through a lot of hoops to get this migration working... because FK restraints in the database and enum restraints in the database prevented some of the changes. But, the general logic for the migration is as follows:
Drop foreign keys on the columns with the position enums we are trying to remove.
Delete the positions table and recreated it with columns of type string instead of enums.
Convert columns previously of type position enums to string enums.
Re-add the foreign keys to those columns.
At this point, no columns are using the position enums, drop enums.
For the down migration, the same logic applies but it changes the columns from type string back to type position enums.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Foreign keys do be a pain in the ass...