mirror of
https://github.com/whyour/qinglong.git
synced 2025-05-29 02:56:08 +08:00
修改迁移数据库逻辑
This commit is contained in:
parent
14134842b0
commit
83f6d9926a
|
@ -1,7 +1,6 @@
|
||||||
import DataStore from 'nedb';
|
import DataStore from 'nedb';
|
||||||
import config from '../config';
|
import config from '../config';
|
||||||
import Logger from './logger';
|
import Logger from './logger';
|
||||||
import fs from 'fs';
|
|
||||||
import { fileExist } from '../config/util';
|
import { fileExist } from '../config/util';
|
||||||
import { EnvModel } from '../data/env';
|
import { EnvModel } from '../data/env';
|
||||||
import { CrontabModel } from '../data/cron';
|
import { CrontabModel } from '../data/cron';
|
||||||
|
@ -10,54 +9,73 @@ import { AppModel } from '../data/open';
|
||||||
import { AuthModel } from '../data/auth';
|
import { AuthModel } from '../data/auth';
|
||||||
import { sequelize } from '../data';
|
import { sequelize } from '../data';
|
||||||
|
|
||||||
interface Dbs {
|
|
||||||
cronDb: DataStore;
|
|
||||||
dependenceDb: DataStore;
|
|
||||||
envDb: DataStore;
|
|
||||||
appDb: DataStore;
|
|
||||||
authDb: DataStore;
|
|
||||||
}
|
|
||||||
|
|
||||||
const db: Dbs = {} as any;
|
|
||||||
|
|
||||||
async function truncateDb() {
|
|
||||||
return new Promise(async (resolve) => {
|
|
||||||
const files = [
|
|
||||||
config.cronDbFile,
|
|
||||||
config.dependenceDbFile,
|
|
||||||
config.envDbFile,
|
|
||||||
config.appDbFile,
|
|
||||||
config.authDbFile,
|
|
||||||
];
|
|
||||||
|
|
||||||
for (const file of files) {
|
|
||||||
const _fileExist = await fileExist(file);
|
|
||||||
if (_fileExist && fs.statSync(file).size >= 1024 * 1024 * 500) {
|
|
||||||
fs.truncateSync(file, 1024 * 1024 * 500);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resolve(null);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
export default async () => {
|
export default async () => {
|
||||||
try {
|
try {
|
||||||
await truncateDb();
|
const crondbExist = await fileExist(config.cronDbFile);
|
||||||
|
const dependenceDbExist = await fileExist(config.dependenceDbFile);
|
||||||
|
const envDbExist = await fileExist(config.envDbFile);
|
||||||
|
const appDbExist = await fileExist(config.appDbFile);
|
||||||
|
const authDbExist = await fileExist(config.authDbFile);
|
||||||
|
|
||||||
db.cronDb = new DataStore({ filename: config.cronDbFile, autoload: true });
|
const cronCount = await CrontabModel.count();
|
||||||
db.dependenceDb = new DataStore({
|
const dependenceCount = await DependenceModel.count();
|
||||||
filename: config.dependenceDbFile,
|
const envCount = await EnvModel.count();
|
||||||
autoload: true,
|
const appCount = await AppModel.count();
|
||||||
});
|
const authCount = await AuthModel.count();
|
||||||
db.envDb = new DataStore({ filename: config.envDbFile, autoload: true });
|
if (crondbExist && cronCount === 0) {
|
||||||
db.appDb = new DataStore({ filename: config.appDbFile, autoload: true });
|
const cronDb = new DataStore({
|
||||||
db.authDb = new DataStore({ filename: config.authDbFile, autoload: true });
|
filename: config.cronDbFile,
|
||||||
|
autoload: true,
|
||||||
|
});
|
||||||
|
cronDb.persistence.compactDatafile();
|
||||||
|
cronDb.find({}).exec(async (err, docs) => {
|
||||||
|
await CrontabModel.bulkCreate(docs, { ignoreDuplicates: true });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// compaction data file
|
if (dependenceDbExist && dependenceCount === 0) {
|
||||||
db.cronDb.persistence.compactDatafile();
|
const dependenceDb = new DataStore({
|
||||||
db.envDb.persistence.compactDatafile();
|
filename: config.dependenceDbFile,
|
||||||
db.dependenceDb.persistence.compactDatafile();
|
autoload: true,
|
||||||
db.appDb.persistence.compactDatafile();
|
});
|
||||||
db.authDb.persistence.compactDatafile();
|
dependenceDb.persistence.compactDatafile();
|
||||||
|
dependenceDb.find({}).exec(async (err, docs) => {
|
||||||
|
await DependenceModel.bulkCreate(docs, { ignoreDuplicates: true });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (envDbExist && envCount === 0) {
|
||||||
|
const envDb = new DataStore({
|
||||||
|
filename: config.envDbFile,
|
||||||
|
autoload: true,
|
||||||
|
});
|
||||||
|
envDb.persistence.compactDatafile();
|
||||||
|
envDb.find({}).exec(async (err, docs) => {
|
||||||
|
await EnvModel.bulkCreate(docs, { ignoreDuplicates: true });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (appDbExist && appCount === 0) {
|
||||||
|
const appDb = new DataStore({
|
||||||
|
filename: config.appDbFile,
|
||||||
|
autoload: true,
|
||||||
|
});
|
||||||
|
appDb.persistence.compactDatafile();
|
||||||
|
appDb.find({}).exec(async (err, docs) => {
|
||||||
|
await AppModel.bulkCreate(docs, { ignoreDuplicates: true });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (authDbExist && authCount === 0) {
|
||||||
|
const authDb = new DataStore({
|
||||||
|
filename: config.authDbFile,
|
||||||
|
autoload: true,
|
||||||
|
});
|
||||||
|
authDb.persistence.compactDatafile();
|
||||||
|
authDb.find({}).exec(async (err, docs) => {
|
||||||
|
await AuthModel.bulkCreate(docs, { ignoreDuplicates: true });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await sequelize.sync({ alter: true });
|
await sequelize.sync({ alter: true });
|
||||||
|
@ -65,40 +83,9 @@ export default async () => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// migrate db to sqlite
|
|
||||||
try {
|
|
||||||
const count = await CrontabModel.count();
|
|
||||||
if (count !== 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
db.cronDb.find({}).exec(async (err, docs) => {
|
|
||||||
await CrontabModel.bulkCreate(docs, { ignoreDuplicates: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
db.dependenceDb.find({}).exec(async (err, docs) => {
|
|
||||||
await DependenceModel.bulkCreate(docs, { ignoreDuplicates: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
db.envDb.find({}).exec(async (err, docs) => {
|
|
||||||
await EnvModel.bulkCreate(docs, { ignoreDuplicates: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
db.appDb.find({}).exec(async (err, docs) => {
|
|
||||||
await AppModel.bulkCreate(docs, { ignoreDuplicates: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
db.authDb.find({}).exec(async (err, docs) => {
|
|
||||||
await AuthModel.bulkCreate(docs, { ignoreDuplicates: true });
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger.info('✌️ DB loaded');
|
Logger.info('✌️ DB loaded');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
Logger.info('✌️ DB load failed');
|
Logger.info('✌️ DB load failed');
|
||||||
Logger.info(error);
|
Logger.info(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const dbs: Dbs = db;
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { Service, Inject } from 'typedi';
|
import { Service, Inject } from 'typedi';
|
||||||
import winston from 'winston';
|
import winston from 'winston';
|
||||||
import config from '../config';
|
import config from '../config';
|
||||||
import DataStore from 'nedb';
|
|
||||||
import {
|
import {
|
||||||
Dependence,
|
Dependence,
|
||||||
InstallDependenceCommandTypes,
|
InstallDependenceCommandTypes,
|
||||||
|
|
|
@ -3,7 +3,6 @@ import winston from 'winston';
|
||||||
import { getFileContentByName } from '../config/util';
|
import { getFileContentByName } from '../config/util';
|
||||||
import config from '../config';
|
import config from '../config';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import DataStore from 'nedb';
|
|
||||||
import { Env, EnvModel, EnvStatus, initEnvPosition } from '../data/env';
|
import { Env, EnvModel, EnvStatus, initEnvPosition } from '../data/env';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { Op } from 'sequelize';
|
import { Op } from 'sequelize';
|
||||||
|
|
|
@ -2,7 +2,6 @@ import { Service, Inject } from 'typedi';
|
||||||
import winston from 'winston';
|
import winston from 'winston';
|
||||||
import { createRandomString } from '../config/util';
|
import { createRandomString } from '../config/util';
|
||||||
import config from '../config';
|
import config from '../config';
|
||||||
import DataStore from 'nedb';
|
|
||||||
import { App, AppModel } from '../data/open';
|
import { App, AppModel } from '../data/open';
|
||||||
import { v4 as uuidV4 } from 'uuid';
|
import { v4 as uuidV4 } from 'uuid';
|
||||||
import sequelize, { Op } from 'sequelize';
|
import sequelize, { Op } from 'sequelize';
|
||||||
|
|
|
@ -10,7 +10,6 @@ import ScheduleService from './schedule';
|
||||||
import { spawn } from 'child_process';
|
import { spawn } from 'child_process';
|
||||||
import SockService from './sock';
|
import SockService from './sock';
|
||||||
import got from 'got';
|
import got from 'got';
|
||||||
import { dbs } from '../loaders/db';
|
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class SystemService {
|
export default class SystemService {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user