Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recipes/Python/576534_Dump_Oracle_db_schema_to_text: Added jobs and s… #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions recipes/Python/576534_Dump_Oracle_db_schema_to_text/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## Dump Oracle db schema to text
Originally published: 2008-10-10 05:34:02
Last updated: 2014-02-21 09:18:55
Author: Michal Niklas

## Dump Oracle db schema to text
Originally published: 2008-10-10 05:34:02
Last updated: 2014-02-21 09:18:55
Author: Michal Niklas, Dean Toader
Export Oracle schema to text.
Usable to compare databases that should be the same

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# http://code.activestate.com/recipes/576534-dump-oracle-db-schema-to-text/
#
# author: Michal Niklas, Adam Kopciński-Galik
# author: Michal Niklas, Adam Kopciński-Galik, Dean Toader

OPTIONS = """[OPTIONS]
Options:
Expand All @@ -29,7 +29,7 @@
tables are defined as CREATE TABLE statement
--sorted-info for CREATE TABLE add comment with columns
sorted by name
--tables-only raport only tables
--tables-only report only tables
--version show version
--verbose show more info in output (SQL queries)
--ver-info-sql[=SELECT ... FROM ... ORDER BY ...]
Expand Down Expand Up @@ -68,6 +68,10 @@
FUNCTIONS_INFO_DIR = SCHEMA_DIR + '/functions'
PROCEDURES_INFO_DIR = SCHEMA_DIR + '/procedures'
PACKAGES_INFO_DIR = SCHEMA_DIR + '/packages'
JOBS_INFO_DIR = SCHEMA_DIR + '/jobs'
SCHEDULES_INFO_DIR = SCHEMA_DIR + '/schedules'
TYPES_INFO_DIR = SCHEMA_DIR + '/types'
TRIGGERS_INFO_DIR = SCHEMA_DIR + '/triggers'
INVALID = '_invalid'

CREATED_FILES = []
Expand Down Expand Up @@ -856,7 +860,7 @@ def show_procedures(sql, separate_files, title, out_dir=None):
for line in lines:
output_line(line[0].rstrip(), fout)
if lines:
output_line('\n/', fout)
output_line('/', fout)
cur2.close()
output_line('\n\n -- <<< %s %s <<< --' % (title, funname))
if fout:
Expand Down Expand Up @@ -886,7 +890,7 @@ def show_packages(separate_files):
for line in lines:
output_line(line[0].rstrip(), fout)
if(lines):
output_line('\n/', fout)
output_line('/', fout)
output_line('\n')
cur2.close()
cur3 = db_conn().cursor()
Expand All @@ -897,7 +901,7 @@ def show_packages(separate_files):
for line in lines:
output_line(line[0].rstrip(), fout)
if(lines):
output_line('\n/', fout)
output_line('/', fout)
cur3.close()
if fout:
fout.close()
Expand All @@ -906,6 +910,53 @@ def show_packages(separate_files):
print_stop_info(title)


def show_using_get_ddl(separate_files, object_type, get_ddl_object_type, INFO_DIR):
"""shows SQL object_type"""
object_type_lower = object_type.lower()
title = object_type_lower + 's';
print_start_info(title)
fout = None
cur1 = db_conn().cursor()
cur1.execute("SELECT object_name FROM user_objects WHERE object_type='%s' ORDER BY 1" % (object_type))
rows = cur1.fetchall()
for row in rows:
funname = row[0]
output_line('\n\n -- >>> %s %s >>> --' % (object_type_lower,funname))
if separate_files:
fname = os.path.join(INFO_DIR, '%s.sql' % (normalize_fname(funname)))
fout = open_file_write(fname)
cur2 = db_conn().cursor()
cur2.execute("SELECT DBMS_METADATA.GET_DDL('%s','%s','HFOWNER') as text FROM dual" % (get_ddl_object_type, funname))
clob = cur2.fetchall()
output_line(clob[0][0].read().strip(), fout)
output_line('/', fout)
output_line('\n')
cur2.close()
if fout:
fout.close()
output_line('\n\n -- <<< %s %s <<< --' % (object_type_lower,funname))
cur1.close()


def show_jobs_files(separate_files):
"""shows SQL jobs"""
show_using_get_ddl(separate_files, 'JOB', 'PROCOBJ', JOBS_INFO_DIR)


def show_schedules_files(separate_files):
"""shows SQL schedules"""
show_using_get_ddl(separate_files, 'SCHEDULE', 'PROCOBJ', SCHEDULES_INFO_DIR)


def show_triggers_files(separate_files):
"""shows SQL triggers"""
show_using_get_ddl(separate_files, 'TRIGGER', 'TRIGGER', TRIGGERS_INFO_DIR)


def show_types_files(separate_files):
show_using_get_ddl(separate_files, 'TYPE', 'TYPE', TYPES_INFO_DIR)


def show_triggers():
"""shows SQL triggers"""
show_qry('triggers', TRIGGERS_INFO_SQL, '\n', '\n-- end trigger --\n')
Expand Down Expand Up @@ -980,6 +1031,10 @@ def show_additional_info(separate_files):
show_normal_procedures(separate_files, 'procedure', PROCEDURES_INFO_DIR)
show_invalid_procedures(separate_files, 'invalid procedure', PROCEDURES_INFO_DIR + INVALID)
show_packages(separate_files)
show_jobs_files(separate_files)
show_schedules_files(separate_files)
show_types_files(separate_files)
show_triggers_files(separate_files)
show_types()
show_types_stat()

Expand All @@ -1003,7 +1058,16 @@ def dump_db_info(separate_files, out_f, stdout):
t0 = time.time()
test = '--test' in sys.argv
if test or separate_files:
for dn in (TABLES_INFO_DIR, VIEWS_INFO_DIR, SEQUENCES_INFO_DIR, FUNCTIONS_INFO_DIR, PROCEDURES_INFO_DIR, PACKAGES_INFO_DIR):
for dn in (TABLES_INFO_DIR,
VIEWS_INFO_DIR,
SEQUENCES_INFO_DIR,
FUNCTIONS_INFO_DIR,
PROCEDURES_INFO_DIR,
PACKAGES_INFO_DIR,
JOBS_INFO_DIR,
SCHEDULES_INFO_DIR,
TYPES_INFO_DIR,
TRIGGERS_INFO_DIR):
ensure_directory(dn)

if not test:
Expand Down