Market Data Service is a high-performance financial data API that provides comprehensive Symbol prices of different markets through both RESTful endpoints and real-time WebSocket connections.

20251016210526-add_unique_constraint_candles.js 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. /** @type {import('sequelize-cli').Migration} */
  3. module.exports = {
  4. async up (queryInterface, Sequelize) {
  5. // Check if table exists before adding constraint
  6. const tableExists = await queryInterface.sequelize.query(
  7. "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'candles_1h');",
  8. { type: Sequelize.QueryTypes.SELECT }
  9. );
  10. if (tableExists && tableExists[0] && tableExists[0].exists) {
  11. // Check if constraint already exists
  12. const constraintExists = await queryInterface.sequelize.query(
  13. "SELECT EXISTS (SELECT FROM pg_constraint WHERE conname = 'unique_symbol_open_time');",
  14. { type: Sequelize.QueryTypes.SELECT }
  15. );
  16. if (!constraintExists || !constraintExists[0] || !constraintExists[0].exists) {
  17. await queryInterface.addConstraint('candles_1h', {
  18. fields: ['symbol_id', 'open_time'],
  19. type: 'unique',
  20. name: 'unique_symbol_open_time'
  21. });
  22. }
  23. } else {
  24. console.log('Table candles_1h does not exist, skipping constraint addition. Tables will be created by model sync.');
  25. }
  26. },
  27. async down (queryInterface, Sequelize) {
  28. const tableExists = await queryInterface.sequelize.query(
  29. "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'candles_1h');",
  30. { type: Sequelize.QueryTypes.SELECT }
  31. );
  32. if (tableExists && tableExists[0] && tableExists[0].exists) {
  33. await queryInterface.removeConstraint('candles_1h', 'unique_symbol_open_time');
  34. }
  35. }
  36. };