From f54a4caacaf0c9e31d621d58f0a05351c134d6c1 Mon Sep 17 00:00:00 2001 From: "Lynn \"Zhorken\" Vaughan" Date: Fri, 21 Feb 2014 16:45:47 -0500 Subject: [PATCH] General code tidying for this Oracle fix. --- pokedex/db/__init__.py | 6 ++---- pokedex/db/load.py | 46 +++++++++++++++++++++--------------------- pokedex/db/oracle.py | 35 +++++++++----------------------- 3 files changed, 35 insertions(+), 52 deletions(-) diff --git a/pokedex/db/__init__.py b/pokedex/db/__init__.py index ddcf799..3e8ecb1 100644 --- a/pokedex/db/__init__.py +++ b/pokedex/db/__init__.py @@ -25,7 +25,7 @@ def connect(uri=None, session_args={}, engine_args={}, engine_prefix=''): if uri is None: uri = get_default_db_uri() - ### Do some fixery for MySQL + ### Do some fixery for specific RDBMSes if uri.startswith('mysql:'): # MySQL uses latin1 for connections by default even if the server is # otherwise oozing with utf8; charset fixes this @@ -37,9 +37,7 @@ def connect(uri=None, session_args={}, engine_args={}, engine_prefix=''): for table in metadata.tables.values(): table.kwargs['mysql_engine'] = 'InnoDB' table.kwargs['mysql_charset'] = 'utf8' - - ### Do some fixery for Oracle - if uri.startswith('oracle:') or uri.startswith('oracle+cx_oracle:'): + elif uri.startswith(('oracle:', 'oracle+cx_oracle:')): # Oracle requires auto_setinputsizes=False (or at least a special # set of exclusions from it, which I don't know) if 'auto_setinputsizes' not in uri: diff --git a/pokedex/db/load.py b/pokedex/db/load.py index ee09721..01859a2 100644 --- a/pokedex/db/load.py +++ b/pokedex/db/load.py @@ -139,19 +139,16 @@ def load(session, tables=[], directory=None, drop_tables=False, verbose=False, s table_names = _get_table_names(metadata, tables) table_objs = [metadata.tables[name] for name in table_names] - # Oracle fixery, load needs short names - # flag for oracle stuff - oranames = (session.connection().dialect.name == 'oracle') - if oranames: - # Shorten table names, Oracle limits table and column names to 30 chars - # Make a dictionary to match old<->new names - oradict = rewrite_long_table_names() - if recursive: table_objs.extend(find_dependent_tables(table_objs)) table_objs = sqlalchemy.sql.util.sort_tables(table_objs) + # Limit table names to 30 characters for Oracle + oracle = (session.connection().dialect.name == 'oracle') + if oracle: + rewrite_long_table_names() + # SQLite speed tweaks if not safe and session.connection().dialect.name == 'sqlite': session.connection().execute("PRAGMA synchronous=OFF") @@ -186,15 +183,17 @@ def load(session, tables=[], directory=None, drop_tables=False, verbose=False, s # Okay, run through the tables and actually load the data now for table_obj in table_objs: - table_name = table_obj.name + if oracle: + table_name = table_obj._original_name + else: + table_name = table_obj.name + insert_stmt = table_obj.insert() print_start(table_name) + try: csvpath = "%s/%s.csv" % (directory, table_name) - # In oracle mode, use the original names instead of current - if oranames: - csvpath = "%s/%s.csv" % (directory, oradict[table_name]) csvfile = open(csvpath, 'rb') except IOError: # File doesn't exist; don't load anything! @@ -380,22 +379,23 @@ def dump(session, tables=[], directory=None, verbose=False, langs=['en']): table_names = _get_table_names(metadata, tables) table_names.sort() - # Oracle fixery : read from short table names, dump long names - oranames = (session.connection().dialect.name == 'oracle') - if oranames: - # Make a dictionary to match old<->new names - oradict = rewrite_long_table_names() + # Oracle needs to dump from tables with shortened names to csvs with the + # usual names + oracle = (session.connection().dialect.name == 'oracle') + if oracle: + rewrite_long_table_names() for table_name in table_names: print_start(table_name) table = metadata.tables[table_name] - writer = csv.writer(open("%s/%s.csv" % (directory, table_name), 'wb'), - lineterminator='\n') - # In oracle mode, use the original names instead of current - if oranames: - writer = csv.writer(open("%s/%s.csv" % (directory, oradict[table_name]), 'wb'), - lineterminator='\n') + if oracle: + filename = '%s/%s.csv' % (directory, table._original_name) + else: + filename = '%s/%s.csv' % (directory, table_name) + + writer = csv.writer(open(filename, 'wb'), lineterminator='\n') + columns = [col.name for col in table.columns] # For name tables, dump rows for official languages, as well as diff --git a/pokedex/db/oracle.py b/pokedex/db/oracle.py index 81b2105..ad9fd11 100644 --- a/pokedex/db/oracle.py +++ b/pokedex/db/oracle.py @@ -8,34 +8,19 @@ def rewrite_long_table_names(): Returns a dictionary matching short names -> long names. """ - # Load table names from metadata - t_names = metadata.tables.keys() - - table_names = list(t_names) - table_objs = [metadata.tables[name] for name in table_names] - - # Prepare a dictionary to match old<->new names - dictionary = {} + # Load tables from metadata + table_objs = metadata.tables.values() # Shorten table names, Oracle limits table and column names to 30 chars for table in table_objs: - table._orginal_name = table.name[:] - dictionary[table.name]=table._orginal_name - if len(table._orginal_name) > 30: - for letter in ['a', 'e', 'i', 'o', 'u', 'y']: - table.name=table.name.replace(letter,'') - dictionary[table.name]=table._orginal_name - return dictionary + table._original_name = table.name + if len(table.name) > 30: + for letter in 'aeiouy': + table.name = table.name.replace(letter, '') -def restore_long_table_names(metadata,dictionary): - """Modifies the table names to restore the long-naming. - - `metadata` - The metadata to restore. - - `dictionary` - The dictionary matching short name -> long name. - """ +def restore_long_table_names(): + """Modifies the table names to restore the long-naming.""" for table in metadata.tables.values(): - table.name = dictionary[table.name] + table.name = table._original_name + del table._original_name