UNPKG

@tomei/live-price

Version:

Tomei live-price Package

141 lines (123 loc) 6.67 kB
'use strict'; /** @type {import('sequelize-cli').Migration} */ module.exports = { async up(queryInterface, Sequelize) { console.log('Starting comprehensive CompanyCode update...'); const transaction = await queryInterface.sequelize.transaction(); try { // Step 1: Ensure CompanyCode exists in price_TomeiPriceHistory const historyTableDesc = await queryInterface.describeTable('price_TomeiPriceHistory'); if (!historyTableDesc.CompanyCode) { console.log('Adding CompanyCode column to price_TomeiPriceHistory...'); await queryInterface.addColumn('price_TomeiPriceHistory', 'CompanyCode', { type: Sequelize.STRING(10), allowNull: false, defaultValue: 'CTH' }, { transaction }); await queryInterface.addIndex('price_TomeiPriceHistory', ['CompanyCode'], { transaction }); await queryInterface.addIndex('price_TomeiPriceHistory', ['FeedName', 'Currency', 'CompanyCode'], { transaction }); } // Step 2: Ensure CompanyCode exists in price_TomeiPriceLatest const latestTableDesc = await queryInterface.describeTable('price_TomeiPriceLatest'); if (!latestTableDesc.CompanyCode) { console.log('Adding CompanyCode column to price_TomeiPriceLatest...'); await queryInterface.addColumn('price_TomeiPriceLatest', 'CompanyCode', { type: Sequelize.STRING(10), allowNull: false, defaultValue: 'CTH' }, { transaction }); await queryInterface.addIndex('price_TomeiPriceLatest', ['CompanyCode'], { transaction }); } // Step 3: Ensure CompanyCode exists in price_FeedManualPriceHistory const manualTableDesc = await queryInterface.describeTable('price_FeedManualPriceHistory'); if (!manualTableDesc.CompanyCode) { console.log('Adding CompanyCode column to price_FeedManualPriceHistory...'); await queryInterface.addColumn('price_FeedManualPriceHistory', 'CompanyCode', { type: Sequelize.STRING(10), allowNull: true, defaultValue: 'CTH', }, { transaction }); await queryInterface.addIndex('price_FeedManualPriceHistory', ['CompanyCode'], { transaction }); await queryInterface.addIndex('price_FeedManualPriceHistory', { fields: ['FeedName', 'CompanyCode', 'Status'], name: 'idx_manual_price_history_feed_company_status' }, { transaction }); } // Step 4: Update existing data to use CTH as default console.log('Updating existing records to use CTH default...'); await queryInterface.sequelize.query(` UPDATE price_TomeiPriceHistory SET CompanyCode = 'CTH' WHERE CompanyCode IS NULL OR CompanyCode = 'CTH' `, { transaction }); await queryInterface.sequelize.query(` UPDATE price_TomeiPriceLatest SET CompanyCode = 'CTH' WHERE CompanyCode IS NULL OR CompanyCode = 'CTH' `, { transaction }); await queryInterface.sequelize.query(` UPDATE price_FeedManualPriceHistory SET CompanyCode = 'CTH' WHERE CompanyCode IS NULL OR CompanyCode = 'CTH' `, { transaction }); // Step 5: Try to update primary key for price_TomeiPriceLatest (may fail due to constraints) try { console.log('Attempting to update primary key for price_TomeiPriceLatest...'); const indexes = await queryInterface.showIndex('price_TomeiPriceLatest'); const hasPrimaryKey = indexes.some(index => index.primary); if (hasPrimaryKey) { // Remove existing primary key and add new one with CompanyCode await queryInterface.removeConstraint('price_TomeiPriceLatest', 'PRIMARY', { transaction }); await queryInterface.addConstraint('price_TomeiPriceLatest', { fields: ['FeedName', 'Currency', 'CompanyCode'], type: 'primary key', name: 'PRIMARY' }, { transaction }); 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 application can work with the current constraint structure'); } await transaction.commit(); console.log('CompanyCode comprehensive update completed successfully'); } catch (error) { await transaction.rollback(); console.error('Error in comprehensive CompanyCode update:', error); throw error; } }, async down(queryInterface, Sequelize) { console.log('Rolling back comprehensive CompanyCode update...'); const transaction = await queryInterface.sequelize.transaction(); try { // Remove indexes first await queryInterface.removeIndex('price_FeedManualPriceHistory', 'idx_manual_price_history_feed_company_status', { transaction }); await queryInterface.removeIndex('price_FeedManualPriceHistory', ['CompanyCode'], { transaction }); await queryInterface.removeIndex('price_TomeiPriceHistory', ['FeedName', 'Currency', 'CompanyCode'], { transaction }); await queryInterface.removeIndex('price_TomeiPriceHistory', ['CompanyCode'], { transaction }); await queryInterface.removeIndex('price_TomeiPriceLatest', ['CompanyCode'], { transaction }); // Try to restore original primary key for price_TomeiPriceLatest try { await queryInterface.removeConstraint('price_TomeiPriceLatest', 'PRIMARY', { transaction }); await queryInterface.addConstraint('price_TomeiPriceLatest', { fields: ['FeedName', 'Currency'], type: 'primary key', name: 'PRIMARY' }, { transaction }); } catch (error) { console.log('Could not restore original primary key:', error.message); } // Remove CompanyCode columns await queryInterface.removeColumn('price_FeedManualPriceHistory', 'CompanyCode', { transaction }); await queryInterface.removeColumn('price_TomeiPriceLatest', 'CompanyCode', { transaction }); await queryInterface.removeColumn('price_TomeiPriceHistory', 'CompanyCode', { transaction }); await transaction.commit(); console.log('CompanyCode rollback completed'); } catch (error) { await transaction.rollback(); console.error('Error in CompanyCode rollback:', error); throw error; } } };