<s> import os <EOL> from migrate import exceptions as versioning_exceptions <EOL> from migrate . versioning import api as versioning_api <EOL> from migrate . versioning . repository import Repository <EOL> from oslo_db . sqlalchemy import utils as db_utils <EOL> from oslo_log import log as logging <EOL> import sqlalchemy <EOL> from sqlalchemy . sql import null <EOL> from nova . db . sqlalchemy import api as db_session <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> INIT_VERSION = { } <EOL> INIT_VERSION [ '<STR_LIT>' ] = <NUM_LIT> <EOL> INIT_VERSION [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> _REPOSITORY = { } <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def get_engine ( database = '<STR_LIT>' ) : <EOL> if database == '<STR_LIT>' : <EOL> return db_session . get_engine ( ) <EOL> if database == '<STR_LIT>' : <EOL> return db_session . get_api_engine ( ) <EOL> def db_sync ( version = None , database = '<STR_LIT>' ) : <EOL> if version is not None : <EOL> try : <EOL> version = int ( version ) <EOL> except ValueError : <EOL> raise exception . NovaException ( _ ( "<STR_LIT>" ) ) <EOL> current_version = db_version ( database ) <EOL> repository = _find_migrate_repo ( database ) <EOL> if version is None or version > current_version : <EOL> return versioning_api . upgrade ( get_engine ( database ) , repository , <EOL> version ) <EOL> else : <EOL> return versioning_api . downgrade ( get_engine ( database ) , repository , <EOL> version ) <EOL> def db_version ( database = '<STR_LIT>' ) : <EOL> repository = _find_migrate_repo ( database ) <EOL> try : <EOL> return versioning_api . db_version ( get_engine ( database ) , repository ) <EOL> except versioning_exceptions . DatabaseNotControlledError as exc : <EOL> meta = sqlalchemy . MetaData ( ) <EOL> engine = get_engine ( database ) <EOL> meta . reflect ( bind = engine ) <EOL> tables = meta . tables <EOL> if len ( tables ) == <NUM_LIT:0> : <EOL> db_version_control ( INIT_VERSION [ database ] , database ) <EOL> return versioning_api . db_version ( get_engine ( database ) , repository ) <EOL> else : <EOL> LOG . exception ( exc ) <EOL> raise exception . NovaException ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> def db_initial_version ( database = '<STR_LIT>' ) : <EOL> return INIT_VERSION [ database ] <EOL> def _process_null_records ( table , col_name , check_fkeys , delete = False ) : <EOL> """<STR_LIT>""" <EOL> records = <NUM_LIT:0> <EOL> if col_name in table . columns : <EOL> if check_fkeys : <EOL> fkey_found = False <EOL> fkeys = table . c [ col_name ] . foreign_keys or [ ] <EOL> for fkey in fkeys : <EOL> if fkey . column . table . name == '<STR_LIT>' : <EOL> fkey_found = True <EOL> if not fkey_found : <EOL> return <NUM_LIT:0> <EOL> if delete : <EOL> records = table . delete ( ) . where ( <EOL> table . c [ col_name ] == null ( ) <EOL> ) . execute ( ) . rowcount <EOL> else : <EOL> records = len ( list ( <EOL> table . select ( ) . where ( table . c [ col_name ] == null ( ) ) . execute ( ) <EOL> ) ) <EOL> return records <EOL> def db_null_instance_uuid_scan ( delete = False ) : <EOL> """<STR_LIT>""" <EOL> engine = get_engine ( ) <EOL> meta = sqlalchemy . MetaData ( bind = engine ) <EOL> meta . reflect ( engine ) <EOL> processed = { } <EOL> for table in reversed ( meta . sorted_tables ) : <EOL> if table . name not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> processed [ table . name ] = _process_null_records ( <EOL> table , '<STR_LIT>' , check_fkeys = True , delete = delete ) <EOL> for table_name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> table = db_utils . get_table ( engine , table_name ) <EOL> processed [ table . name ] = _process_null_records ( <EOL> table , '<STR_LIT>' , check_fkeys = False , delete = delete ) <EOL> return processed <EOL> def db_version_control ( version = None , database = '<STR_LIT>' ) : <EOL> repository = _find_migrate_repo ( database ) <EOL> versioning_api . version_control ( get_engine ( database ) , repository , version ) <EOL> return version <EOL> def _find_migrate_repo ( database = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> global _REPOSITORY <EOL> rel_path = '<STR_LIT>' <EOL> if database == '<STR_LIT>' : <EOL> rel_path = os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> path = os . path . join ( os . path . abspath ( os . path . dirname ( __file__ ) ) , <EOL> rel_path ) <EOL> assert os . path . exists ( path ) <EOL> if _REPOSITORY . get ( database ) is None : <EOL> _REPOSITORY [ database ] = Repository ( path ) <EOL> return _REPOSITORY [ database ] </s>
