migrate_subscriptions.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const sequelize = require('../config/database');
  2. const { DataTypes } = require('sequelize');
  3. async function migrateSubscriptions() {
  4. try {
  5. console.log('开始创建订阅表...');
  6. // 创建订阅表
  7. await sequelize.getQueryInterface().createTable('subscriptions', {
  8. id: {
  9. type: DataTypes.INTEGER,
  10. primaryKey: true,
  11. autoIncrement: true
  12. },
  13. name: {
  14. type: DataTypes.STRING(100),
  15. allowNull: false,
  16. comment: '订阅名称'
  17. },
  18. url: {
  19. type: DataTypes.STRING(500),
  20. allowNull: false,
  21. comment: '订阅链接'
  22. },
  23. description: {
  24. type: DataTypes.TEXT,
  25. allowNull: true,
  26. comment: '订阅描述'
  27. },
  28. speedTestConfig: {
  29. type: DataTypes.JSON,
  30. allowNull: true,
  31. comment: '测速配置'
  32. },
  33. notifyConfig: {
  34. type: DataTypes.JSON,
  35. allowNull: true,
  36. comment: '通知配置'
  37. },
  38. isActive: {
  39. type: DataTypes.BOOLEAN,
  40. allowNull: false,
  41. defaultValue: true,
  42. comment: '是否启用'
  43. },
  44. lastUpdateTime: {
  45. type: DataTypes.DATE,
  46. allowNull: true,
  47. comment: '最后更新时间'
  48. },
  49. nodeCount: {
  50. type: DataTypes.INTEGER,
  51. allowNull: false,
  52. defaultValue: 0,
  53. comment: '节点数量'
  54. },
  55. createdAt: {
  56. type: DataTypes.DATE,
  57. allowNull: false,
  58. defaultValue: DataTypes.NOW
  59. },
  60. updatedAt: {
  61. type: DataTypes.DATE,
  62. allowNull: false,
  63. defaultValue: DataTypes.NOW
  64. }
  65. });
  66. // 创建索引
  67. await sequelize.getQueryInterface().addIndex('subscriptions', ['isActive']);
  68. await sequelize.getQueryInterface().addIndex('subscriptions', ['name']);
  69. console.log('订阅表创建成功');
  70. // 为nodes表添加subscriptionId字段
  71. console.log('为nodes表添加subscriptionId字段...');
  72. try {
  73. await sequelize.getQueryInterface().addColumn('nodes', 'subscriptionId', {
  74. type: DataTypes.INTEGER,
  75. allowNull: true,
  76. comment: '所属订阅ID'
  77. });
  78. await sequelize.getQueryInterface().addIndex('nodes', ['subscriptionId']);
  79. console.log('nodes表subscriptionId字段添加成功');
  80. } catch (error) {
  81. if (error.message.includes('already exists')) {
  82. console.log('subscriptionId字段已存在,跳过');
  83. } else {
  84. throw error;
  85. }
  86. }
  87. console.log('数据库迁移完成');
  88. } catch (error) {
  89. console.error('数据库迁移失败:', error.message);
  90. throw error;
  91. }
  92. }
  93. // 如果直接运行此文件,执行迁移
  94. if (require.main === module) {
  95. migrateSubscriptions()
  96. .then(() => {
  97. console.log('迁移完成');
  98. process.exit(0);
  99. })
  100. .catch((error) => {
  101. console.error('迁移失败:', error);
  102. process.exit(1);
  103. });
  104. }
  105. module.exports = migrateSubscriptions;