From b1fc110f8892a09953e022f10c4981492955a9b2 Mon Sep 17 00:00:00 2001 From: Nathan Graham Date: Fri, 2 Dec 2016 14:15:43 -0600 Subject: [PATCH] Print status/diagnostics while backing up CDM tables (#4407) - As per Dan's suggestion --- Oracle/backup_cdm.py | 48 ++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/Oracle/backup_cdm.py b/Oracle/backup_cdm.py index 6e31a25..de8ef87 100644 --- a/Oracle/backup_cdm.py +++ b/Oracle/backup_cdm.py @@ -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):