Skip to content

Commit

Permalink
Merge pull request ARCH-commons#45 from njgraham/backup_cdm_4407
Browse files Browse the repository at this point in the history
Backup CDM tables to a different schema (#4407)
  • Loading branch information
Michael Prittie authored Dec 2, 2016
2 parents 87111b5 + b1fc110 commit f7c8957
Showing 1 changed file with 38 additions and 33 deletions.
71 changes: 38 additions & 33 deletions Oracle/backup_cdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,58 @@


def main(get_cursor):
cursor = get_cursor()
cursor, backup_schema = get_cursor()

for table in CDM3_TABLES:
if has_rows(cursor, table):
drop_triggers(cursor, table)
drop(cursor, table + '_BAK')
rename(cursor, table, table + '_BAK')


def drop_triggers(cursor, table):
cursor.execute("""select trigger_name
from user_triggers
where table_name = '%(table)s'""" %
dict(table=table))

for res in cursor.fetchall():
cursor.execute('drop trigger %(trigger)s' % dict(trigger=res[0]))


def drop(cursor, table):
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 %(table)s' % dict(table=table))
except DatabaseError, e:
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 rename(cursor, table, new_name):
cursor.execute('alter table %(table)s rename to %(new_name)s' %
dict(table=table, new_name=new_name))
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(bak_schema_table=bak_schema_table, table=table))


class MockCursor(object):
def __init__(self):
self.fetch_count = 0
self.fetch_count = 1

def execute(self, sql):
print 'execute: ' + sql
Expand All @@ -75,17 +80,17 @@ def _tcb():
from sys import argv

def get_cursor():
host, port, sid = argv[1:4]
host, port, sid, backup_schema = argv[1:5]
user = environ['pcornet_cdm_user']
password = environ['pcornet_cdm']

if '--dry-run' in argv:
return MockCursor()
return MockCursor(), backup_schema
else:
import cx_Oracle as cx
conn = cx.connect(user, password,
dsn=cx.makedsn(host, port, sid))
return conn.cursor()
return conn.cursor(), backup_schema

main(get_cursor)
_tcb()

0 comments on commit f7c8957

Please sign in to comment.