mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 08:26:21 +00:00
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:
parent
5370e95f4b
commit
8dd8303f21
@ -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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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: {
|
||||
|
Loading…
Reference in New Issue
Block a user