console: allow editing comments on views (fix #8846)

GITHUB_PR_NUMBER: 8847
GITHUB_PR_URL: https://github.com/hasura/graphql-engine/pull/8847

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5625
Co-authored-by: Gordan Nekić <48327736+gnekich@users.noreply.github.com>
GitOrigin-RevId: e54b3024533dab51231e2a9ac578c115d4a21dcb
This commit is contained in:
hasura-bot 2022-09-06 12:54:35 +01:00
parent ff25d27ab1
commit 432c481c9c
10 changed files with 76 additions and 13 deletions

View File

@ -1430,7 +1430,7 @@ const deleteConstraintSql = (tableName, cName) => {
};
};
const saveTableCommentSql = () => {
const saveTableCommentSql = tableType => {
return (dispatch, getState) => {
const source = getState().tables.currentDataSource;
const updatedComment =
@ -1439,16 +1439,34 @@ const saveTableCommentSql = () => {
const currentSchema = getState().tables.currentSchema;
const tableName = getState().tables.currentTable;
const commentQueryUp = dataSource.getAlterTableCommentSql({
tableName,
schemaName: currentSchema,
comment: updatedComment ?? null,
});
const commentDownQuery = dataSource.getAlterTableCommentSql({
tableName,
schemaName: currentSchema,
comment: null,
});
// For now the saveTableCommentSql is also used for views, we need to make a check
// tableType should be returned by findViewType in ModifyView.js
const property = `${tableType}`.toLowerCase();
const commentQueryUp =
property === 'view'
? dataSource.getAlterViewCommentSql({
viewName: tableName,
schemaName: currentSchema,
comment: updatedComment ?? null,
})
: dataSource.getAlterTableCommentSql({
tableName,
schemaName: currentSchema,
comment: updatedComment ?? null,
});
const commentDownQuery =
property === 'view'
? dataSource.getAlterViewCommentSql({
viewName: tableName,
schemaName: currentSchema,
comment: null,
})
: dataSource.getAlterTableCommentSql({
tableName,
schemaName: currentSchema,
comment: null,
});
const migration = new Migration();
migration.add(
@ -1457,8 +1475,7 @@ const saveTableCommentSql = () => {
);
// Apply migrations
const migrationName =
'alter_table_' + currentSchema + '_' + tableName + '_update_comment';
const migrationName = `alter_${property}_${currentSchema}_${tableName}_update_comment`;
const requestMsg = 'Updating Comment...';
const successMsg = 'Comment Updated';

View File

@ -288,6 +288,15 @@ export interface DataSourcesAPI {
columnName: string;
columnType?: string;
}) => string;
getAlterViewCommentSql: ({
viewName,
schemaName,
comment,
}: {
viewName: string;
schemaName: string;
comment: string;
}) => string;
getAlterFunctionCommentSql: ({
functionName,
schemaName,

View File

@ -375,6 +375,7 @@ export const bigquery: DataSourcesAPI = {
},
getAlterTableCommentSql: () => '',
getAlterColumnCommentSql: () => '',
getAlterViewCommentSql: () => '',
getAlterFunctionCommentSql: () => '',
getSetColumnDefaultSql: () => {
return '';

View File

@ -1009,6 +1009,9 @@ WHERE
return `${dropCommonCommentStatement},@level1type = N'TABLE', @level1name = '${tableName}',@level2type = N'COLUMN', @level2name = '${columnName}';
${commonCommentStatement},@level1type = N'TABLE', @level1name = '${tableName}',@level2type = N'COLUMN', @level2name = '${columnName}'`;
},
getAlterViewCommentSql: () => {
return '';
},
getAlterFunctionCommentSql: () => {
return '';
},

View File

@ -35,6 +35,7 @@ import {
getAdditionalColumnsInfoQuerySql,
getAlterTableCommentSql,
getAlterColumnCommentSql,
getAlterViewCommentSql,
getAlterFunctionCommentSql,
} from './sqlUtils';
import { getTableSupportedQueries } from '../postgresql';
@ -254,5 +255,6 @@ WHERE
violationActions,
getAlterTableCommentSql,
getAlterColumnCommentSql,
getAlterViewCommentSql,
getAlterFunctionCommentSql,
};

View File

@ -199,6 +199,11 @@ export const getAlterColumnCommentSql: DataSourcesAPI['getAlterColumnCommentSql'
)} modify column \`${columnName}\` ${columnType} comment ${commentStr};`;
};
export const getAlterViewCommentSql: DataSourcesAPI['getAlterViewCommentSql'] =
() => {
return '';
};
export const getAlterFunctionCommentSql: DataSourcesAPI['getAlterFunctionCommentSql'] =
() => {
return '';

View File

@ -32,6 +32,8 @@ COMMIT TRANSACTION;"
exports[`postgresql datasource tests getAlterTableCommentSql should generate SQL for modifying table comment 1`] = `"comment on table \\"public\\".\\"users\\" is E'user\\\\'s comment'"`;
exports[`postgresql datasource tests getAlterViewCommentSql should generate SQL for modifying view comment 1`] = `"comment on view \\"public\\".\\"view_users\\" is E'user\\\\'s comment'"`;
exports[`postgresql datasource tests getDataTriggerInvocations should generate SQL to fetch invocations for an event 1`] = `
"SELECT *
FROM \\"hdb_catalog\\".\\"event_invocation_logs\\"

View File

@ -62,6 +62,21 @@ describe('postgresql datasource tests', () => {
});
});
describe('getAlterViewCommentSql', () => {
const { getAlterViewCommentSql } = postgres;
it('should generate SQL for modifying view comment', () => {
const query = getAlterViewCommentSql({
viewName: 'view_users',
schemaName: 'public',
comment: "user's comment",
});
expect(query).toContain('comment on view');
expect(query).toContain('"public"."view_users"');
expect(query).toContain("E'user\\'s comment'");
expect(query).toMatchSnapshot();
});
});
describe('getAlterFunctionCommentSql', () => {
const { getAlterFunctionCommentSql } = postgres;
it('should generate SQL for modifying function comment', () => {

View File

@ -73,6 +73,7 @@ import {
schemaListQuery,
getAlterTableCommentSql,
getAlterColumnCommentSql,
getAlterViewCommentSql,
getAlterFunctionCommentSql,
getDataTriggerInvocations,
getDataTriggerLogsCountQuery,
@ -869,6 +870,7 @@ export const postgres: DataSourcesAPI = {
schemaListQuery,
getAlterTableCommentSql,
getAlterColumnCommentSql,
getAlterViewCommentSql,
getAlterFunctionCommentSql,
getDataTriggerInvocations,
getDataTriggerLogsCountQuery,

View File

@ -680,6 +680,13 @@ export const getAlterColumnCommentSql: DataSourcesAPI['getAlterColumnCommentSql'
`;
};
export const getAlterViewCommentSql: DataSourcesAPI['getAlterViewCommentSql'] =
({ viewName, schemaName, comment }) => {
return `comment on view "${schemaName}"."${viewName}" is ${
comment ? sqlEscapeText(comment) : 'NULL'
}`;
};
export const getAlterFunctionCommentSql: DataSourcesAPI['getAlterFunctionCommentSql'] =
({ functionName, schemaName, comment }) => {
return `