From 1a843966917cb8c0c983e69f02d4c32ea82cc227 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Fri, 3 Mar 2023 11:05:57 +0800 Subject: [PATCH] fix: remove field when collection has difference schema with database (#1524) --- packages/core/database/src/collection.ts | 16 +++++++-- packages/core/database/src/fields/field.ts | 8 +++-- .../__tests__/collections.repository.test.ts | 36 +++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/packages/core/database/src/collection.ts b/packages/core/database/src/collection.ts index f37cdda23..98c1a68d0 100644 --- a/packages/core/database/src/collection.ts +++ b/packages/core/database/src/collection.ts @@ -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; + } } diff --git a/packages/core/database/src/fields/field.ts b/packages/core/database/src/fields/field.ts index 9f1b3eba0..1dcf120b8 100644 --- a/packages/core/database/src/fields/field.ts +++ b/packages/core/database/src/fields/field.ts @@ -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); diff --git a/packages/plugins/collection-manager/src/__tests__/collections.repository.test.ts b/packages/plugins/collection-manager/src/__tests__/collections.repository.test.ts index 4bdf46b33..bbfff11b7 100644 --- a/packages/plugins/collection-manager/src/__tests__/collections.repository.test.ts +++ b/packages/plugins/collection-manager/src/__tests__/collections.repository.test.ts @@ -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(); + }); });