improve migration detection logic in Raw SQL page (close #3266) (#3306)

This commit is contained in:
Rikin Kachhia 2019-11-11 14:25:10 +05:30 committed by GitHub
parent 4178230d93
commit 689c97e9a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 13 deletions

View File

@ -7,3 +7,25 @@ export const sqlEscapeText = text => {
return `E'${_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;
};

View File

@ -6,6 +6,7 @@ import 'brace/mode/sql';
import Modal from '../../../Common/Modal/Modal'; import Modal from '../../../Common/Modal/Modal';
import Button from '../../../Common/Button/Button'; import Button from '../../../Common/Button/Button';
import { parseCreateSQL } from './utils'; import { parseCreateSQL } from './utils';
import { checkSchemaModification } from '../../../Common/utils/sqlUtils';
import OverlayTrigger from 'react-bootstrap/lib/OverlayTrigger'; import OverlayTrigger from 'react-bootstrap/lib/OverlayTrigger';
import Tooltip from 'react-bootstrap/lib/Tooltip'; import Tooltip from 'react-bootstrap/lib/Tooltip';
@ -62,16 +63,6 @@ const RawSQL = ({
</Tooltip> </Tooltip>
); );
const isSchemaModification = _sql => {
const formattedSQL = _sql.toLowerCase();
return (
formattedSQL.includes('create') ||
formattedSQL.includes('alter') ||
formattedSQL.includes('drop')
);
};
const submitSQL = () => { const submitSQL = () => {
// check migration mode global // check migration mode global
if (migrationMode) { if (migrationMode) {
@ -84,7 +75,7 @@ const RawSQL = ({
} }
if (!isMigration && globals.consoleMode === CLI_CONSOLE_MODE) { if (!isMigration && globals.consoleMode === CLI_CONSOLE_MODE) {
// if migration is not checked, check if is schema modification // if migration is not checked, check if is schema modification
if (isSchemaModification(sql)) { if (checkSchemaModification(sql)) {
dispatch(modalOpen()); dispatch(modalOpen());
const confirmation = false; const confirmation = false;
if (confirmation) { if (confirmation) {
@ -166,14 +157,14 @@ const RawSQL = ({
dispatch({ type: SET_SQL, data: val }); dispatch({ type: SET_SQL, data: val });
// set migration checkbox true // set migration checkbox true
if (isSchemaModification(val)) { if (checkSchemaModification(val)) {
dispatch({ type: SET_MIGRATION_CHECKED, data: true }); dispatch({ type: SET_MIGRATION_CHECKED, data: true });
} else { } else {
dispatch({ type: SET_MIGRATION_CHECKED, data: false }); dispatch({ type: SET_MIGRATION_CHECKED, data: false });
} }
// set track this checkbox true // set track this checkbox true
const objects = parseCreateSQL(val, true); const objects = parseCreateSQL(val);
if (objects.length) { if (objects.length) {
let allObjectsTrackable = true; let allObjectsTrackable = true;