fix: schema test
This commit is contained in:
		
							parent
							
								
									ee3db22c8a
								
							
						
					
					
						commit
						130fe22704
					
				@ -60,7 +60,7 @@ import {
 | 
				
			|||||||
  UpdateWithAssociationsListener,
 | 
					  UpdateWithAssociationsListener,
 | 
				
			||||||
  ValidateListener,
 | 
					  ValidateListener,
 | 
				
			||||||
} from './types';
 | 
					} from './types';
 | 
				
			||||||
import { snakeCase } from './utils';
 | 
					import { patchSequelizeQueryInterface, snakeCase } from './utils';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import DatabaseUtils from './database-utils';
 | 
					import DatabaseUtils from './database-utils';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -200,9 +200,10 @@ export class Database extends EventEmitter implements AsyncEmitter {
 | 
				
			|||||||
      // https://github.com/sequelize/sequelize/issues/1774
 | 
					      // https://github.com/sequelize/sequelize/issues/1774
 | 
				
			||||||
      require('pg').defaults.parseInt8 = true;
 | 
					      require('pg').defaults.parseInt8 = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    this.options = opts;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this.sequelize = new Sequelize(opts);
 | 
					    this.sequelize = new Sequelize(opts);
 | 
				
			||||||
    this.options = opts;
 | 
					
 | 
				
			||||||
    this.collections = new Map();
 | 
					    this.collections = new Map();
 | 
				
			||||||
    this.modelHook = new ModelHook(this);
 | 
					    this.modelHook = new ModelHook(this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -266,6 +267,7 @@ export class Database extends EventEmitter implements AsyncEmitter {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this.initListener();
 | 
					    this.initListener();
 | 
				
			||||||
 | 
					    patchSequelizeQueryInterface(this);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  initListener() {
 | 
					  initListener() {
 | 
				
			||||||
 | 
				
			|||||||
@ -32,7 +32,7 @@ export function getConfigByEnv() {
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
    timezone: process.env.DB_TIMEZONE,
 | 
					    timezone: process.env.DB_TIMEZONE,
 | 
				
			||||||
    underscored: process.env.DB_UNDERSCORED === 'true',
 | 
					    underscored: process.env.DB_UNDERSCORED === 'true',
 | 
				
			||||||
    schema: process.env.DB_SCHEMA,
 | 
					    schema: process.env.DB_SCHEMA !== 'public' ? process.env.DB_SCHEMA : undefined,
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,7 @@ import crypto from 'crypto';
 | 
				
			|||||||
import { IdentifierError } from './errors/identifier-error';
 | 
					import { IdentifierError } from './errors/identifier-error';
 | 
				
			||||||
import { Model } from './model';
 | 
					import { Model } from './model';
 | 
				
			||||||
import lodash from 'lodash';
 | 
					import lodash from 'lodash';
 | 
				
			||||||
 | 
					import Database from './database';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type HandleAppendsQueryOptions = {
 | 
					type HandleAppendsQueryOptions = {
 | 
				
			||||||
  templateModel: any;
 | 
					  templateModel: any;
 | 
				
			||||||
@ -82,3 +83,33 @@ export function getTableName(collectionName: string, options) {
 | 
				
			|||||||
export function snakeCase(name: string) {
 | 
					export function snakeCase(name: string) {
 | 
				
			||||||
  return require('sequelize').Utils.underscore(name);
 | 
					  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(' ');
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -70,7 +70,7 @@ export class MockServer extends Application {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async cleanDb() {
 | 
					  async cleanDb() {
 | 
				
			||||||
    await this.db.sequelize.getQueryInterface().dropAllTables();
 | 
					    await this.db.clean({ drop: true });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  agent(): SuperAgentTest & { resource: (name: string, resourceOf?: any) => Resource } {
 | 
					  agent(): SuperAgentTest & { resource: (name: string, resourceOf?: any) => Resource } {
 | 
				
			||||||
 | 
				
			|||||||
@ -504,7 +504,7 @@ describe('collections repository', () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const indexes = (await app.db.sequelize
 | 
					    const indexes = (await app.db.sequelize
 | 
				
			||||||
      .getQueryInterface()
 | 
					      .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;
 | 
					    const columnName = app.db.getCollection('test').model.rawAttributes.testField.field;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -528,7 +528,7 @@ describe('collections repository', () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const afterIndexes = (await app.db.sequelize
 | 
					    const afterIndexes = (await app.db.sequelize
 | 
				
			||||||
      .getQueryInterface()
 | 
					      .getQueryInterface()
 | 
				
			||||||
      .showIndex(app.db.getCollection('test').model.tableName)) as any;
 | 
					      .showIndex(app.db.getCollection('test').addSchemaTableName())) as any;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    expect(
 | 
					    expect(
 | 
				
			||||||
      afterIndexes.find(
 | 
					      afterIndexes.find(
 | 
				
			||||||
 | 
				
			|||||||
@ -120,11 +120,7 @@ export class FieldModel extends MagicAttributeModel {
 | 
				
			|||||||
    let constraintName = `${tableName}_${field.name}_uk`;
 | 
					    let constraintName = `${tableName}_${field.name}_uk`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (existUniqueIndex) {
 | 
					    if (existUniqueIndex) {
 | 
				
			||||||
      const existsUniqueConstraints = await queryInterface.showConstraint(
 | 
					      const existsUniqueConstraints = await queryInterface.showConstraint(tableName, constraintName, {});
 | 
				
			||||||
        collection.addSchemaTableName(),
 | 
					 | 
				
			||||||
        constraintName,
 | 
					 | 
				
			||||||
        {},
 | 
					 | 
				
			||||||
      );
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      existsUniqueConstraint = existsUniqueConstraints[0];
 | 
					      existsUniqueConstraint = existsUniqueConstraints[0];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -13,8 +13,7 @@ describe('action test', () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    db = app.db;
 | 
					    db = app.db;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const queryInterface = db.sequelize.getQueryInterface();
 | 
					    await db.clean({ drop: true });
 | 
				
			||||||
    await queryInterface.dropAllTables();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    app.plugin(PluginUiSchema, { name: 'ui-schema-storage' });
 | 
					    app.plugin(PluginUiSchema, { name: 'ui-schema-storage' });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -22,8 +22,7 @@ describe('ui_schema repository', () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    db = app.db;
 | 
					    db = app.db;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const queryInterface = db.sequelize.getQueryInterface();
 | 
					    await db.clean({ drop: true });
 | 
				
			||||||
    await queryInterface.dropAllTables();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    app.plugin(PluginUiSchema, { name: 'ui-schema-storage' });
 | 
					    app.plugin(PluginUiSchema, { name: 'ui-schema-storage' });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -20,7 +20,7 @@ describe('ui-schema', () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    db = app.db;
 | 
					    db = app.db;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await db.sequelize.getQueryInterface().dropAllTables();
 | 
					    await db.clean({ drop: true });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    app.plugin(PluginUiSchema, { name: 'ui-schema-storage' });
 | 
					    app.plugin(PluginUiSchema, { name: 'ui-schema-storage' });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user