-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete_without_where.py
65 lines (60 loc) · 1.73 KB
/
delete_without_where.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
###
### This script checks for the phrase "DELETE FROM" without "WHERE"
###
### Notes:
###
###
### Helpers come from Liquibase
###
import liquibase_utilities
import sqlparse
import sys
###
### Retrieve log handler
### Ex. liquibase_logger.info(message)
###
liquibase_logger = liquibase_utilities.get_logger()
###
### Retrieve status handler
###
liquibase_status = liquibase_utilities.get_status()
###
### Retrieve all changes in changeset
###
changes = liquibase_utilities.get_changeset().getChanges()
###
### Loop through all changes
###
for change in changes:
###
### LoadData change types are not currently supported
###
if "loaddatachange" in change.getClass().getSimpleName().lower():
continue
###
### Retrieve sql as string, remove extra whitespace
###
raw_sql = liquibase_utilities.strip_comments(liquibase_utilities.generate_sql(change)).casefold()
raw_sql = " ".join(raw_sql.split())
###
### Split sql into statements
###
raw_statements = liquibase_utilities.split_statements(raw_sql)
for raw_statement in raw_statements:
###
### Get list of token objects, convert to string
###
tokens = liquibase_utilities.tokenize(raw_statement)
keywords = [str(token) for token in tokens if token.is_keyword or isinstance(token, sqlparse.sql.Where)]
keywords = [keyword for keyword in " ".join(keywords).split()]
###
### Look for delete
###
if len(keywords) >= 2 and keywords[0] == "delete" and keywords[1] == "from" and "where" not in keywords:
liquibase_status.fired = True
liquibase_status.message = liquibase_utilities.get_script_message()
sys.exit(1)
###
### Default return code
###
False