fix(backup-restore): dump with collection that names were reserved words in mysql (#4734)

* fix(backup-restore): dump with collection that names were reserved words

* chore: test
This commit is contained in:
ChengLei Shao 2024-06-24 15:50:37 +08:00 committed by GitHub
parent 5370e95f4b
commit 8dd8303f21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 4 deletions

View File

@ -115,8 +115,9 @@ export default class MysqlQueryInterface extends QueryInterface {
if (currentVal === null) {
// use max value of field instead
const maxSql = `SELECT MAX(${fieldName}) as currentVal
FROM ${tableInfo.tableName};`;
const maxSql = `SELECT MAX(\`${fieldName}\`) as currentVal
FROM \`${tableInfo.tableName}\`;`;
const maxResults = await this.db.sequelize.query(maxSql, { type: 'SELECT', transaction });
currentVal = maxResults[0]['currentVal'] as number;
}
@ -135,7 +136,9 @@ export default class MysqlQueryInterface extends QueryInterface {
}): Promise<void> {
const { tableInfo, columnName, seqName, currentVal, transaction } = options;
const sql = `ALTER TABLE ${this.quoteIdentifier(tableInfo.tableName)} AUTO_INCREMENT = ${currentVal};`;
await this.db.sequelize.query(sql, { transaction });
if (currentVal) {
const sql = `ALTER TABLE ${this.quoteIdentifier(tableInfo.tableName)} AUTO_INCREMENT = ${currentVal};`;
await this.db.sequelize.query(sql, { transaction });
}
}
}

View File

@ -14,6 +14,7 @@ import path from 'path';
import { Dumper } from '../dumper';
import { Restorer } from '../restorer';
import createApp from './index';
import * as process from 'node:process';
describe('dumper', () => {
let app: MockServer;
@ -69,6 +70,52 @@ describe('dumper', () => {
});
});
it.runIf(process.env['DB_DIALECT'] === 'mysql')('should dump with table named by reserved word', async () => {
await db.getRepository('collections').create({
values: {
name: 'update',
tableName: 'update',
autoGenId: false,
fields: [
{
type: 'bigInt',
name: 'id',
},
{
type: 'string',
name: 'name',
},
],
},
context: {},
});
await db.getRepository('update').create({
values: {
name: 'test',
},
});
const dumper = new Dumper(app);
db.getCollection('update').model['rawAttributes']['id'].autoIncrement = true;
const result = await dumper.dump({
groups: new Set(['required', 'custom']),
});
const restorer = new Restorer(app, {
backUpFilePath: result.filePath,
});
await restorer.restore({
groups: new Set(['required', 'custom']),
});
const testCollection = app.db.getCollection('update');
const items = await testCollection.repository.find();
expect(items.length).toBe(1);
});
it('should dump and restore date field', async () => {
await db.getRepository('collections').create({
values: {