Skip to content

Commit

Permalink
Merge pull request #18 from consag/develop
Browse files Browse the repository at this point in the history
Oracle error handling
  • Loading branch information
jacbeekers authored May 11, 2020
2 parents 42cb6b5 + d4e593a commit ebb89e8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
3 changes: 3 additions & 0 deletions cicd/database/dbConstants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# @Author: Jac. Beekers
# @Version: 20190410.0 - JBE - Initial
# @Version: 20190817.0 - JBE - Added user/password functionality
# @Version: 20200511.0 - JBE - Added on_error

# Database artifacts
varOracleSchemaName='ORACLE_SCHEMA'
Expand All @@ -37,6 +38,7 @@
varOracleDatabaseUser = 'ORACLE_USER'
varDatabaseUserPassword = 'ORACLE_PASSWORD' # encrypted
varOracleTNSName = 'ORACLE_TNS'
varOnError = 'ORACLE_ON_ERROR'

##
# Database artifact defaults
Expand All @@ -46,5 +48,6 @@
DEFAULT_SQL_DEPLOYLIST ='sqlserver_deploylist.txt'
DEFAULT_DB2_DEPLOYLIST ='db2_deploylist.txt'
DEFAULT_SQL_PREFIX = ''
DEFAULT_ON_ERROR = 'REPORT'

NOT_SET ='NOT_SET'
5 changes: 5 additions & 0 deletions cicd/database/dbSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# @Author: Jac. Beekers
# @Version: 20190410.0 - JBE - Initial
# @Version: 20190817.0 - JBE - Added user/password functionality
# @Version: 20200511.0 - JBE - Added on_error
##

import cicd.database.dbConstants as constants
Expand All @@ -40,6 +41,8 @@
targetsqldir = constants.DEFAULT_TARGET_SQLDIR
databaseType = 'UNKNOWN'
sqlprefix = constants.DEFAULT_SQL_PREFIX
on_error = constants.DEFAULT_ON_ERROR

if os.name == 'nt':
sqlplus_command = 'sqlplus.exe'
else:
Expand All @@ -54,6 +57,7 @@ def getdbenvvars():
targetsqldir = completePath(os.environ.get(constants.varTargetSqlDir, constants.DEFAULT_TARGET_SQLDIR), generalsettings.sourceDir)
# prefix for ordered sql files
sqlprefix = os.environ.get(constants.varSqlPrefix, constants.DEFAULT_SQL_PREFIX)
on_error = os.environ.get(constants.varOnError, constants.DEFAULT_ON_ERROR)


def getschemaenvvars(schema):
Expand All @@ -74,6 +78,7 @@ def outdbenvvars():
supporting.log(logger, logging.INFO, thisproc, 'dbdeploylist is >' + dbdeploylist + "<.")
supporting.log(logger, logging.INFO, thisproc, 'sourcesqldir is >' + sourcesqldir +"<.")
supporting.log(logger, logging.INFO, thisproc, 'targetsqldir is >' + targetsqldir +"<.")
supporting.log(logger, logging.INFO, thisproc, 'on_error is >' + on_error + '<.')


def outschemaenvvars():
Expand Down
34 changes: 27 additions & 7 deletions cicd/database/utilities/OracleUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,36 @@ def run_sqlplus(self, sqlfile):
+ "\n" + self.spool_clause_off
+ "\n" + "exit 0")[0]
log(logger, logging.INFO, thisproc, "SQLPlus output: " + stdoutput)
if p.returncode == 0:
if self.on_sql_error == "IGNORE":
log(logger, logging.INFO, thisproc
, "on_sql_error=IGNORE, surpressed all output and errors. Returning OK.")
return err.OK
else:
if p.returncode == 0:
# if the script reaches the end, it will execute the exit 0 statement
if self.on_sql_error == "ABORT":
err.SQLPLUS_ERROR.message = stdoutput
return err.SQLPLUS_ERROR
else:
log(logger, logging.WARNING, thisproc, "Errors occurred but were ignored as on_sql_error is >"
+ self.on_sql_error + "<. You may want to check >" + self.output_file + "<.")
# ABORT also had set whenever sqlerror, so returncode==0 means everything is ok
return err.OK
else:
if self.on_sql_error == "REPORT":
if "ORA-" in stdoutput:
log(logger, logging.ERROR, thisproc, "Found ORA- error in >" + stdoutput
+ "<. Failing...")
err.SQLPLUS_ERROR.message = stdoutput
return err.SQLPLUS_ERROR
else:
log(logger, logging.INFO, thisproc, "No Oracle error found.")
return err.OK
else:
log(logger, logging.WARNING, thisproc,
"on_sql_error is >" + self.on_sql_error
+ "<, but should be IGNORE, ABORT or REPORT. Failing...")
err.SQLPLUS_ERROR.message = stdoutput
return err.SQLPLUS_ERROR
else:
log(logger, logging.ERROR, thisproc, "sqlplus exited with return code >" + str(p.returncode)
+ "<. Failing...")
err.SQLPLUS_ERROR.message = stdoutput
return err.SQLPLUS_ERROR

except FileNotFoundError as e:
log(logger, logging.ERROR, thisproc, e.strerror + ": " + dbSettings.sqlplus_command)
Expand Down
19 changes: 4 additions & 15 deletions cicd/deployOracle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License
#
# Copyright (c) 2019 Jac. Beekers
# Copyright (c) 2020 Jac. Beekers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,18 +21,6 @@
# SOFTWARE.
#

# MIT License
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
#

from supporting import log
import supporting
Expand All @@ -59,6 +47,7 @@ def __init__(self, schema):
self.database_tns_name, self.database_schema, self.database_user, self.database_user_password = dbSettings.getschemaenvvars(
schema)
self.sqldir = dbSettings.targetsqldir
self.on_error = dbSettings.on_error
dbSettings.outdbenvvars()
dbSettings.outschemaenvvars()

Expand Down Expand Up @@ -89,10 +78,10 @@ def deployArtifact(self):
oracle_util = OracleUtils.OracleUtilities(self.database_user
, self.database_user_password
, self.database_tns_name
, 'REPORT'
, self.on_error
, self.database_schema + '_sqloutput.log')
sqlplus_result = oracle_util.run_sqlplus(sqlfile)
if sqlplus_result.rc != 0:
if sqlplus_result.rc != err.OK:
log(self.logger, logging.WARNING, thisproc, "sqlplus returned >" + sqlplus_result.code + "<.")
overall_result = sqlplus_result

Expand Down
2 changes: 1 addition & 1 deletion version/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.2.15"
__version__ = "1.2.16"

0 comments on commit ebb89e8

Please sign in to comment.