From 1589d11e82cc0258522747d86d914b9386baea4a Mon Sep 17 00:00:00 2001 From: Chareice Date: Tue, 14 Feb 2023 20:04:49 +0800 Subject: [PATCH] feat: collection add schema table name method --- packages/core/database/src/collection.ts | 12 +++++++++++- packages/core/database/src/database.ts | 8 ++++++-- .../plugins/collection-manager/src/models/field.ts | 10 +++++----- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/core/database/src/collection.ts b/packages/core/database/src/collection.ts index 3c43342bf..8908def2d 100644 --- a/packages/core/database/src/collection.ts +++ b/packages/core/database/src/collection.ts @@ -7,7 +7,7 @@ import { QueryInterfaceDropTableOptions, SyncOptions, Transactionable, - Utils + Utils, } from 'sequelize'; import { Database } from './database'; import { Field, FieldOptions } from './fields'; @@ -532,4 +532,14 @@ export class Collection< public isParent() { return this.context.database.inheritanceMap.isParentNode(this.name); } + + public addSchemaTableName() { + const tableName = this.model.tableName; + + if (this.options.schema) { + return this.db.utils.addSchema(tableName, this.options.schema); + } + + return tableName; + } } diff --git a/packages/core/database/src/database.ts b/packages/core/database/src/database.ts index 9224bfed8..3fb68af5c 100644 --- a/packages/core/database/src/database.ts +++ b/packages/core/database/src/database.ts @@ -15,7 +15,7 @@ import { Sequelize, SyncOptions, Transactionable, - Utils + Utils, } from 'sequelize'; import { SequelizeStorage, Umzug } from 'umzug'; import { Collection, CollectionOptions, RepositoryType } from './collection'; @@ -58,7 +58,7 @@ import { SyncListener, UpdateListener, UpdateWithAssociationsListener, - ValidateListener + ValidateListener, } from './types'; import { snakeCase } from './utils'; @@ -324,6 +324,10 @@ export class Database extends EventEmitter implements AsyncEmitter { }); } } + + if (this.options.schema && !options.schema) { + options.schema = this.options.schema; + } }); } diff --git a/packages/plugins/collection-manager/src/models/field.ts b/packages/plugins/collection-manager/src/models/field.ts index 239a989b2..62cc73b82 100644 --- a/packages/plugins/collection-manager/src/models/field.ts +++ b/packages/plugins/collection-manager/src/models/field.ts @@ -107,7 +107,7 @@ export class FieldModel extends MagicAttributeModel { const queryInterface = this.db.sequelize.getQueryInterface() as any; - const existsIndexes = await queryInterface.showIndex(collection.model.tableName, { + const existsIndexes = await queryInterface.showIndex(collection.addSchemaTableName(), { transaction: options.transaction, }); @@ -121,7 +121,7 @@ export class FieldModel extends MagicAttributeModel { if (existUniqueIndex) { const existsUniqueConstraints = await queryInterface.showConstraint( - collection.model.tableName, + collection.addSchemaTableName(), constraintName, {}, ); @@ -133,7 +133,7 @@ export class FieldModel extends MagicAttributeModel { // @ts-ignore await collection.sync({ ...options, force: false, alter: { drop: false } }); - await queryInterface.addConstraint(tableName, { + await queryInterface.addConstraint(collection.addSchemaTableName(), { type: 'unique', fields: [columnName], name: constraintName, @@ -142,7 +142,7 @@ export class FieldModel extends MagicAttributeModel { } if (!unique && existsUniqueConstraint) { - await queryInterface.removeConstraint(collection.model.tableName, constraintName, { + await queryInterface.removeConstraint(collection.addSchemaTableName(), constraintName, { transaction: options.transaction, }); } @@ -164,7 +164,7 @@ export class FieldModel extends MagicAttributeModel { const queryInterface = collection.db.sequelize.getQueryInterface(); await queryInterface.changeColumn( - collection.model.tableName, + collection.addSchemaTableName(), collection.model.rawAttributes[this.get('name')].field, { type: field.dataType,