fix: restore with pg schema

This commit is contained in:
Chareice 2023-02-09 22:00:30 +08:00
parent 4449325489
commit 9c9c39c0cd
2 changed files with 33 additions and 11 deletions

View File

@ -127,12 +127,12 @@ export class Restorer extends AppMigrator {
const metaContent = await fsPromises.readFile(collectionMetaPath, 'utf8'); const metaContent = await fsPromises.readFile(collectionMetaPath, 'utf8');
const meta = JSON.parse(metaContent); const meta = JSON.parse(metaContent);
const tableName = meta.tableName; const tableName = this.quoteTable(meta.tableName);
try { try {
// disable trigger // disable trigger
if (this.app.db.inDialect('postgres')) { if (this.app.db.inDialect('postgres')) {
await this.app.db.sequelize.query(`ALTER TABLE IF EXISTS "${tableName}" DISABLE TRIGGER ALL`); await this.app.db.sequelize.query(`ALTER TABLE IF EXISTS ${tableName} DISABLE TRIGGER ALL`);
} }
await this.importCollection({ await this.importCollection({
@ -144,7 +144,7 @@ export class Restorer extends AppMigrator {
}); });
} finally { } finally {
if (this.app.db.inDialect('postgres')) { if (this.app.db.inDialect('postgres')) {
await this.app.db.sequelize.query(`ALTER TABLE IF EXISTS "${tableName}" ENABLE TRIGGER ALL`); await this.app.db.sequelize.query(`ALTER TABLE IF EXISTS ${tableName} ENABLE TRIGGER ALL`);
} }
} }
}; };
@ -199,6 +199,25 @@ export class Restorer extends AppMigrator {
await decompress(backupFilePath, this.workDir); await decompress(backupFilePath, this.workDir);
} }
quoteTable(tableName) {
// @ts-ignore
tableName = this.app.db.sequelize.getQueryInterface().queryGenerator.quoteTable(this.addSchema(tableName));
return tableName;
}
addSchema(tableName) {
if (this.app.db.options.schema) {
// @ts-ignore
tableName = this.app.db.sequelize.getQueryInterface().queryGenerator.addSchema({
tableName,
_schema: this.app.db.options.schema,
});
}
return tableName;
}
async importCollection(options: { async importCollection(options: {
name: string; name: string;
insert?: boolean; insert?: boolean;
@ -216,15 +235,16 @@ export class Restorer extends AppMigrator {
const meta = JSON.parse(metaContent); const meta = JSON.parse(metaContent);
app.log.info(`collection meta ${metaContent}`); app.log.info(`collection meta ${metaContent}`);
const tableName = meta.tableName; const addSchemaTableName = this.addSchema(meta.tableName);
const tableName = this.quoteTable(meta.tableName);
if (options.clear !== false) { if (options.clear !== false) {
// truncate old data // truncate old data
let sql = `TRUNCATE TABLE "${tableName}"`; let sql = `TRUNCATE TABLE ${tableName}`;
if (app.db.inDialect('sqlite')) { if (app.db.inDialect('sqlite')) {
sql = `DELETE sql = `DELETE
FROM "${tableName}"`; FROM ${tableName}`;
} }
await app.db.sequelize.query(sqlAdapter(app.db, sql)); await app.db.sequelize.query(sqlAdapter(app.db, sql));
@ -282,7 +302,7 @@ export class Restorer extends AppMigrator {
//@ts-ignore //@ts-ignore
const sql = collection.model.queryInterface.queryGenerator.bulkInsertQuery( const sql = collection.model.queryInterface.queryGenerator.bulkInsertQuery(
tableName, addSchemaTableName,
rowsWithMeta, rowsWithMeta,
{}, {},
fieldMappedAttributes, fieldMappedAttributes,
@ -303,18 +323,20 @@ export class Restorer extends AppMigrator {
if (this.app.db.inDialect('postgres')) { if (this.app.db.inDialect('postgres')) {
const sequenceNameResult = await app.db.sequelize.query( const sequenceNameResult = await app.db.sequelize.query(
`SELECT column_default FROM information_schema.columns WHERE `SELECT column_default FROM information_schema.columns WHERE
table_name='${collection.model.tableName}' and "column_name" = 'id';`, table_name='${collection.model.tableName}' and "column_name" = 'id' and table_schema = '${
app.db.options.schema || 'public'
}';`,
); );
if (sequenceNameResult[0].length) { if (sequenceNameResult[0].length) {
const columnDefault = sequenceNameResult[0][0]['column_default']; const columnDefault = sequenceNameResult[0][0]['column_default'];
if (columnDefault.includes(`${collection.model.tableName}_id_seq`)) { if (columnDefault.includes(`${collection.model.tableName}_id_seq`)) {
const regex = new RegExp(/nextval\('("?\w+"?)\'.*\)/); const regex = new RegExp(/nextval\('(.*)'::regclass\)/);
const match = regex.exec(columnDefault); const match = regex.exec(columnDefault);
const sequenceName = match[1]; const sequenceName = match[1];
const maxVal = await app.db.sequelize.query( const maxVal = await app.db.sequelize.query(
`SELECT MAX("${primaryKeyAttribute.field}") FROM "${collection.model.tableName}"`, `SELECT MAX("${primaryKeyAttribute.field}") FROM ${tableName}`,
{ {
type: 'SELECT', type: 'SELECT',
}, },

View File

@ -103,7 +103,7 @@ export class UiSchemaRepository extends Repository {
tableNameAdapter(tableName) { tableNameAdapter(tableName) {
if (this.database.sequelize.getDialect() === 'postgres') { if (this.database.sequelize.getDialect() === 'postgres') {
return `"${tableName}"`; return `"${this.database.options.schema || 'public'}"."${tableName}"`;
} }
return tableName; return tableName;
} }