123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- const sequelize = require('../config/database');
- const { DataTypes } = require('sequelize');
- async function migrateSubscriptions() {
- try {
- console.log('开始创建订阅表...');
-
- // 创建订阅表
- await sequelize.getQueryInterface().createTable('subscriptions', {
- id: {
- type: DataTypes.INTEGER,
- primaryKey: true,
- autoIncrement: true
- },
- name: {
- type: DataTypes.STRING(100),
- allowNull: false,
- comment: '订阅名称'
- },
- url: {
- type: DataTypes.STRING(500),
- allowNull: false,
- comment: '订阅链接'
- },
- description: {
- type: DataTypes.TEXT,
- allowNull: true,
- comment: '订阅描述'
- },
- speedTestConfig: {
- type: DataTypes.JSON,
- allowNull: true,
- comment: '测速配置'
- },
- notifyConfig: {
- type: DataTypes.JSON,
- allowNull: true,
- comment: '通知配置'
- },
- isActive: {
- type: DataTypes.BOOLEAN,
- allowNull: false,
- defaultValue: true,
- comment: '是否启用'
- },
- lastUpdateTime: {
- type: DataTypes.DATE,
- allowNull: true,
- comment: '最后更新时间'
- },
- nodeCount: {
- type: DataTypes.INTEGER,
- allowNull: false,
- defaultValue: 0,
- comment: '节点数量'
- },
- createdAt: {
- type: DataTypes.DATE,
- allowNull: false,
- defaultValue: DataTypes.NOW
- },
- updatedAt: {
- type: DataTypes.DATE,
- allowNull: false,
- defaultValue: DataTypes.NOW
- }
- });
- // 创建索引
- await sequelize.getQueryInterface().addIndex('subscriptions', ['isActive']);
- await sequelize.getQueryInterface().addIndex('subscriptions', ['name']);
- console.log('订阅表创建成功');
- // 为nodes表添加subscriptionId字段
- console.log('为nodes表添加subscriptionId字段...');
-
- try {
- await sequelize.getQueryInterface().addColumn('nodes', 'subscriptionId', {
- type: DataTypes.INTEGER,
- allowNull: true,
- comment: '所属订阅ID'
- });
-
- await sequelize.getQueryInterface().addIndex('nodes', ['subscriptionId']);
- console.log('nodes表subscriptionId字段添加成功');
- } catch (error) {
- if (error.message.includes('already exists')) {
- console.log('subscriptionId字段已存在,跳过');
- } else {
- throw error;
- }
- }
- console.log('数据库迁移完成');
- } catch (error) {
- console.error('数据库迁移失败:', error.message);
- throw error;
- }
- }
- // 如果直接运行此文件,执行迁移
- if (require.main === module) {
- migrateSubscriptions()
- .then(() => {
- console.log('迁移完成');
- process.exit(0);
- })
- .catch((error) => {
- console.error('迁移失败:', error);
- process.exit(1);
- });
- }
- module.exports = migrateSubscriptions;
|