Skip to content

Commit

Permalink
Print status/diagnostics while backing up CDM tables (#4407)
Browse files Browse the repository at this point in the history
 - As per Dan's suggestion
  • Loading branch information
Nathan Graham committed Dec 2, 2016
1 parent 0e2085e commit b1fc110
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions Oracle/backup_cdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,50 @@ def main(get_cursor):
cursor, backup_schema = get_cursor()

for table in CDM3_TABLES:
if has_rows(cursor, table):
drop(cursor, table, backup_schema)
copy(cursor, table, table, backup_schema)


def drop(cursor, table, backup_schema):
bak_schema_table = ('%(backup_schema)s.%(table)s' %
dict(backup_schema=backup_schema,
table=table))
rcount = count_rows(cursor, table)
print ('%(table)s (to be backed up) currently has %(rcount)s rows.'
% dict(table=table, rcount=rcount))

if rcount:
drop(cursor, bak_schema_table)
copy(cursor, table, bak_schema_table)
print ('%(bak_schema_table)s now has %(rows)s rows.' %
dict(bak_schema_table=bak_schema_table,
rows=count_rows(cursor, bak_schema_table)))


def drop(cursor, schema_table):
print ('Dropping %(schema_table)s (%(rows)s rows).' %
dict(schema_table=schema_table,
rows=count_rows(cursor, schema_table)))
try:
cursor.execute('drop table %(backup_schema)s.%(table)s' %
dict(backup_schema=backup_schema, table=table))
cursor.execute('drop table %(schema_table)s' %
dict(schema_table=schema_table))
except DatabaseError, e: # noqa
# Table might not exist
pass


def has_rows(cursor, table):
ret = False
def count_rows(cursor, schema_table):
ret = None
try:
cursor.execute('select count(*) from %(table)s' %
dict(table=table))
if int(cursor.fetchall()[0][0]) > 0:
ret = True
cursor.execute('select count(*) from %(schema_table)s' %
dict(schema_table=schema_table))
ret = int(cursor.fetchall()[0][0])
except:
pass
return ret


def copy(cursor, table, new_name, backup_schema):
cursor.execute('create table %(backup_schema)s.%(table)s as '
def copy(cursor, table, bak_schema_table):
print ('Copying %(table)s to %(bak_schema_table)s.' %
dict(table=table, bak_schema_table=bak_schema_table))
cursor.execute('create table %(bak_schema_table)s as '
'select * from %(table)s' %
dict(backup_schema=backup_schema, table=table))
dict(bak_schema_table=bak_schema_table, table=table))


class MockCursor(object):
Expand Down

0 comments on commit b1fc110

Please sign in to comment.