|
@@ -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);
|