fix: get pg view def (#1641)

* fix: get pg view def

* chore: console.log
This commit is contained in:
ChengLei Shao 2023-04-02 22:50:55 +08:00 committed by GitHub
parent bec624eb54
commit 86abc251d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 8 deletions

View File

@ -56,9 +56,7 @@ export default class PostgresQueryInterface extends QueryInterface {
const viewDefQuery = await this.db.sequelize.query( const viewDefQuery = await this.db.sequelize.query(
` `
SELECT viewname, definition select pg_get_viewdef(format('%I.%I', '${schema}', '${viewName}')::regclass, true) as definition
FROM pg_views
WHERE schemaname = '${schema}' AND viewname = '${viewName}';
`, `,
{ type: 'SELECT' }, { type: 'SELECT' },
); );
@ -71,10 +69,19 @@ WHERE schemaname = '${schema}' AND viewname = '${viewName}';
const usages = columns const usages = columns
.map((column) => { .map((column) => {
const fieldAlias = column.as || column.expr.column; const fieldAlias = column.as || column.expr.column;
const columnUsage = columnUsages.find( const columnUsage = columnUsages.find((columnUsage) => {
(columnUsage) => let columnExprTable = column.expr.table;
columnUsage.column_name === column.expr.column && columnUsage.table_name === column.expr.table,
); // handle column alias
const from = ast[0].from;
const findAs = from.find((from) => from.as === columnExprTable);
if (findAs) {
columnExprTable = findAs.table;
}
return columnUsage.column_name === column.expr.column && columnUsage.table_name === columnExprTable;
});
return [ return [
fieldAlias, fieldAlias,

View File

@ -1,4 +1,4 @@
import Database, { Collection as DBCollection } from '@nocobase/database'; import Database, { Collection as DBCollection, HasManyRepository } from '@nocobase/database';
import Application from '@nocobase/server'; import Application from '@nocobase/server';
import { createApp } from '.'; import { createApp } from '.';
import CollectionManagerPlugin, { CollectionRepository } from '@nocobase/plugin-collection-manager'; import CollectionManagerPlugin, { CollectionRepository } from '@nocobase/plugin-collection-manager';
@ -605,4 +605,78 @@ describe('collections repository', () => {
expect(err).toBeFalsy(); expect(err).toBeFalsy();
}); });
it('should update association field', async () => {
const A = await Collection.repository.create({
values: {
name: 'a',
fields: [
{
name: 'title',
type: 'string',
},
{
name: 'bs',
type: 'hasMany',
uiSchema: {
title: 'bs-title',
},
},
],
},
context: {},
});
const B = await Collection.repository.create({
values: {
name: 'b',
fields: [
{
type: 'string',
name: 'title',
},
{
type: 'belongsTo',
name: 'a',
},
],
uiSchema: {
title: 'b-title',
},
},
context: {},
});
const C = await Collection.repository.create({
values: {
name: 'c',
fields: [
{
type: 'string',
name: 'title',
},
{
type: 'belongsTo',
name: 'a',
},
],
uiSchema: {
title: 'c-title',
},
},
context: {},
});
await db.sync();
await db.getRepository<HasManyRepository>('collections.fields', 'c').update({
filterByTk: 'a',
values: {
key: C.key,
uiSchema: {
title: 'c-hello-world',
},
},
});
});
}); });