Skip to content

Commit

Permalink
make fkey constraint migration more dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
VoxBorealis committed Jul 20, 2023
1 parent fc56641 commit be9af20
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions app/src/migrations/20230717105644-clean-up-fkey-constraints.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@

module.exports = {
async up(queryInterface, Sequelize) {
const removeConstraints = async (foreignKeys, target) => {
for (const foreignKey of foreignKeys) {
const { tableName, constraintName } = foreignKey;
if (constraintName.includes(target)) {
await queryInterface.removeConstraint(tableName, constraintName);
}
}
};

// Clean up ScheduledMessage constraints to Jobs.
await queryInterface.removeConstraint("ScheduledMessages", "ScheduledMessages_channelId_fkey");
const scheduledMessageForeignKeys = await queryInterface.getForeignKeyReferencesForTable(
"ScheduledMessages",
);
await removeConstraints(scheduledMessageForeignKeys, "channelId");
await queryInterface.changeColumn("ScheduledMessages", "channelId", {
type: Sequelize.INTEGER,
references: {
Expand All @@ -14,10 +26,8 @@ module.exports = {
});

// Clean up DefaultOffice constraints.
await queryInterface.removeConstraint("People", "People_DefaultOffice_fkey");
await queryInterface.removeConstraint("People", "People_DefaultOffice_fkey1");
await queryInterface.removeConstraint("People", "People_DefaultOffice_fkey2");
await queryInterface.removeConstraint("People", "People_DefaultOffice_fkey3");
const defaultOfficeForeignKeys = await queryInterface.getForeignKeyReferencesForTable("People");
await removeConstraints(defaultOfficeForeignKeys, "DefaultOffice");
await queryInterface.changeColumn("People", "DefaultOffice", {
type: Sequelize.INTEGER,
references: {
Expand All @@ -27,10 +37,8 @@ module.exports = {
onDelete: "SET NULL",
});
// Clean up Signups constraints.
await queryInterface.removeConstraint("Signups", "Signups_OfficeId_fkey");
await queryInterface.removeConstraint("Signups", "Signups_OfficeId_fkey1");
await queryInterface.removeConstraint("Signups", "Signups_OfficeId_fkey2");
await queryInterface.removeConstraint("Signups", "Signups_OfficeId_fkey3");
const signupsForeignkeys = await queryInterface.getForeignKeyReferencesForTable("Signups");
await removeConstraints(signupsForeignkeys, "OfficeId");
await queryInterface.changeColumn("Signups", "OfficeId", {
type: Sequelize.INTEGER,
references: {
Expand All @@ -41,9 +49,10 @@ module.exports = {
});

// Clean up Defaultsignups constraints.
await queryInterface.removeConstraint("Defaultsignups", "Defaultsignups_OfficeId_fkey");
await queryInterface.removeConstraint("Defaultsignups", "Defaultsignups_OfficeId_fkey1");
await queryInterface.removeConstraint("Defaultsignups", "Defaultsignups_OfficeId_fkey2");
const defaultsignupsForeignKeys = await queryInterface.getForeignKeyReferencesForTable(
"Defaultsignups",
);
await removeConstraints(defaultsignupsForeignKeys, "OfficeId");
await queryInterface.changeColumn("Defaultsignups", "OfficeId", {
type: Sequelize.INTEGER,
references: {
Expand All @@ -66,8 +75,6 @@ module.exports = {
});
};
await addOldDefaultsignupsConstraint();
await addOldDefaultsignupsConstraint();
await addOldDefaultsignupsConstraint();

await queryInterface.removeConstraint("Signups", "Signups_OfficeId_fkey");
const addOldSignupsConstraint = async () => {
Expand All @@ -80,9 +87,6 @@ module.exports = {
});
};
await addOldSignupsConstraint();
await addOldSignupsConstraint();
await addOldSignupsConstraint();
await addOldSignupsConstraint();

const addOldDefaultOfficeConstraint = async () => {
await queryInterface.changeColumn("People", "DefaultOffice", {
Expand All @@ -95,9 +99,6 @@ module.exports = {
};
await queryInterface.removeConstraint("People", "People_DefaultOffice_fkey");
await addOldDefaultOfficeConstraint();
await addOldDefaultOfficeConstraint();
await addOldDefaultOfficeConstraint();
await addOldDefaultOfficeConstraint();

await queryInterface.removeConstraint("ScheduledMessages", "ScheduledMessages_channelId_fkey");

Expand Down

0 comments on commit be9af20

Please sign in to comment.