Fix/multiple schema query (#1488)

* fix: describeTableQuery in multiple schema database

* fix: beforeUpdateCollection position
This commit is contained in:
ChengLei Shao 2023-02-23 14:13:32 +08:00 committed by GitHub
parent e0736f8aed
commit e18bccd00a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 28 deletions

View File

@ -351,10 +351,10 @@ export class Collection<
updateOptions(options: CollectionOptions, mergeOptions?: any) {
let newOptions = lodash.cloneDeep(options);
newOptions = merge(this.options, newOptions, mergeOptions);
this.options = newOptions;
this.context.database.emit('beforeUpdateCollection', this, newOptions);
this.options = newOptions;
this.setFields(options.fields, false);
this.setRepository(options.repository);

View File

@ -83,11 +83,7 @@ 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;
function patchShowConstraintsQuery(queryGenerator, db) {
queryGenerator.showConstraintsQuery = (tableName, constraintName) => {
const lines = [
'SELECT constraint_catalog AS "constraintCatalog",',
@ -114,6 +110,45 @@ export function patchSequelizeQueryInterface(db: Database) {
return lines.join(' ');
};
}
function patchDescribeTableQuery(queryGenerator) {
const describeTableQuery = function (tableName, schema) {
schema = schema || this.options.schema || 'public';
return (
'SELECT ' +
'pk.constraint_type as "Constraint",' +
'c.column_name as "Field", ' +
'c.column_default as "Default",' +
'c.is_nullable as "Null", ' +
"(CASE WHEN c.udt_name = 'hstore' THEN c.udt_name ELSE c.data_type END) || (CASE WHEN c.character_maximum_length IS NOT NULL THEN '(' || c.character_maximum_length || ')' ELSE '' END) as \"Type\", " +
'(SELECT array_agg(e.enumlabel) FROM pg_catalog.pg_type t JOIN pg_catalog.pg_enum e ON t.oid=e.enumtypid WHERE t.typname=c.udt_name) AS "special", ' +
'(SELECT pgd.description FROM pg_catalog.pg_statio_all_tables AS st INNER JOIN pg_catalog.pg_description pgd on (pgd.objoid=st.relid) WHERE c.ordinal_position=pgd.objsubid AND c.table_name=st.relname AND st.schemaname = c.table_schema) AS "Comment" ' +
'FROM information_schema.columns c ' +
'LEFT JOIN (SELECT tc.table_schema, tc.table_name, ' +
'cu.column_name, tc.constraint_type ' +
'FROM information_schema.TABLE_CONSTRAINTS tc ' +
'JOIN information_schema.KEY_COLUMN_USAGE cu ' +
'ON tc.table_schema=cu.table_schema and tc.table_name=cu.table_name ' +
'and tc.constraint_name=cu.constraint_name ' +
"and tc.constraint_type='PRIMARY KEY') pk " +
'ON pk.table_schema=c.table_schema ' +
'AND pk.table_name=c.table_name ' +
'AND pk.column_name=c.column_name ' +
`WHERE c.table_name = ${this.escape(tableName)} AND c.table_schema = ${this.escape(schema)}`
);
};
queryGenerator.describeTableQuery = describeTableQuery.bind(queryGenerator);
}
export function patchSequelizeQueryInterface(db: Database) {
if (db.inDialect('postgres')) {
//@ts-ignore
const queryGenerator = db.sequelize.dialect.queryGenerator;
patchShowConstraintsQuery(queryGenerator, db);
patchDescribeTableQuery(queryGenerator);
}
}
export function percent2float(value: string) {