-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- delete `AccountTypes` table - change `accountTypeId` to `accountCategory` - update Accounts model, controller, service, and tests, to reflect changes
- Loading branch information
Showing
8 changed files
with
179 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,141 @@ | ||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
const transaction = await queryInterface.sequelize.transaction(); | ||
|
||
try { | ||
// Define the ENUM type for account types | ||
const ACCOUNT_TYPES = [ | ||
'general', 'cash', 'current-account', 'credit-card', 'saving', | ||
'bonus', 'insurance', 'investment', 'loan', 'mortgage', | ||
'overdraft', 'crypto', | ||
]; | ||
|
||
// Add a temporary ENUM column | ||
await queryInterface.addColumn( | ||
'Accounts', | ||
'accountCategory', | ||
{ | ||
type: Sequelize.ENUM(...ACCOUNT_TYPES), | ||
allowNull: false, | ||
defaultValue: 'general', | ||
}, | ||
{ transaction } | ||
); | ||
|
||
// Map numeric IDs to ENUM values | ||
for (let i = 0; i < ACCOUNT_TYPES.length; i++) { | ||
await queryInterface.sequelize.query( | ||
`UPDATE "Accounts" SET "accountCategory" = '${ACCOUNT_TYPES[i]}' WHERE "accountTypeId" = ${i + 1}`, | ||
{ transaction } | ||
); | ||
} | ||
|
||
// Remove the old accountTypeId column | ||
await queryInterface.removeColumn('Accounts', 'accountTypeId', { transaction }); | ||
|
||
// Drop the AccountTypes table | ||
await queryInterface.dropTable('AccountTypes', { transaction }); | ||
|
||
await transaction.commit(); | ||
} catch (err) { | ||
await transaction.rollback(); | ||
throw err; | ||
} | ||
}, | ||
down: async (queryInterface, Sequelize) => { | ||
const transaction = await queryInterface.sequelize.transaction(); | ||
|
||
try { | ||
// Strict data for recovering, do not edit until you sure | ||
const ACCOUNT_TYPES = { | ||
general: "General", | ||
cash: "Cash", | ||
'current-account': "Current account", | ||
'credit-card': "Credit card", | ||
saving: "Saving account", | ||
bonus: "Bonus", | ||
insurance: "Insurance", | ||
investment: "Investment", | ||
loan: "Loan", | ||
mortgage: "Mortgage", | ||
overdraft: "Account with overdraft", | ||
crypto: "Crypto", | ||
} | ||
|
||
// Recreate the AccountTypes table | ||
await queryInterface.createTable('AccountTypes', { | ||
id: { | ||
type: Sequelize.INTEGER, | ||
unique: true, | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
}, | ||
name: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
}, { transaction }); | ||
|
||
await queryInterface.bulkInsert( | ||
'AccountTypes', | ||
Object.values(ACCOUNT_TYPES).map((name, index) => ({ id: index + 1, name })), | ||
{ transaction }, | ||
); | ||
|
||
// Firstly create column with allowNull: true | ||
// then fill it | ||
// then set allowNull: fakse | ||
await queryInterface.addColumn( | ||
'Accounts', | ||
'accountTypeId', | ||
{ | ||
type: Sequelize.INTEGER, | ||
allowNull: true, | ||
references: { | ||
model: 'AccountTypes', | ||
key: 'id', | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'SET NULL', | ||
}, | ||
{ transaction }, | ||
); | ||
|
||
// Map ENUM values back to numeric IDs | ||
for (let i = 0; i < Object.values(ACCOUNT_TYPES).length; i++) { | ||
await queryInterface.sequelize.query( | ||
`UPDATE "Accounts" SET "accountTypeId" = ${i + 1} WHERE "accountCategory" = '${Object.keys(ACCOUNT_TYPES)[i]}'`, | ||
{ transaction } | ||
); | ||
} | ||
|
||
await queryInterface.changeColumn( | ||
'Accounts', | ||
'accountTypeId', | ||
{ | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'AccountTypes', | ||
key: 'id', | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'SET NULL', | ||
}, | ||
{ transaction }, | ||
); | ||
|
||
// Remove the ENUM accountTypeId column | ||
await queryInterface.removeColumn('Accounts', 'accountCategory', { transaction }); | ||
|
||
// Drop the ENUM type | ||
await queryInterface.sequelize.query('DROP TYPE "enum_Accounts_accountCategory"', { transaction }); | ||
|
||
await transaction.commit(); | ||
} catch (err) { | ||
await transaction.rollback(); | ||
throw err; | ||
} | ||
}, | ||
}; |
This file contains 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 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 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