Skip to content

Commit

Permalink
Backup CDM tables to a different schema (#4407)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Graham committed Dec 2, 2016
1 parent 55d75b4 commit 0e2085e
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions Oracle/backup_cdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,19 @@


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')
drop(cursor, table, backup_schema)
copy(cursor, table, table, backup_schema)


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):
def drop(cursor, table, backup_schema):
try:
cursor.execute('drop table %(table)s' % dict(table=table))
except DatabaseError, e:
cursor.execute('drop table %(backup_schema)s.%(table)s' %
dict(backup_schema=backup_schema, table=table))
except DatabaseError, e: # noqa
# Table might not exist
pass

Expand All @@ -51,14 +41,15 @@ def has_rows(cursor, table):
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, new_name, backup_schema):
cursor.execute('create table %(backup_schema)s.%(table)s as '
'select * from %(table)s' %
dict(backup_schema=backup_schema, 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 +66,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 0e2085e

Please sign in to comment.