UNPKG

@tomei/live-price

Version:

Tomei live-price Package

113 lines (92 loc) 4.77 kB
'use strict'; /** @type {import('sequelize-cli').Migration} */ module.exports = { async up(queryInterface, Sequelize) { const transaction = await queryInterface.sequelize.transaction(); try { const tableDescription = await queryInterface.describeTable('price_TomeiPriceLatest'); if (!tableDescription.CompanyCode) { await queryInterface.addColumn('price_TomeiPriceLatest', 'CompanyCode', { type: Sequelize.STRING(10), allowNull: false, defaultValue: 'CTH', }, { transaction }); await queryInterface.addIndex('price_TomeiPriceLatest', ['CompanyCode'], { transaction }); // Try to update the primary key to include CompanyCode (may fail due to foreign keys) try { // First, try to find and temporarily drop foreign key constraints const foreignKeys = await queryInterface.sequelize.query(` SELECT CONSTRAINT_NAME, TABLE_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_NAME = 'price_TomeiPriceLatest' AND REFERENCED_TABLE_SCHEMA = DATABASE() AND CONSTRAINT_NAME != 'PRIMARY' `, { type: queryInterface.sequelize.QueryTypes.SELECT, transaction }); // Drop foreign key constraints temporarily for (const fk of foreignKeys) { try { await queryInterface.removeConstraint(fk.TABLE_NAME, fk.CONSTRAINT_NAME, { transaction }); console.log(`Temporarily removed foreign key: ${fk.CONSTRAINT_NAME} from ${fk.TABLE_NAME}`); } catch (fkError) { console.log(`Could not remove foreign key ${fk.CONSTRAINT_NAME}: ${fkError.message}`); } } // Now try to drop and recreate the primary key await queryInterface.removeConstraint('price_TomeiPriceLatest', 'PRIMARY', { transaction }); await queryInterface.addConstraint('price_TomeiPriceLatest', { fields: ['FeedName', 'Currency', 'CompanyCode'], type: 'primary key', name: 'PRIMARY' }, { transaction }); // Recreate foreign key constraints with new primary key for (const fk of foreignKeys) { try { // This would need the original constraint definition - for now we'll skip recreation console.log(`Foreign key ${fk.CONSTRAINT_NAME} needs to be manually recreated`); } catch (fkError) { console.log(`Could not recreate foreign key ${fk.CONSTRAINT_NAME}: ${fkError.message}`); } } console.log('Successfully updated primary key for price_TomeiPriceLatest'); } catch (error) { console.log('Could not update primary key for price_TomeiPriceLatest (likely due to foreign key constraints):', error.message); console.log('This is not critical - the comprehensive migration will handle this later'); } } await transaction.commit(); } catch (error) { await transaction.rollback(); throw error; } }, async down(queryInterface, Sequelize) { const transaction = await queryInterface.sequelize.transaction(); try { const tableDescription = await queryInterface.describeTable('price_TomeiPriceLatest'); if (tableDescription.CompanyCode) { await queryInterface.removeIndex('price_TomeiPriceLatest', ['CompanyCode'], { transaction }); // Try to restore original primary key (may fail due to foreign keys) try { console.log('Attempting to restore original primary key for price_TomeiPriceLatest...'); await queryInterface.removeConstraint('price_TomeiPriceLatest', 'PRIMARY', { transaction }); await queryInterface.addConstraint('price_TomeiPriceLatest', { fields: ['FeedName', 'Currency'], type: 'primary key', name: 'PRIMARY' }, { transaction }); console.log('Successfully restored original primary key'); } catch (error) { console.log('Could not restore original primary key (likely due to foreign key constraints):', error.message); console.log('This may require manual intervention to fix foreign key references'); } await queryInterface.removeColumn('price_TomeiPriceLatest', 'CompanyCode', { transaction }); } await transaction.commit(); } catch (error) { await transaction.rollback(); throw error; } } };