<s> from migrate import UniqueConstraint <EOL> from oslo_log import log as logging <EOL> from sqlalchemy import MetaData <EOL> from sqlalchemy . sql import null <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> LOG = logging . getLogger ( __name__ ) <EOL> UC_NAME = '<STR_LIT>' <EOL> def scan_for_null_records ( table , col_name , check_fkeys ) : <EOL> """<STR_LIT>""" <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 <EOL> records = len ( list ( <EOL> table . select ( ) . where ( table . c [ col_name ] == null ( ) ) . execute ( ) <EOL> ) ) <EOL> if records : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % ( <EOL> { '<STR_LIT>' : records , '<STR_LIT>' : table . name } ) <EOL> raise exception . ValidationError ( detail = msg ) <EOL> def process_null_records ( meta , scan = True ) : <EOL> """<STR_LIT>""" <EOL> if scan : <EOL> for table in reversed ( meta . sorted_tables ) : <EOL> if table . name not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> scan_for_null_records ( table , '<STR_LIT>' , check_fkeys = True ) <EOL> for table_name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> table = meta . tables [ table_name ] <EOL> if scan : <EOL> scan_for_null_records ( table , '<STR_LIT>' , check_fkeys = False ) <EOL> else : <EOL> table . columns . uuid . alter ( nullable = False ) <EOL> def upgrade ( migrate_engine ) : <EOL> meta = MetaData ( migrate_engine ) <EOL> meta . reflect ( migrate_engine ) <EOL> process_null_records ( meta , scan = True ) <EOL> process_null_records ( meta , scan = False ) <EOL> instances = meta . tables [ '<STR_LIT>' ] <EOL> UniqueConstraint ( '<STR_LIT>' , table = instances , name = UC_NAME ) . create ( ) </s>
