Skip to content

Commit 3170efa

Browse files
author
Artem
committed
add migration old data to the new home directory
1 parent 1166a29 commit 3170efa

File tree

7 files changed

+47
-15
lines changed

7 files changed

+47
-15
lines changed

redisinsight/api/config/default.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { join } from 'path';
22

3-
const homedir = join(__dirname, '..');
3+
const prevHomedir = join(__dirname, '..', '.redisinsight-v2.0-dev');
4+
5+
const homedir = join(__dirname, '..', '.redisinsight-v2-dev');
46

57
const buildInfoFileName = 'build.json';
68
const dataZipFileName = 'data.zip';
79

8-
910
const staticDir = process.env.BUILD_TYPE === 'ELECTRON' && process['resourcesPath']
1011
? join(process['resourcesPath'], 'static')
1112
: join(__dirname, '..', 'static');
@@ -17,6 +18,7 @@ const defaultsDir = process.env.BUILD_TYPE === 'ELECTRON' && process['resourcesP
1718
export default {
1819
dir_path: {
1920
homedir,
21+
prevHomedir,
2022
staticDir,
2123
defaultsDir,
2224
logs: join(homedir, 'logs'),

redisinsight/api/config/production.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import { join } from 'path';
22
import * as os from 'os';
33

44
const homedir = process.env.APP_FOLDER_ABSOLUTE_PATH
5-
|| (join(os.homedir(), process.env.APP_FOLDER_NAME || '.redisinsight-preview'));
5+
|| (join(os.homedir(), process.env.APP_FOLDER_NAME || '.redisinsight-v2'));
6+
7+
const prevHomedir = join(os.homedir(), '.redisinsight-preview');
68

79
export default {
810
dir_path: {
911
homedir,
12+
prevHomedir,
1013
logs: join(homedir, 'logs'),
1114
customPlugins: join(homedir, 'plugins'),
1215
commands: join(homedir, 'commands'),

redisinsight/api/config/staging.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import { join } from 'path';
22
import * as os from 'os';
33

44
const homedir = process.env.APP_FOLDER_ABSOLUTE_PATH
5-
|| (join(os.homedir(), process.env.APP_FOLDER_NAME || '.redisinsight-v2.0-stage'));
5+
|| (join(os.homedir(), process.env.APP_FOLDER_NAME || '.redisinsight-v2-stage'));
6+
7+
const prevHomedir = join(os.homedir(), '.redisinsight-v2.0-stage');
68

79
export default {
810
dir_path: {
911
homedir,
12+
prevHomedir,
1013
logs: join(homedir, 'logs'),
1114
customPlugins: join(homedir, 'plugins'),
1215
commands: join(homedir, 'commands'),

redisinsight/api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redisinsight-api",
3-
"version": "2.0.0",
3+
"version": "2.0.5",
44
"description": "RedisInsight API",
55
"private": true,
66
"author": {

redisinsight/api/src/app.module.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as fs from 'fs';
1+
import * as fs from 'fs-extra';
22
import {
33
MiddlewareConsumer, Module, NestModule, OnModuleInit,
44
} from '@nestjs/common';
@@ -73,18 +73,14 @@ const PATH_CONFIG = config.get('dir_path');
7373
providers: [],
7474
})
7575
export class AppModule implements OnModuleInit, NestModule {
76-
onModuleInit() {
76+
async onModuleInit() {
7777
// creating required folders
7878
const foldersToCreate = [
7979
PATH_CONFIG.pluginsAssets,
8080
PATH_CONFIG.customPlugins,
8181
];
8282

83-
foldersToCreate.forEach((folder) => {
84-
if (!fs.existsSync(folder)) {
85-
fs.mkdirSync(folder, { recursive: true });
86-
}
87-
});
83+
await Promise.all(foldersToCreate.map(fs.ensureDir));
8884
}
8985

9086
configure(consumer: MiddlewareConsumer) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as fs from 'fs-extra';
2+
import config from 'src/utils/config';
3+
import { join } from 'path';
4+
5+
const PATH_CONFIG = config.get('dir_path');
6+
const DB_CONFIG = config.get('db');
7+
8+
export const migrateHomeFolder = async () => {
9+
try {
10+
if (!await fs.pathExists(DB_CONFIG.database) && await fs.pathExists(PATH_CONFIG.prevHomedir)) {
11+
await fs.ensureDir(PATH_CONFIG.homedir);
12+
13+
await Promise.all([
14+
'redisinsight.db',
15+
'plugins',
16+
'plugins2',
17+
].map((target) => fs.copy(
18+
join(PATH_CONFIG.prevHomedir, target),
19+
join(PATH_CONFIG.homedir, target),
20+
).catch()));
21+
}
22+
} catch (e) {
23+
// continue initialization even without migration
24+
}
25+
};

redisinsight/api/src/main.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import 'dotenv/config'
1+
import 'dotenv/config';
22
import { NestFactory } from '@nestjs/core';
33
import { SwaggerModule } from '@nestjs/swagger';
44
import { NestApplicationOptions } from '@nestjs/common';
55
import * as bodyParser from 'body-parser';
66
import { WinstonModule } from 'nest-winston';
77
import { GlobalExceptionFilter } from 'src/exceptions/global-exception.filter';
8+
import { get } from 'src/utils';
9+
import { migrateHomeFolder } from 'src/init-helper';
810
import { AppModule } from './app.module';
911
import SWAGGER_CONFIG from '../config/swagger';
1012
import LOGGER_CONFIG from '../config/logger';
11-
import config from './utils/config';
1213

1314
export default async function bootstrap() {
14-
const serverConfig = config.get('server');
15+
await migrateHomeFolder();
16+
17+
const serverConfig = get('server');
1518
const port = process.env.API_PORT || serverConfig.port;
1619
const logger = WinstonModule.createLogger(LOGGER_CONFIG);
1720

0 commit comments

Comments
 (0)