From 130fe227042e5eb0a1b886bd579127a6b7304bab Mon Sep 17 00:00:00 2001 From: Chareice Date: Tue, 14 Feb 2023 21:34:23 +0800 Subject: [PATCH] fix: schema test --- packages/core/database/src/database.ts | 6 ++-- packages/core/database/src/mock-database.ts | 2 +- packages/core/database/src/utils.ts | 31 +++++++++++++++++++ packages/core/test/src/mockServer.ts | 2 +- .../__tests__/http-api/collections.test.ts | 4 +-- .../collection-manager/src/models/field.ts | 6 +--- .../src/__tests__/action.test.ts | 3 +- .../__tests__/ui-schema-repository.test.ts | 3 +- .../src/__tests__/ui-schema.test.ts | 2 +- 9 files changed, 43 insertions(+), 16 deletions(-) diff --git a/packages/core/database/src/database.ts b/packages/core/database/src/database.ts index 3fb68af5c..0ba129e3d 100644 --- a/packages/core/database/src/database.ts +++ b/packages/core/database/src/database.ts @@ -60,7 +60,7 @@ import { UpdateWithAssociationsListener, ValidateListener, } from './types'; -import { snakeCase } from './utils'; +import { patchSequelizeQueryInterface, snakeCase } from './utils'; import DatabaseUtils from './database-utils'; @@ -200,9 +200,10 @@ export class Database extends EventEmitter implements AsyncEmitter { // https://github.com/sequelize/sequelize/issues/1774 require('pg').defaults.parseInt8 = true; } + this.options = opts; this.sequelize = new Sequelize(opts); - this.options = opts; + this.collections = new Map(); this.modelHook = new ModelHook(this); @@ -266,6 +267,7 @@ export class Database extends EventEmitter implements AsyncEmitter { }); this.initListener(); + patchSequelizeQueryInterface(this); } initListener() { diff --git a/packages/core/database/src/mock-database.ts b/packages/core/database/src/mock-database.ts index fb4692b94..8ce980526 100644 --- a/packages/core/database/src/mock-database.ts +++ b/packages/core/database/src/mock-database.ts @@ -32,7 +32,7 @@ export function getConfigByEnv() { }, timezone: process.env.DB_TIMEZONE, underscored: process.env.DB_UNDERSCORED === 'true', - schema: process.env.DB_SCHEMA, + schema: process.env.DB_SCHEMA !== 'public' ? process.env.DB_SCHEMA : undefined, }; } diff --git a/packages/core/database/src/utils.ts b/packages/core/database/src/utils.ts index 173fc17b8..6fbbcc8c7 100644 --- a/packages/core/database/src/utils.ts +++ b/packages/core/database/src/utils.ts @@ -2,6 +2,7 @@ import crypto from 'crypto'; import { IdentifierError } from './errors/identifier-error'; import { Model } from './model'; import lodash from 'lodash'; +import Database from './database'; type HandleAppendsQueryOptions = { templateModel: any; @@ -82,3 +83,33 @@ export function getTableName(collectionName: string, options) { export function snakeCase(name: string) { return require('sequelize').Utils.underscore(name); } + +export function patchSequelizeQueryInterface(db: Database) { + if (db.inDialect('postgres')) { + //@ts-ignore + const queryGenerator = db.sequelize.dialect.queryGenerator; + + queryGenerator.showConstraintsQuery = (tableName, constraintName) => { + const lines = [ + 'SELECT constraint_catalog AS "constraintCatalog",', + 'constraint_schema AS "constraintSchema",', + 'constraint_name AS "constraintName",', + 'table_catalog AS "tableCatalog",', + 'table_schema AS "tableSchema",', + 'table_name AS "tableName",', + 'constraint_type AS "constraintType",', + 'is_deferrable AS "isDeferrable",', + 'initially_deferred AS "initiallyDeferred"', + 'from INFORMATION_SCHEMA.table_constraints', + `WHERE table_name='${tableName}'`, + `AND constraint_name='${constraintName}'`, + ]; + + if (db.options.schema && db.options.schema !== 'public') { + lines.push(`AND table_schema='${db.options.schema}'`); + } + + return lines.join(' '); + }; + } +} diff --git a/packages/core/test/src/mockServer.ts b/packages/core/test/src/mockServer.ts index 5341ead20..0272a287f 100644 --- a/packages/core/test/src/mockServer.ts +++ b/packages/core/test/src/mockServer.ts @@ -70,7 +70,7 @@ export class MockServer extends Application { } async cleanDb() { - await this.db.sequelize.getQueryInterface().dropAllTables(); + await this.db.clean({ drop: true }); } agent(): SuperAgentTest & { resource: (name: string, resourceOf?: any) => Resource } { diff --git a/packages/plugins/collection-manager/src/__tests__/http-api/collections.test.ts b/packages/plugins/collection-manager/src/__tests__/http-api/collections.test.ts index e7da82ed0..d3d3f61fa 100644 --- a/packages/plugins/collection-manager/src/__tests__/http-api/collections.test.ts +++ b/packages/plugins/collection-manager/src/__tests__/http-api/collections.test.ts @@ -504,7 +504,7 @@ describe('collections repository', () => { const indexes = (await app.db.sequelize .getQueryInterface() - .showIndex(app.db.getCollection('test').model.tableName)) as any; + .showIndex(app.db.getCollection('test').addSchemaTableName())) as any; const columnName = app.db.getCollection('test').model.rawAttributes.testField.field; @@ -528,7 +528,7 @@ describe('collections repository', () => { const afterIndexes = (await app.db.sequelize .getQueryInterface() - .showIndex(app.db.getCollection('test').model.tableName)) as any; + .showIndex(app.db.getCollection('test').addSchemaTableName())) as any; expect( afterIndexes.find( diff --git a/packages/plugins/collection-manager/src/models/field.ts b/packages/plugins/collection-manager/src/models/field.ts index 62cc73b82..783ae522f 100644 --- a/packages/plugins/collection-manager/src/models/field.ts +++ b/packages/plugins/collection-manager/src/models/field.ts @@ -120,11 +120,7 @@ export class FieldModel extends MagicAttributeModel { let constraintName = `${tableName}_${field.name}_uk`; if (existUniqueIndex) { - const existsUniqueConstraints = await queryInterface.showConstraint( - collection.addSchemaTableName(), - constraintName, - {}, - ); + const existsUniqueConstraints = await queryInterface.showConstraint(tableName, constraintName, {}); existsUniqueConstraint = existsUniqueConstraints[0]; } diff --git a/packages/plugins/ui-schema-storage/src/__tests__/action.test.ts b/packages/plugins/ui-schema-storage/src/__tests__/action.test.ts index 0cdf4a9e2..40138c05f 100644 --- a/packages/plugins/ui-schema-storage/src/__tests__/action.test.ts +++ b/packages/plugins/ui-schema-storage/src/__tests__/action.test.ts @@ -13,8 +13,7 @@ describe('action test', () => { db = app.db; - const queryInterface = db.sequelize.getQueryInterface(); - await queryInterface.dropAllTables(); + await db.clean({ drop: true }); app.plugin(PluginUiSchema, { name: 'ui-schema-storage' }); diff --git a/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-repository.test.ts b/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-repository.test.ts index 723d268a6..7004fea86 100644 --- a/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-repository.test.ts +++ b/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-repository.test.ts @@ -22,8 +22,7 @@ describe('ui_schema repository', () => { db = app.db; - const queryInterface = db.sequelize.getQueryInterface(); - await queryInterface.dropAllTables(); + await db.clean({ drop: true }); app.plugin(PluginUiSchema, { name: 'ui-schema-storage' }); diff --git a/packages/plugins/ui-schema-storage/src/__tests__/ui-schema.test.ts b/packages/plugins/ui-schema-storage/src/__tests__/ui-schema.test.ts index 0846ef451..fda699246 100644 --- a/packages/plugins/ui-schema-storage/src/__tests__/ui-schema.test.ts +++ b/packages/plugins/ui-schema-storage/src/__tests__/ui-schema.test.ts @@ -20,7 +20,7 @@ describe('ui-schema', () => { db = app.db; - await db.sequelize.getQueryInterface().dropAllTables(); + await db.clean({ drop: true }); app.plugin(PluginUiSchema, { name: 'ui-schema-storage' });