Skip to content

Commit

Permalink
chore(action-import): skip reset sequence if no auto-increment primar…
Browse files Browse the repository at this point in the history
…y is imported (nocobase#4631)

* chore: auto incr primary key imported test

* chore: skip reset sequence if no auto increment primary imported
  • Loading branch information
chareice authored Jun 12, 2024
1 parent 93d3313 commit 690b7cd
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,94 @@ describe('xlsx importer', () => {
expect(user2Json['boolean']).toBe(false);
});

it('should reset id seq after import', async () => {
it('should not reset id seq if not import id field', async () => {
const User = app.db.collection({
name: 'users',
autoGenId: false,
fields: [
{
type: 'bigInt',
name: 'id',
primaryKey: true,
autoIncrement: true,
},
{
type: 'string',
name: 'name',
},
{
type: 'string',
name: 'email',
},
],
});

await app.db.sync();

const templateCreator = new TemplateCreator({
collection: User,
columns: [
{
dataIndex: ['name'],
defaultTitle: '姓名',
},
{
dataIndex: ['email'],
defaultTitle: '邮箱',
},
],
});

const template = await templateCreator.run();

const worksheet = template.Sheets[template.SheetNames[0]];

XLSX.utils.sheet_add_aoa(
worksheet,
[
['User1', '[email protected]'],
['User2', '[email protected]'],
],
{
origin: 'A2',
},
);

const importer = new XlsxImporter({
collectionManager: app.mainDataSource.collectionManager,
collection: User,
columns: [
{
dataIndex: ['name'],
defaultTitle: '姓名',
},
{
dataIndex: ['email'],
defaultTitle: '邮箱',
},
],
workbook: template,
});

const testFn = vi.fn();
importer.on('seqReset', testFn);

await importer.run();

expect(await User.repository.count()).toBe(2);

const user3 = await User.repository.create({
values: {
name: 'User3',
email: '[email protected]',
},
});

expect(user3.get('id')).toBe(3);
expect(testFn).not.toBeCalled();
});

it('should reset id seq after import id field', async () => {
const User = app.db.collection({
name: 'users',
autoGenId: false,
Expand Down Expand Up @@ -293,6 +380,9 @@ describe('xlsx importer', () => {
workbook: template,
});

const testFn = vi.fn();
importer.on('seqReset', testFn);

await importer.run();

expect(await User.repository.count()).toBe(2);
Expand All @@ -305,6 +395,8 @@ describe('xlsx importer', () => {
});

expect(user3.get('id')).toBe(3);

expect(testFn).toBeCalled();
});

it('should validate workbook with error', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import lodash from 'lodash';
import { ICollection, ICollectionManager, IRelationField } from '@nocobase/data-source-manager';
import { Collection as DBCollection, Database } from '@nocobase/database';
import { Transaction } from 'sequelize';
import EventEmitter from 'events';

export type ImportColumn = {
dataIndex: Array<string>;
Expand All @@ -31,8 +32,10 @@ type RunOptions = {
transaction?: Transaction;
};

export class XlsxImporter {
export class XlsxImporter extends EventEmitter {
constructor(private options: ImporterOptions) {
super();

if (options.columns.length == 0) {
throw new Error(`columns is empty`);
}
Expand Down Expand Up @@ -67,6 +70,7 @@ export class XlsxImporter {

async resetSeq(options?: RunOptions) {
const { transaction } = options;

// @ts-ignore
const db: Database = this.options.collectionManager.db;
const collection: DBCollection = this.options.collection as DBCollection;
Expand All @@ -77,6 +81,18 @@ export class XlsxImporter {
return;
}

let hasImportedAutoIncrementPrimary = false;
for (const importedDataIndex of this.options.columns) {
if (importedDataIndex.dataIndex[0] === autoIncrementAttribute) {
hasImportedAutoIncrementPrimary = true;
break;
}
}

if (!hasImportedAutoIncrementPrimary) {
return;
}

let tableInfo = collection.getTableNameWithSchema();
if (typeof tableInfo === 'string') {
tableInfo = {
Expand All @@ -93,13 +109,16 @@ export class XlsxImporter {
const maxVal = (await collection.model.max(autoIncrementAttribute, { transaction })) as number;

const queryInterface = db.queryInterface;

await queryInterface.setAutoIncrementVal({
tableInfo,
columnName: collection.model.rawAttributes[autoIncrementAttribute].field,
currentVal: maxVal,
seqName: autoIncrInfo.seqName,
transaction,
});

this.emit('seqReset', { maxVal, seqName: autoIncrInfo.seqName });
}

async performImport(options?: RunOptions) {
Expand Down Expand Up @@ -134,6 +153,7 @@ export class XlsxImporter {
const dataKey = column.dataIndex[0];

const fieldOptions = field.options;

const interfaceName = fieldOptions.interface;

const InterfaceClass = this.options.collectionManager.getFieldInterface(interfaceName);
Expand Down

0 comments on commit 690b7cd

Please sign in to comment.