fix: schema test

This commit is contained in:
Chareice 2023-02-14 21:34:23 +08:00
parent ee3db22c8a
commit 130fe22704
9 changed files with 43 additions and 16 deletions

View File

@ -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() {

View File

@ -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,
};
}

View File

@ -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(' ');
};
}
}

View File

@ -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 } {

View File

@ -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(

View File

@ -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];
}

View File

@ -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' });

View File

@ -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' });

View File

@ -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' });