fix: remove field when collection has difference schema with database (#1524)

This commit is contained in:
ChengLei Shao 2023-03-03 11:05:57 +08:00 committed by GitHub
parent 946c8f25a3
commit 1a84396691
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 5 deletions

View File

@ -542,8 +542,8 @@ export class Collection<
public addSchemaTableName() { public addSchemaTableName() {
const tableName = this.model.tableName; const tableName = this.model.tableName;
if (this.options.schema) { if (this.collectionSchema()) {
return this.db.utils.addSchema(tableName, this.options.schema); return this.db.utils.addSchema(tableName, this.collectionSchema());
} }
return tableName; return tableName;
@ -552,4 +552,16 @@ export class Collection<
public quotedTableName() { public quotedTableName() {
return this.db.utils.quoteTable(this.addSchemaTableName()); return this.db.utils.quoteTable(this.addSchemaTableName());
} }
public collectionSchema() {
if (this.options.schema) {
return this.options.schema;
}
if (this.db.options.schema) {
return this.db.options.schema;
}
return undefined;
}
} }

View File

@ -6,7 +6,7 @@ import {
ModelIndexesOptions, ModelIndexesOptions,
QueryInterfaceOptions, QueryInterfaceOptions,
SyncOptions, SyncOptions,
Transactionable Transactionable,
} from 'sequelize'; } from 'sequelize';
import { Collection } from '../collection'; import { Collection } from '../collection';
import { Database } from '../database'; import { Database } from '../database';
@ -169,7 +169,7 @@ export abstract class Field {
columnReferencesCount == 1 columnReferencesCount == 1
) { ) {
const queryInterface = this.database.sequelize.getQueryInterface(); const queryInterface = this.database.sequelize.getQueryInterface();
await queryInterface.removeColumn(this.collection.model.tableName, this.columnName(), options); await queryInterface.removeColumn(this.collection.addSchemaTableName(), this.columnName(), options);
} }
this.remove(); this.remove();
@ -194,7 +194,9 @@ export abstract class Field {
sql = ` sql = `
select column_name select column_name
from INFORMATION_SCHEMA.COLUMNS from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='${this.collection.model.tableName}' AND column_name='${this.columnName()}' where TABLE_NAME='${
this.collection.model.tableName
}' AND column_name='${this.columnName()}' AND table_schema='${this.collection.collectionSchema() || 'public'}'
`; `;
} }
const [rows] = await this.database.sequelize.query(sql, opts); const [rows] = await this.database.sequelize.query(sql, opts);

View File

@ -555,4 +555,40 @@ describe('collections repository', () => {
expect(await Field.repository.count()).toBe(2); expect(await Field.repository.count()).toBe(2);
}); });
it('should destroy field when collection set to difference schema', async () => {
if (db.sequelize.getDialect() !== 'postgres') {
return;
}
const collection = await Collection.repository.create({
values: {
name: 'test',
schema: 'test_schema',
},
context: {},
});
const field = await Field.repository.create({
values: {
name: 'test_field',
type: 'string',
collectionName: collection.get('name'),
},
context: {},
});
let err;
try {
await Field.repository.destroy({
filter: {
name: field.get('name'),
},
});
} catch (e) {
err = e;
}
expect(err).toBeFalsy();
});
}); });