Skip to content

Commit

Permalink
Adding disallowed keywords to script
Browse files Browse the repository at this point in the history
  • Loading branch information
wilo087 committed Dec 10, 2020
1 parent 0d2d251 commit b1bbba6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 44 deletions.
5 changes: 4 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Important:
# Do not use simple or double quotations

# Blocked keywords on scripts execution
DISALLOW_KEYWORDS=ANALYZE, AUDIT, ALTER, DATABASE, DROP, FOR, UPDATE, GRANT, LINK, LOCK, PUBLIC, TRUNCATE, RENAME, USER

# Developer schema setup
DB_SERVICE_NAME=DIAOMEGA
DB_HOST=maisi.om.do
Expand All @@ -13,4 +16,4 @@ DB_ADMIN_USER=sys
DB_ADMIN_PASSWORD=SYSPassword
DB_DEFAULT_TABLE_SPACE=OMEGA_DATA
DB_TEMP_TABLE_SPACE=TEMP_TBS
DB_MAIN_SCHEMA=OMEGA
DB_MAIN_SCHEMA=OMEGA
27 changes: 16 additions & 11 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,15 @@ def wc2db(dry_run, force):


def migrate(dry_run, force, name=None, types=None):
path = '/plsql/scripts/pendigns/T00001/AS_T00001_01_WDELACRUZ2_20201204092326.sql'
words = ['ANALYZE', 'AUDIT', 'ALTER', 'DATABASE', 'DROP', 'FOR', 'UPDATE', 'GRANT', 'LINK', 'LOCK', 'PUBLIC', 'TRUNCATE', 'RENAME', 'USER']

files.checkWordsInFile(wordList=words, path=path)
exit()

# If -d options exist, print dry run message
if dry_run:
utils.dryRun()

disallowed_keywords=[]
words = db.disallowed_keywords
if words:
disallowed_keywords = words.split(',')


failedGroups = []
dba = db.dbConnect(sysDBA=True)
Expand All @@ -284,7 +285,6 @@ def migrate(dry_run, force, name=None, types=None):
# Create it if not exist
db.scriptsMigrationTable()


fScripts = files.listAllScriptsFiles()
# Execute scripts by groups
for groupID, group in sorted(fScripts.items()):
Expand All @@ -298,16 +298,21 @@ def migrate(dry_run, force, name=None, types=None):
group = list(filter(lambda d: d['name'] in [name], group))

# For each script in group
for item in group:
for item in group:
# Before excute the script, we have to checkout if special keywords exist
wordInScripts = files.checkWordsInFile(wordList=disallowed_keywords, path=item['path'])
if wordInScripts:
item['output'] = "Please, remove the following DDL instructions: %s " % ', '.join(wordInScripts)
item['status'] = 'BLOCKED'
infoScript.add_row([item['name'], groupID, item['type'], item['status'], item['output']])
continue

# Is the object group exist on failed group, avoid it.
if groupID in failedGroups and not force:
item['status'] = 'HOLD'
db.insertOrUpdateMigration(item, db=dbm)
infoScript.add_row([item['name'], groupID, item['type'], item['status'], item['output']])
continue

# Before excute the script, we have to checkout if special keywords exist


dbScript = db.getMigration(scriptName=item['name'], db=dba)
if dbScript:
Expand Down
1 change: 1 addition & 0 deletions pladmin/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, displayInfo=False):
# Load database config
load_dotenv(files.db_cnfpath)

self.disallowed_keywords = os.getenv("DISALLOW_KEYWORDS")
self.db_admin_user = os.getenv("DB_ADMIN_USER").upper()
self.db_admin_password = os.getenv("DB_ADMIN_PASSWORD")
self.db_default_table_space = os.getenv("DB_DEFAULT_TABLE_SPACE").upper()
Expand Down
37 changes: 6 additions & 31 deletions pladmin/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,42 +371,17 @@ def listAllScriptsFiles(self):

return new


def checkWordsInFile(self, wordList, path):
wordScripts = []
fContent = open(path, 'r')
content = str(fContent.read())

# print(content.read())
# for line in file:
# line = line.strip()
# lista = line.split()
# print()

for word in wordList:
paterm = "(%s|%s)" % (word.upper(), word.lower())
d = re.findall(paterm, content)
if d:
wordScripts.extend(lista)
# print(word)


# print(paterm)
# if word in lista:
# print(word)
# break
# wordScripts.extend(lista)
# print(line)

# for word in wordList:
# print(word)
lowers = content.count(word.lower())
uppers = content.count(word.upper())
if lowers + uppers:
wordScripts.append(word)

# print(wordScripts)
# word_list = list(set(wordList))

# while True:
# word = input('Type in word, must be in English and at least 3 letters long: ')
# if word in word_list:
# break
# else:
# print("Try Again")
fContent.close()
return wordScripts
2 changes: 1 addition & 1 deletion pladmin/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def checkPlaceScript(self):
if existsWord > 0:
scriptsMove.append(dirFiles)
os.rename(scriptRevision, os.path.join(self.__asPath, dirFiles))

if scriptsMove:
message = ''' the scripts %s was moved to the execution of
ace scripts, because it contained ddl instructions' % scriptsMove '''
Expand Down

0 comments on commit b1bbba6

Please sign in to comment.