From f3905e3ab81115d78569c191af504266e87d0078 Mon Sep 17 00:00:00 2001 From: Muhammad Ubaid Raza Date: Thu, 23 Apr 2020 11:19:59 +0500 Subject: [PATCH 01/10] migrate to ts: GraphQL utils,SQL utils,Main utils --- .../{graphqlUtils.js => graphqlUtils.ts} | 10 +- .../src/components/Common/utils/sqlUtils.js | 76 --------------- .../src/components/Common/utils/sqlUtils.ts | 95 +++++++++++++++++++ .../components/Main/{utils.js => utils.ts} | 20 ++-- 4 files changed, 116 insertions(+), 85 deletions(-) rename console/src/components/Common/utils/{graphqlUtils.js => graphqlUtils.ts} (84%) delete mode 100644 console/src/components/Common/utils/sqlUtils.js create mode 100644 console/src/components/Common/utils/sqlUtils.ts rename console/src/components/Main/{utils.js => utils.ts} (79%) diff --git a/console/src/components/Common/utils/graphqlUtils.js b/console/src/components/Common/utils/graphqlUtils.ts similarity index 84% rename from console/src/components/Common/utils/graphqlUtils.js rename to console/src/components/Common/utils/graphqlUtils.ts index e2213a20e1f5e..d7591291dfb3a 100644 --- a/console/src/components/Common/utils/graphqlUtils.js +++ b/console/src/components/Common/utils/graphqlUtils.ts @@ -1,14 +1,19 @@ +import { + buildClientSchema, + getIntrospectionQuery, + GraphQLSchema, +} from 'graphql'; import React from 'react'; -import { getIntrospectionQuery, buildClientSchema } from 'graphql'; import endpoints from '../../../Endpoints'; export const useIntrospectionSchema = (headers = {}) => { - const [schema, setSchema] = React.useState(null); + const [schema, setSchema] = React.useState(null); const [loading, setLoading] = React.useState(true); const [error, setError] = React.useState(null); const introspect = () => { setLoading(true); + fetch(endpoints.graphQLUrl, { method: 'POST', headers, @@ -28,6 +33,7 @@ export const useIntrospectionSchema = (headers = {}) => { setLoading(false); setError(e); }); + return () => setSchema(null); }; diff --git a/console/src/components/Common/utils/sqlUtils.js b/console/src/components/Common/utils/sqlUtils.js deleted file mode 100644 index 342d14e4e28fc..0000000000000 --- a/console/src/components/Common/utils/sqlUtils.js +++ /dev/null @@ -1,76 +0,0 @@ -export const sqlEscapeText = text => { - let _text = text; - - if (_text) { - _text = _text.replace(/'/g, "\\'"); - } - - return `E'${_text}'`; -}; - -// detect DDL statements in SQL -export const checkSchemaModification = _sql => { - let _isSchemaModification = false; - - const sqlStatements = _sql - .toLowerCase() - .split(';') - .map(s => s.trim()); - - sqlStatements.forEach(statement => { - if ( - statement.startsWith('create ') || - statement.startsWith('alter ') || - statement.startsWith('drop ') - ) { - _isSchemaModification = true; - } - }); - - return _isSchemaModification; -}; - -export const getCheckConstraintBoolExp = check => { - if (check) { - return check.substring(7, check.length - 1); - } - - return check; -}; - -/* queries */ - -export const getCreateCheckConstraintSql = ( - tableName, - schemaName, - constraintName, - check -) => { - return `alter table "${schemaName}"."${tableName}" add constraint "${constraintName}" check (${check})`; -}; - -export const getDropConstraintSql = (tableName, schemaName, constraintName) => { - return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}"`; -}; - -export const getCreatePkSql = ({ - schemaName, - tableName, - selectedPkColumns, - constraintName, -}) => { - return `alter table "${schemaName}"."${tableName}" - add constraint "${constraintName}" - primary key ( ${selectedPkColumns.map(pkc => `"${pkc}"`).join(', ')} );`; -}; - -export const getDropPkSql = ({ schemaName, tableName, constraintName }) => { - return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}";`; -}; - -export const terminateSql = sql => { - const sqlSanitised = sql.trim(); - return sqlSanitised[sqlSanitised.length - 1] !== ';' - ? sqlSanitised + ';' - : sqlSanitised; -}; diff --git a/console/src/components/Common/utils/sqlUtils.ts b/console/src/components/Common/utils/sqlUtils.ts new file mode 100644 index 0000000000000..f42ab870c1536 --- /dev/null +++ b/console/src/components/Common/utils/sqlUtils.ts @@ -0,0 +1,95 @@ +interface SqlUtilsOptions { + tableName: string; + schemaName: string; + constraintName: string; + check?: string; + selectedPkColumns?: string[]; +} + +// all function could benefit from above interface for uniform function systax +// requires editing other files to update call references, so will update in next iteration +export const sqlEscapeText = (text: string) => { + let escapedText = text; + + if (escapedText) { + escapedText = escapedText.replace(/'/g, "\\'"); + } + + return `E'${escapedText}'`; +}; + +// detect DDL statements in SQL +export const checkSchemaModification = (_sql: string) => { + let isSchemaModification = false; + + const sqlStatements = _sql + .toLowerCase() + .split(';') + .map((s: string) => s.trim()); + + sqlStatements.forEach((statement: string) => { + if ( + statement.startsWith('create ') || + statement.startsWith('alter ') || + statement.startsWith('drop ') + ) { + isSchemaModification = true; + } + }); + + return isSchemaModification; +}; + +export const getCheckConstraintBoolExp = (check: string) => { + if (check) { + return check.substring(7, check.length - 1); + } + + return check; +}; + +/* queries */ + +export const getCreateCheckConstraintSql = ( + tableName: string, + schemaName: string, + constraintName: string, + check: string +) => { + return `alter table "${schemaName}"."${tableName}" add constraint "${constraintName}" check (${check})`; +}; + +export const getDropConstraintSql = ( + tableName: string, + schemaName: string, + constraintName: string +) => { + return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}"`; +}; + +export const getCreatePkSql = (options: SqlUtilsOptions) => { + const { schemaName, tableName, selectedPkColumns, constraintName } = options; + + // if no primary key columns provided, return empty query + if (!selectedPkColumns || selectedPkColumns.length === 0) { + return ''; + } + + return `alter table "${schemaName}"."${tableName}" + add constraint "${constraintName}" + primary key ( ${selectedPkColumns + .map((pkc: string) => `"${pkc}"`) + .join(', ')} );`; +}; + +export const getDropPkSql = (options: SqlUtilsOptions) => { + const { schemaName, tableName, constraintName } = options; + + return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}";`; +}; + +export const terminateSql = (sql: string) => { + const sqlSanitised = sql.trim(); + + return sqlSanitised[sqlSanitised.length - 1] !== ';' || sqlSanitised; +}; diff --git a/console/src/components/Main/utils.js b/console/src/components/Main/utils.ts similarity index 79% rename from console/src/components/Main/utils.js rename to console/src/components/Main/utils.ts index 046717a457e90..2d7decf327946 100644 --- a/console/src/components/Main/utils.js +++ b/console/src/components/Main/utils.ts @@ -6,41 +6,47 @@ const defaultState = { const defaultProClickState = { isProClicked: false, }; -const setLoveConsentState = stateData => { + +const setLoveConsentState = (stateData: { isDismissed: boolean }) => { window.localStorage.setItem(loveConsentState, JSON.stringify(stateData)); }; -const getLoveConsentState = stateData => { - const s = window.localStorage.getItem( - loveConsentState, - JSON.stringify(stateData) - ); +const getLoveConsentState = () => { + const s = window.localStorage.getItem(loveConsentState); + if (s) { return JSON.parse(s); } + window.localStorage.setItem(loveConsentState, JSON.stringify(defaultState)); + return defaultState; }; -const setProClickState = proStateData => { +const setProClickState = (proStateData: { isProClicked: boolean }) => { window.localStorage.setItem(proClickState, JSON.stringify(proStateData)); }; + const getProClickState = () => { try { const p = window.localStorage.getItem(proClickState); + if (p) { return JSON.parse(p); } + window.localStorage.setItem( proClickState, JSON.stringify(defaultProClickState) ); + return defaultProClickState; } catch (e) { console.error(e); return defaultProClickState; } }; + export { getLoveConsentState, setLoveConsentState, From b2c78eea87f9deefdec949f0a1eb2d455cb36791 Mon Sep 17 00:00:00 2001 From: Muhammad Ubaid Raza Date: Thu, 23 Apr 2020 12:24:09 +0500 Subject: [PATCH 02/10] fix logic in terminateSql --- console/src/components/Common/utils/sqlUtils.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/console/src/components/Common/utils/sqlUtils.ts b/console/src/components/Common/utils/sqlUtils.ts index f42ab870c1536..9c73e5e9826d2 100644 --- a/console/src/components/Common/utils/sqlUtils.ts +++ b/console/src/components/Common/utils/sqlUtils.ts @@ -91,5 +91,7 @@ export const getDropPkSql = (options: SqlUtilsOptions) => { export const terminateSql = (sql: string) => { const sqlSanitised = sql.trim(); - return sqlSanitised[sqlSanitised.length - 1] !== ';' || sqlSanitised; + return sqlSanitised[sqlSanitised.length - 1] !== ';' + ? `${sqlSanitised};` + : sqlSanitised; }; From 3ba751491b87dad1fa81702fc877b1c7f17af5e1 Mon Sep 17 00:00:00 2001 From: Muhammad Ubaid Raza Date: Thu, 23 Apr 2020 12:26:46 +0500 Subject: [PATCH 03/10] match var to function --- console/src/components/Common/utils/sqlUtils.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/console/src/components/Common/utils/sqlUtils.ts b/console/src/components/Common/utils/sqlUtils.ts index 9c73e5e9826d2..9e06cee532250 100644 --- a/console/src/components/Common/utils/sqlUtils.ts +++ b/console/src/components/Common/utils/sqlUtils.ts @@ -89,9 +89,9 @@ export const getDropPkSql = (options: SqlUtilsOptions) => { }; export const terminateSql = (sql: string) => { - const sqlSanitised = sql.trim(); + const sqlTerminated = sql.trim(); - return sqlSanitised[sqlSanitised.length - 1] !== ';' - ? `${sqlSanitised};` - : sqlSanitised; + return sqlTerminated[sqlTerminated.length - 1] !== ';' + ? `${sqlTerminated};` + : sqlTerminated; }; From c6105d6461418ac364fd53ef55bb0b7899b0c612 Mon Sep 17 00:00:00 2001 From: Muhammad Ubaid Raza Date: Thu, 23 Apr 2020 21:03:38 +0500 Subject: [PATCH 04/10] Update console/src/components/Common/utils/sqlUtils.ts Co-Authored-By: Aleksandra Sikora --- console/src/components/Common/utils/sqlUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/console/src/components/Common/utils/sqlUtils.ts b/console/src/components/Common/utils/sqlUtils.ts index 9e06cee532250..0e71605eb8ba9 100644 --- a/console/src/components/Common/utils/sqlUtils.ts +++ b/console/src/components/Common/utils/sqlUtils.ts @@ -78,7 +78,7 @@ export const getCreatePkSql = (options: SqlUtilsOptions) => { return `alter table "${schemaName}"."${tableName}" add constraint "${constraintName}" primary key ( ${selectedPkColumns - .map((pkc: string) => `"${pkc}"`) + .map(pkc => `"${pkc}"`) .join(', ')} );`; }; From 82fb5db41a54d930a1d43457341e82700f267adb Mon Sep 17 00:00:00 2001 From: Muhammad Ubaid Raza Date: Thu, 23 Apr 2020 21:09:16 +0500 Subject: [PATCH 05/10] Update console/src/components/Common/utils/sqlUtils.ts Co-Authored-By: Aleksandra Sikora --- console/src/components/Common/utils/sqlUtils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/console/src/components/Common/utils/sqlUtils.ts b/console/src/components/Common/utils/sqlUtils.ts index 0e71605eb8ba9..2778a5aa1612c 100644 --- a/console/src/components/Common/utils/sqlUtils.ts +++ b/console/src/components/Common/utils/sqlUtils.ts @@ -6,7 +6,6 @@ interface SqlUtilsOptions { selectedPkColumns?: string[]; } -// all function could benefit from above interface for uniform function systax // requires editing other files to update call references, so will update in next iteration export const sqlEscapeText = (text: string) => { let escapedText = text; From 4e2fa4013be344c523b96afa48e947f7204524f9 Mon Sep 17 00:00:00 2001 From: Muhammad Ubaid Raza Date: Thu, 23 Apr 2020 21:09:25 +0500 Subject: [PATCH 06/10] Update console/src/components/Common/utils/sqlUtils.ts Co-Authored-By: Aleksandra Sikora --- console/src/components/Common/utils/sqlUtils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/console/src/components/Common/utils/sqlUtils.ts b/console/src/components/Common/utils/sqlUtils.ts index 2778a5aa1612c..b5341c42726f1 100644 --- a/console/src/components/Common/utils/sqlUtils.ts +++ b/console/src/components/Common/utils/sqlUtils.ts @@ -6,7 +6,6 @@ interface SqlUtilsOptions { selectedPkColumns?: string[]; } -// requires editing other files to update call references, so will update in next iteration export const sqlEscapeText = (text: string) => { let escapedText = text; From dd3acd52aa8878e578dbaf04fd60b356b6bc98fd Mon Sep 17 00:00:00 2001 From: Muhammad Ubaid Raza Date: Thu, 23 Apr 2020 21:09:35 +0500 Subject: [PATCH 07/10] Update console/src/components/Common/utils/sqlUtils.ts Co-Authored-By: Aleksandra Sikora --- console/src/components/Common/utils/sqlUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/console/src/components/Common/utils/sqlUtils.ts b/console/src/components/Common/utils/sqlUtils.ts index b5341c42726f1..cde49bf443703 100644 --- a/console/src/components/Common/utils/sqlUtils.ts +++ b/console/src/components/Common/utils/sqlUtils.ts @@ -23,7 +23,7 @@ export const checkSchemaModification = (_sql: string) => { const sqlStatements = _sql .toLowerCase() .split(';') - .map((s: string) => s.trim()); + .map(s=> s.trim()); sqlStatements.forEach((statement: string) => { if ( From 50162c441b0cd09c1cf8a0e9f4c8e9a0cdc33ca0 Mon Sep 17 00:00:00 2001 From: Muhammad Ubaid Raza Date: Thu, 23 Apr 2020 21:09:43 +0500 Subject: [PATCH 08/10] Update console/src/components/Common/utils/sqlUtils.ts Co-Authored-By: Aleksandra Sikora --- console/src/components/Common/utils/sqlUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/console/src/components/Common/utils/sqlUtils.ts b/console/src/components/Common/utils/sqlUtils.ts index cde49bf443703..721e0b2ba0f79 100644 --- a/console/src/components/Common/utils/sqlUtils.ts +++ b/console/src/components/Common/utils/sqlUtils.ts @@ -65,7 +65,7 @@ export const getDropConstraintSql = ( return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}"`; }; -export const getCreatePkSql = (options: SqlUtilsOptions) => { +export const getCreatePkSql = ({ schemaName, tableName, selectedPkColumns, constraintName }: SqlUtilsOptions) => { const { schemaName, tableName, selectedPkColumns, constraintName } = options; // if no primary key columns provided, return empty query From cb7ef96bde92a79fb09eec1183a6a76fa05987a0 Mon Sep 17 00:00:00 2001 From: Muhammad Ubaid Raza Date: Thu, 23 Apr 2020 21:09:52 +0500 Subject: [PATCH 09/10] Update console/src/components/Common/utils/sqlUtils.ts Co-Authored-By: Aleksandra Sikora --- console/src/components/Common/utils/sqlUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/console/src/components/Common/utils/sqlUtils.ts b/console/src/components/Common/utils/sqlUtils.ts index 721e0b2ba0f79..eeb1e3a9e69aa 100644 --- a/console/src/components/Common/utils/sqlUtils.ts +++ b/console/src/components/Common/utils/sqlUtils.ts @@ -80,7 +80,7 @@ export const getCreatePkSql = ({ schemaName, tableName, selectedPkColumns, const .join(', ')} );`; }; -export const getDropPkSql = (options: SqlUtilsOptions) => { +export const getDropPkSql = ({ schemaName, tableName, constraintName }: SqlUtilsOptions) => { const { schemaName, tableName, constraintName } = options; return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}";`; From d4c9d4c99b8545604c6baf1e9e518ec5b788f55e Mon Sep 17 00:00:00 2001 From: Muhammad Ubaid Raza Date: Thu, 23 Apr 2020 21:26:08 +0500 Subject: [PATCH 10/10] update destructuring usage --- .../src/components/Common/utils/sqlUtils.ts | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/console/src/components/Common/utils/sqlUtils.ts b/console/src/components/Common/utils/sqlUtils.ts index eeb1e3a9e69aa..6afaf7fdf80b1 100644 --- a/console/src/components/Common/utils/sqlUtils.ts +++ b/console/src/components/Common/utils/sqlUtils.ts @@ -23,7 +23,7 @@ export const checkSchemaModification = (_sql: string) => { const sqlStatements = _sql .toLowerCase() .split(';') - .map(s=> s.trim()); + .map(s => s.trim()); sqlStatements.forEach((statement: string) => { if ( @@ -65,9 +65,12 @@ export const getDropConstraintSql = ( return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}"`; }; -export const getCreatePkSql = ({ schemaName, tableName, selectedPkColumns, constraintName }: SqlUtilsOptions) => { - const { schemaName, tableName, selectedPkColumns, constraintName } = options; - +export const getCreatePkSql = ({ + schemaName, + tableName, + selectedPkColumns, + constraintName, +}: SqlUtilsOptions) => { // if no primary key columns provided, return empty query if (!selectedPkColumns || selectedPkColumns.length === 0) { return ''; @@ -75,14 +78,14 @@ export const getCreatePkSql = ({ schemaName, tableName, selectedPkColumns, const return `alter table "${schemaName}"."${tableName}" add constraint "${constraintName}" - primary key ( ${selectedPkColumns - .map(pkc => `"${pkc}"`) - .join(', ')} );`; + primary key ( ${selectedPkColumns.map(pkc => `"${pkc}"`).join(', ')} );`; }; -export const getDropPkSql = ({ schemaName, tableName, constraintName }: SqlUtilsOptions) => { - const { schemaName, tableName, constraintName } = options; - +export const getDropPkSql = ({ + schemaName, + tableName, + constraintName, +}: SqlUtilsOptions) => { return `alter table "${schemaName}"."${tableName}" drop constraint "${constraintName}";`; };