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.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. /** @type {import('sequelize-cli').Migration} */
  3. module.exports = {
  4. async up (queryInterface, Sequelize) {
  5. // Rename table from candles_1h to candles
  6. await queryInterface.renameTable('candles_1h', 'candles');
  7. // Add timeframe column with enum
  8. await queryInterface.addColumn('candles', 'timeframe', {
  9. type: Sequelize.ENUM('15m', '30m', '1h', '1D', '1W', '1M'),
  10. allowNull: false,
  11. defaultValue: '1h'
  12. });
  13. // Update existing records to have '1h' timeframe
  14. await queryInterface.sequelize.query('UPDATE candles SET timeframe = \'1h\' WHERE timeframe IS NULL');
  15. // Remove the default value after setting existing records
  16. await queryInterface.changeColumn('candles', 'timeframe', {
  17. type: Sequelize.ENUM('15m', '30m', '1h', '1D', '1W', '1M'),
  18. allowNull: false
  19. });
  20. // Drop the old unique constraint
  21. await queryInterface.removeConstraint('candles', 'unique_symbol_open_time');
  22. // Add new unique constraint including timeframe
  23. await queryInterface.addConstraint('candles', {
  24. fields: ['symbol_id', 'open_time', 'timeframe'],
  25. type: 'unique',
  26. name: 'unique_symbol_open_time_timeframe'
  27. });
  28. // Update indexes to include timeframe
  29. await queryInterface.removeIndex('candles', 'idx_candles_open_time');
  30. await queryInterface.addIndex('candles', ['open_time', 'timeframe'], {
  31. name: 'idx_candles_open_time_timeframe'
  32. });
  33. },
  34. async down (queryInterface, Sequelize) {
  35. // Reverse the changes
  36. // Remove new indexes
  37. await queryInterface.removeIndex('candles', 'idx_candles_open_time_timeframe');
  38. // Add back old index
  39. await queryInterface.addIndex('candles', ['open_time'], {
  40. name: 'idx_candles_open_time'
  41. });
  42. // Remove new constraint
  43. await queryInterface.removeConstraint('candles', 'unique_symbol_open_time_timeframe');
  44. // Add back old constraint
  45. await queryInterface.addConstraint('candles', {
  46. fields: ['symbol_id', 'open_time'],
  47. type: 'unique',
  48. name: 'unique_symbol_open_time'
  49. });
  50. // Remove timeframe column
  51. await queryInterface.removeColumn('candles', 'timeframe');
  52. // Rename table back to candles_1h
  53. await queryInterface.renameTable('candles', 'candles_1h');
  54. }
  55. };