Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 02d13ba

Browse filesBrowse files
Rishichandra Wawhalrikinsk
authored andcommitted
run sql queries refactor (close hasura#3543) (hasura#3662)
1 parent 6a4d643 commit 02d13ba
Copy full SHA for 02d13ba

File tree

Expand file treeCollapse file tree

11 files changed

+278
-573
lines changed
Filter options
Expand file treeCollapse file tree

11 files changed

+278
-573
lines changed

‎console/src/components/Common/utils/sqlUtils.js

Copy file name to clipboardExpand all lines: console/src/components/Common/utils/sqlUtils.js
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,10 @@ export const getCreatePkSql = ({
6767
export const getDropPkSql = ({ schemaName, tableName, constraintName }) => {
6868
return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}";`;
6969
};
70+
71+
export const terminateSql = sql => {
72+
const sqlSanitised = sql.trim();
73+
return sqlSanitised[sqlSanitised.length - 1] !== ';'
74+
? sqlSanitised + ';'
75+
: sqlSanitised;
76+
};

‎console/src/components/Common/utils/v1QueryUtils.js

Copy file name to clipboardExpand all lines: console/src/components/Common/utils/v1QueryUtils.js
+29-1Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { terminateSql } from './sqlUtils';
2+
13
export const getRunSqlQuery = (sql, shouldCascade, readOnly) => {
24
return {
35
type: 'run_sql',
46
args: {
5-
sql,
7+
sql: terminateSql(sql),
68
cascade: !!shouldCascade,
79
read_only: !!readOnly,
810
},
@@ -50,3 +52,29 @@ export const getSetCustomRootFieldsQuery = (
5052
},
5153
};
5254
};
55+
56+
export const getSetTableEnumQuery = (tableDef, isEnum) => {
57+
return {
58+
type: 'set_table_is_enum',
59+
args: {
60+
table: tableDef,
61+
is_enum: isEnum,
62+
},
63+
};
64+
};
65+
66+
export const getTrackTableQuery = tableDef => {
67+
return {
68+
type: 'add_existing_table_or_view',
69+
args: tableDef,
70+
};
71+
};
72+
73+
export const getUntrackTableQuery = tableDef => {
74+
return {
75+
type: 'untrack_table',
76+
args: {
77+
table: tableDef,
78+
},
79+
};
80+
};

‎console/src/components/Services/Data/Add/AddActions.js

Copy file name to clipboardExpand all lines: console/src/components/Services/Data/Add/AddActions.js
+4-14Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { setTable } from '../DataActions.js';
1111

1212
import { isPostgresFunction } from '../utils';
1313
import { sqlEscapeText } from '../../../Common/utils/sqlUtils';
14+
import { getRunSqlQuery } from '../../../Common/utils/v1QueryUtils';
1415
import { getTableModifyRoute } from '../../../Common/utils/routesUtils';
1516

1617
const SET_DEFAULTS = 'AddTable/SET_DEFAULTS';
@@ -315,16 +316,10 @@ const createTableSql = () => {
315316
if (hasUUIDDefault) {
316317
const sqlCreateExtension = 'CREATE EXTENSION IF NOT EXISTS pgcrypto;';
317318

318-
upQueryArgs.push({
319-
type: 'run_sql',
320-
args: { sql: sqlCreateExtension },
321-
});
319+
upQueryArgs.push(getRunSqlQuery(sqlCreateExtension));
322320
}
323321

324-
upQueryArgs.push({
325-
type: 'run_sql',
326-
args: { sql: sqlCreateTable },
327-
});
322+
upQueryArgs.push(getRunSqlQuery(sqlCreateTable));
328323

329324
upQueryArgs.push({
330325
type: 'add_existing_table_or_view',
@@ -345,12 +340,7 @@ const createTableSql = () => {
345340

346341
const downQuery = {
347342
type: 'bulk',
348-
args: [
349-
{
350-
type: 'run_sql',
351-
args: { sql: sqlDropTable },
352-
},
353-
],
343+
args: [getRunSqlQuery(sqlDropTable)],
354344
};
355345

356346
// make request

‎console/src/components/Services/Data/DataActions.js

Copy file name to clipboardExpand all lines: console/src/components/Services/Data/DataActions.js
+12-22Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sanitize from 'sanitize-filename';
22

33
import { getSchemaBaseRoute } from '../../Common/utils/routesUtils';
4+
import { getRunSqlQuery } from '../../Common/utils/v1QueryUtils';
45
import Endpoints, { globalCookiePolicy } from '../../../Endpoints';
56
import requestAction from '../../../utils/requestAction';
67
import defaultState from './DataState';
@@ -547,28 +548,17 @@ const makeMigrationCall = (
547548
};
548549

549550
const getBulkColumnInfoFetchQuery = schema => {
550-
const fetchColumnTypes = {
551-
type: 'run_sql',
552-
args: {
553-
sql: fetchColumnTypesQuery,
554-
read_only: true,
555-
},
556-
};
557-
const fetchTypeDefaultValues = {
558-
type: 'run_sql',
559-
args: {
560-
sql: fetchColumnDefaultFunctions(schema),
561-
read_only: true,
562-
},
563-
};
564-
565-
const fetchValidTypeCasts = {
566-
type: 'run_sql',
567-
args: {
568-
sql: fetchColumnCastsQuery,
569-
read_only: true,
570-
},
571-
};
551+
const fetchColumnTypes = getRunSqlQuery(fetchColumnTypesQuery, false, true);
552+
const fetchTypeDefaultValues = getRunSqlQuery(
553+
fetchColumnDefaultFunctions(schema),
554+
false,
555+
true
556+
);
557+
const fetchValidTypeCasts = getRunSqlQuery(
558+
fetchColumnCastsQuery,
559+
false,
560+
true
561+
);
572562

573563
return {
574564
type: 'bulk',

‎console/src/components/Services/Data/Function/customFunctionReducer.js

Copy file name to clipboardExpand all lines: console/src/components/Services/Data/Function/customFunctionReducer.js
+3-10Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { fetchTrackedFunctions } from '../DataActions';
2020

2121
import _push from '../push';
2222
import { getSchemaBaseRoute } from '../../../Common/utils/routesUtils';
23+
import { getRunSqlQuery } from '../../../Common/utils/v1QueryUtils';
2324

2425
/* Constants */
2526

@@ -193,19 +194,11 @@ const deleteFunctionSql = () => {
193194
const sqlDropFunction =
194195
'DROP FUNCTION ' + functionNameWithSchema + functionArgString;
195196

196-
const sqlUpQueries = [
197-
{
198-
type: 'run_sql',
199-
args: { sql: sqlDropFunction },
200-
},
201-
];
197+
const sqlUpQueries = [getRunSqlQuery(sqlDropFunction)];
202198

203199
const sqlDownQueries = [];
204200
if (functionDefinition && functionDefinition.length > 0) {
205-
sqlDownQueries.push({
206-
type: 'run_sql',
207-
args: { sql: functionDefinition },
208-
});
201+
sqlDownQueries.push(getRunSqlQuery(functionDefinition));
209202
}
210203

211204
// Apply migrations

‎console/src/components/Services/Data/RawSQL/Actions.js

Copy file name to clipboardExpand all lines: console/src/components/Services/Data/RawSQL/Actions.js
+2-10Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { parseCreateSQL } from './utils';
1717
import dataHeaders from '../Common/Headers';
1818
import returnMigrateUrl from '../Common/getMigrateUrl';
19+
import { getRunSqlQuery } from '../../../Common/utils/v1QueryUtils';
1920

2021
const MAKING_REQUEST = 'RawSQL/MAKING_REQUEST';
2122
const SET_SQL = 'RawSQL/SET_SQL';
@@ -43,16 +44,7 @@ const executeSQL = (isMigration, migrationName) => (dispatch, getState) => {
4344
const isCascadeChecked = getState().rawSQL.isCascadeChecked;
4445

4546
let url = Endpoints.rawSQL;
46-
const schemaChangesUp = [
47-
{
48-
type: 'run_sql',
49-
args: {
50-
sql: sql,
51-
cascade: isCascadeChecked,
52-
read_only: readOnlyMode,
53-
},
54-
},
55-
];
47+
const schemaChangesUp = [getRunSqlQuery(sql, isCascadeChecked, readOnlyMode)];
5648
// check if track view enabled
5749

5850
if (getState().rawSQL.isTableTrackChecked) {

‎console/src/components/Services/Data/Schema/Actions.js

Copy file name to clipboardExpand all lines: console/src/components/Services/Data/Schema/Actions.js
+4-24Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import gqlPattern, { gqlSchemaErrorNotif } from '../Common/GraphQLValidation';
22
import { showErrorNotification } from '../../Common/Notification';
33
import { makeMigrationCall, fetchSchemaList } from '../DataActions';
44
import { getConfirmation } from '../../../Common/utils/jsUtils';
5+
import { getRunSqlQuery } from '../../../Common/utils/v1QueryUtils';
56

67
const getDropSchemaSql = schemaName => `drop schema "${schemaName}" cascade;`;
78

@@ -19,23 +20,9 @@ export const createNewSchema = (schemaName, successCb, errorCb) => {
1920
);
2021
}
2122

22-
const migrationUp = [
23-
{
24-
type: 'run_sql',
25-
args: {
26-
sql: getCreateSchemaSql(schemaName),
27-
},
28-
},
29-
];
23+
const migrationUp = [getRunSqlQuery(getCreateSchemaSql(schemaName))];
3024

31-
const migrationDown = [
32-
{
33-
type: 'run_sql',
34-
args: {
35-
sql: getDropSchemaSql(schemaName),
36-
},
37-
},
38-
];
25+
const migrationDown = [getRunSqlQuery(getDropSchemaSql(schemaName))];
3926

4027
const migrationName = `create_schema_${schemaName}`;
4128
const requestMsg = 'Creating schema';
@@ -86,14 +73,7 @@ export const deleteCurrentSchema = (successCb, errorCb) => {
8673
return;
8774
}
8875

89-
const migrationUp = [
90-
{
91-
type: 'run_sql',
92-
args: {
93-
sql: getDropSchemaSql(currentSchema),
94-
},
95-
},
96-
];
76+
const migrationUp = [getRunSqlQuery(getDropSchemaSql(currentSchema))];
9777
const migrationName = `drop_schema_${currentSchema}`;
9878
const requestMsg = 'Dropping schema';
9979
const successMsg = 'Successfully dropped schema';

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.