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() {
const tableName = this.model.tableName;
if (this.options.schema) {
return this.db.utils.addSchema(tableName, this.options.schema);
if (this.collectionSchema()) {
return this.db.utils.addSchema(tableName, this.collectionSchema());
}
return tableName;
@ -552,4 +552,16 @@ export class Collection<
public quotedTableName() {
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,
QueryInterfaceOptions,
SyncOptions,
Transactionable
Transactionable,
} from 'sequelize';
import { Collection } from '../collection';
import { Database } from '../database';
@ -169,7 +169,7 @@ export abstract class Field {
columnReferencesCount == 1
) {
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();
@ -194,7 +194,9 @@ export abstract class Field {
sql = `
select column_name
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);

View File

@ -555,4 +555,40 @@ describe('collections repository', () => {
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();
});
});