diff --git a/console/src/components/Common/utils/sqlUtils.js b/console/src/components/Common/utils/sqlUtils.js index 10f432946f8..ab37b616797 100644 --- a/console/src/components/Common/utils/sqlUtils.js +++ b/console/src/components/Common/utils/sqlUtils.js @@ -7,3 +7,25 @@ export const sqlEscapeText = text => { 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; +}; diff --git a/console/src/components/Services/Data/RawSQL/RawSQL.js b/console/src/components/Services/Data/RawSQL/RawSQL.js index c69b77847d8..fe75d767744 100644 --- a/console/src/components/Services/Data/RawSQL/RawSQL.js +++ b/console/src/components/Services/Data/RawSQL/RawSQL.js @@ -6,6 +6,7 @@ import 'brace/mode/sql'; import Modal from '../../../Common/Modal/Modal'; import Button from '../../../Common/Button/Button'; import { parseCreateSQL } from './utils'; +import { checkSchemaModification } from '../../../Common/utils/sqlUtils'; import OverlayTrigger from 'react-bootstrap/lib/OverlayTrigger'; import Tooltip from 'react-bootstrap/lib/Tooltip'; @@ -62,16 +63,6 @@ const RawSQL = ({ ); - const isSchemaModification = _sql => { - const formattedSQL = _sql.toLowerCase(); - - return ( - formattedSQL.includes('create') || - formattedSQL.includes('alter') || - formattedSQL.includes('drop') - ); - }; - const submitSQL = () => { // check migration mode global if (migrationMode) { @@ -84,7 +75,7 @@ const RawSQL = ({ } if (!isMigration && globals.consoleMode === CLI_CONSOLE_MODE) { // if migration is not checked, check if is schema modification - if (isSchemaModification(sql)) { + if (checkSchemaModification(sql)) { dispatch(modalOpen()); const confirmation = false; if (confirmation) { @@ -166,14 +157,14 @@ const RawSQL = ({ dispatch({ type: SET_SQL, data: val }); // set migration checkbox true - if (isSchemaModification(val)) { + if (checkSchemaModification(val)) { dispatch({ type: SET_MIGRATION_CHECKED, data: true }); } else { dispatch({ type: SET_MIGRATION_CHECKED, data: false }); } // set track this checkbox true - const objects = parseCreateSQL(val, true); + const objects = parseCreateSQL(val); if (objects.length) { let allObjectsTrackable = true;