def get_db_tables(conn): """Get current and default table values from the db. :param engine: Initialized alembic engine object. :type engine: object :returns: tuple """ query = text("SELECT TABLE_NAME from information_schema.tables\ WHERE TABLE_NAME\ LIKE '%alembic_version%'\ AND table_schema = 'refstack'") context = alembic_migration.MigrationContext.configure(conn) op = Operations(context) connection = op.get_bind() search = connection.execute(query) result = search.fetchall() if isinstance(result, Iterable): result = [table[0] for table in result] else: result = None # if there is more than one version table, modify the # one that does not have the default name, because subunit2sql uses the # default name. if result: current_name =\ next((table for table in result if table != "alembic_version"), result[0]) current_name = current_name.decode('utf-8') current_version = get_table_version(conn, current_name) default_name =\ next((table for table in result if table == "alembic_version"), None) default_version = get_table_version(conn, default_name) if len(result) > 1 and not current_version: if not default_name: # this is the case where there is more than one # nonstandard-named alembic table, and no default current_name = next( (table for table in result if table != current_name), result[0]) current_name = current_name.decode('utf-8') elif current_name: # this is the case where the current-named table # exists, but is empty current_name = default_name current_version = default_version current_table = (current_name, current_version) default_table = (default_name, default_version) else: default_table = (None, None) current_table = default_table return current_table, default_table
def get_db_tables(conn): """Get current and default table values from the db. :param engine: Initialized alembic engine object. :type engine: object :returns: tuple """ query = text("SELECT TABLE_NAME from information_schema.tables\ WHERE TABLE_NAME\ LIKE '%alembic_version%'\ AND table_schema = 'refstack'") context = alembic_migration.MigrationContext.configure(conn) op = Operations(context) connection = op.get_bind() search = connection.execute(query) result = search.fetchall() if isinstance(result, Iterable): result = [table[0] for table in result] else: result = None # if there is more than one version table, modify the # one that does not have the default name, because subunit2sql uses the # default name. if result: current_name =\ next((table for table in result if table != "alembic_version"), result[0]) current_name = current_name.decode('utf-8') current_version = get_table_version(conn, current_name) default_name =\ next((table for table in result if table == "alembic_version"), None) default_version = get_table_version(conn, default_name) if len(result) > 1 and not current_version: if not default_name: # this is the case where there is more than one # nonstandard-named alembic table, and no default current_name = next((table for table in result if table != current_name), result[0]) current_name = current_name.decode('utf-8') elif current_name: # this is the case where the current-named table # exists, but is empty current_name = default_name current_version = default_version current_table = (current_name, current_version) default_table = (default_name, default_version) else: default_table = (None, None) current_table = default_table return current_table, default_table