| 12345678910111213141516171819202122232425262728293031323334 |
- 'use strict';
- /** @type {import('sequelize-cli').Migration} */
- module.exports = {
- async up (queryInterface, Sequelize) {
- // Check if table exists
- const tableExists = await queryInterface.sequelize.query(
- "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'symbols');",
- { type: Sequelize.QueryTypes.SELECT }
- );
-
- if (tableExists && tableExists[0] && tableExists[0].exists) {
- // Drop the existing CHECK constraint if it exists and add a new one with 'index'
- await queryInterface.sequelize.query("ALTER TABLE symbols DROP CONSTRAINT IF EXISTS symbols_instrument_type_check;");
- await queryInterface.sequelize.query("ALTER TABLE symbols ADD CONSTRAINT symbols_instrument_type_check CHECK (instrument_type IN ('crypto', 'stock', 'forex', 'commodity', 'index'));");
- } else {
- console.log('Table symbols does not exist, skipping constraint update. Tables will be created by model sync.');
- }
- },
- async down (queryInterface, Sequelize) {
- const tableExists = await queryInterface.sequelize.query(
- "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'symbols');",
- { type: Sequelize.QueryTypes.SELECT }
- );
-
- if (tableExists && tableExists[0] && tableExists[0].exists) {
- // Revert the CHECK constraint without 'index'
- await queryInterface.sequelize.query("ALTER TABLE symbols DROP CONSTRAINT IF EXISTS symbols_instrument_type_check;");
- await queryInterface.sequelize.query("ALTER TABLE symbols ADD CONSTRAINT symbols_instrument_type_check CHECK (instrument_type IN ('crypto', 'stock', 'forex', 'commodity'));");
- }
- }
- };
|