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.

20251112102032-add-timeframe-to-candles.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. /** @type {import('sequelize-cli').Migration} */
  3. module.exports = {
  4. async up (queryInterface, Sequelize) {
  5. // Check if candles_1h table exists
  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. console.log('Table candles_1h does not exist, skipping migration. Tables will be created by model sync with timeframe support.');
  12. return;
  13. }
  14. // Rename table from candles_1h to candles
  15. await queryInterface.renameTable('candles_1h', 'candles');
  16. // Add timeframe column with enum
  17. await queryInterface.addColumn('candles', 'timeframe', {
  18. type: Sequelize.ENUM('15m', '30m', '1h', '1D', '1W', '1M'),
  19. allowNull: false,
  20. defaultValue: '1h'
  21. });
  22. // Update existing records to have '1h' timeframe
  23. await queryInterface.sequelize.query('UPDATE candles SET timeframe = \'1h\' WHERE timeframe IS NULL');
  24. // Remove the default value after setting existing records
  25. await queryInterface.changeColumn('candles', 'timeframe', {
  26. type: Sequelize.ENUM('15m', '30m', '1h', '1D', '1W', '1M'),
  27. allowNull: false
  28. });
  29. // Drop the old unique constraint
  30. await queryInterface.removeConstraint('candles', 'unique_symbol_open_time');
  31. // Add new unique constraint including timeframe
  32. await queryInterface.addConstraint('candles', {
  33. fields: ['symbol_id', 'open_time', 'timeframe'],
  34. type: 'unique',
  35. name: 'unique_symbol_open_time_timeframe'
  36. });
  37. // Update indexes to include timeframe
  38. await queryInterface.removeIndex('candles', 'idx_candles_open_time');
  39. await queryInterface.addIndex('candles', ['open_time', 'timeframe'], {
  40. name: 'idx_candles_open_time_timeframe'
  41. });
  42. },
  43. async down (queryInterface, Sequelize) {
  44. // Reverse the changes
  45. // Remove new indexes
  46. await queryInterface.removeIndex('candles', 'idx_candles_open_time_timeframe');
  47. // Add back old index
  48. await queryInterface.addIndex('candles', ['open_time'], {
  49. name: 'idx_candles_open_time'
  50. });
  51. // Remove new constraint
  52. await queryInterface.removeConstraint('candles', 'unique_symbol_open_time_timeframe');
  53. // Add back old constraint
  54. await queryInterface.addConstraint('candles', {
  55. fields: ['symbol_id', 'open_time'],
  56. type: 'unique',
  57. name: 'unique_symbol_open_time'
  58. });
  59. // Remove timeframe column
  60. await queryInterface.removeColumn('candles', 'timeframe');
  61. // Rename table back to candles_1h
  62. await queryInterface.renameTable('candles', 'candles_1h');
  63. }
  64. };