lrf 6 months ago
parent
commit
86efe34344

+ 16 - 1
src/service/dbBackup.service.ts

@@ -47,16 +47,31 @@ export class DbBackupService {
     });
     for (const tableName of dir) {
       // 清空表, 重置id;
+      console.log(tableName);
       const filePath = path.resolve(dbBackUpPathDir, `${tableName}.json`);
       let datas = fs.readFileSync(filePath, 'utf-8');
       if (datas) datas = JSON.parse(datas);
+      console.log(`${tableName} start`);
       await this.defaultDataSource.query(`TRUNCATE TABLE "public"."${tableName}" RESTART IDENTITY RESTRICT;`);
       await this.defaultDataSource.query(`SELECT setval('"${tableName}_id_seq"', (SELECT max(id) FROM "${tableName}"));`);
       const entities: any = this.defaultDataSource.options.entities;
       const entity = entities.find(f => get(f, 'name') === `${upperFirst(tableName)}`);
       for (const i of datas) {
         try {
-          await this.defaultDataSource.manager.createQueryBuilder().insert().into(entity).values(i).execute();
+          // 需要处理id的问题
+          const data_id = get(i, 'id', 1);
+          // 获取当前表id的序列
+          const lastIdResult = await this.defaultDataSource.query(`SELECT max(id) FROM "${tableName}"`);
+          let last_id = get(head(lastIdResult), 'max', 0);
+          // 判断same_id: 表中最后一个id和当前数据id是否一致
+          let same_id = last_id === data_id;
+          if (!same_id && data_id > 1) {
+            // 不一致:只有一种情况----当前数据的id比表中的数据id要大.那就直接执行id补上
+
+            await this.defaultDataSource.query(`SELECT setval('"${tableName}_id_seq"', ${data_id - 1});`);
+          }
+          const builder = await this.defaultDataSource.manager.createQueryBuilder().insert().into(entity);
+          await builder.values(i).execute();
         } catch (error) {
           console.log(`${tableName} - ${get(i, 'id')} -  发生错误`);
           console.log(error);

+ 1 - 1
src/service/elasticsearch/es.service.ts

@@ -58,7 +58,7 @@ export class ESService {
     const tableList = get(result, 'data.data', []);
     if (!tableList) return;
     for (const index of tableList) {
-      this.initData(index);
+      await this.initData(index);
     }
   }
   /**

+ 1 - 1
src/service/serviceUtil.service.ts

@@ -37,7 +37,7 @@ export class ServiceUtilService {
     const user = this.ctx.user;
     if (user && get(user, 'id')) {
       const collectionData = await this.collectionService.fetch({ user: get(user, 'id'), source: get(data, 'id'), type });
-      if (collectionData) data.is_collection = true;
+      if (data && collectionData) data.is_collection = true;
     }
     return data;
   }