<s> """<STR_LIT>""" <EOL> from . . import util <EOL> from . elements import ClauseElement <EOL> from . base import Executable , _generative , SchemaVisitor , _bind_or_error <EOL> from . . util import topological <EOL> from . . import event <EOL> from . . import exc <EOL> class _DDLCompiles ( ClauseElement ) : <EOL> def _compiler ( self , dialect , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return dialect . ddl_compiler ( dialect , self , ** kw ) <EOL> class DDLElement ( Executable , _DDLCompiles ) : <EOL> """<STR_LIT>""" <EOL> _execution_options = Executable . _execution_options . union ( { '<STR_LIT>' : True } ) <EOL> target = None <EOL> on = None <EOL> dialect = None <EOL> callable_ = None <EOL> def _execute_on_connection ( self , connection , multiparams , params ) : <EOL> return connection . _execute_ddl ( self , multiparams , params ) <EOL> def execute ( self , bind = None , target = None ) : <EOL> """<STR_LIT>""" <EOL> if bind is None : <EOL> bind = _bind_or_error ( self ) <EOL> if self . _should_execute ( target , bind ) : <EOL> return bind . execute ( self . against ( target ) ) <EOL> else : <EOL> bind . engine . logger . info ( <EOL> "<STR_LIT>" ) <EOL> @ util . deprecated ( "<STR_LIT>" , "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def execute_at ( self , event_name , target ) : <EOL> """<STR_LIT>""" <EOL> def call_event ( target , connection , ** kw ) : <EOL> if self . _should_execute_deprecated ( event_name , <EOL> target , connection , ** kw ) : <EOL> return connection . execute ( self . against ( target ) ) <EOL> event . listen ( target , "<STR_LIT>" + event_name . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) , call_event ) <EOL> @ _generative <EOL> def against ( self , target ) : <EOL> """<STR_LIT>""" <EOL> self . target = target <EOL> @ _generative <EOL> def execute_if ( self , dialect = None , callable_ = None , state = None ) : <EOL> """<STR_LIT>""" <EOL> self . dialect = dialect <EOL> self . callable_ = callable_ <EOL> self . state = state <EOL> def _should_execute ( self , target , bind , ** kw ) : <EOL> if self . on is not None and not self . _should_execute_deprecated ( None , target , bind , ** kw ) : <EOL> return False <EOL> if isinstance ( self . dialect , util . string_types ) : <EOL> if self . dialect != bind . engine . name : <EOL> return False <EOL> elif isinstance ( self . dialect , ( tuple , list , set ) ) : <EOL> if bind . engine . name not in self . dialect : <EOL> return False <EOL> if ( self . callable_ is not None and <EOL> not self . callable_ ( self , target , bind , <EOL> state = self . state , ** kw ) ) : <EOL> return False <EOL> return True <EOL> def _should_execute_deprecated ( self , event , target , bind , ** kw ) : <EOL> if self . on is None : <EOL> return True <EOL> elif isinstance ( self . on , util . string_types ) : <EOL> return self . on == bind . engine . name <EOL> elif isinstance ( self . on , ( tuple , list , set ) ) : <EOL> return bind . engine . name in self . on <EOL> else : <EOL> return self . on ( self , event , target , bind , ** kw ) <EOL> def __call__ ( self , target , bind , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if self . _should_execute ( target , bind , ** kw ) : <EOL> return bind . execute ( self . against ( target ) ) <EOL> def _check_ddl_on ( self , on ) : <EOL> if ( on is not None and <EOL> ( not isinstance ( on , util . string_types + ( tuple , list , set ) ) and <EOL> not util . callable ( on ) ) ) : <EOL> raise exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % type ( on ) . __name__ ) <EOL> def bind ( self ) : <EOL> if self . _bind : <EOL> return self . _bind <EOL> def _set_bind ( self , bind ) : <EOL> self . _bind = bind <EOL> bind = property ( bind , _set_bind ) <EOL> def _generate ( self ) : <EOL> s = self . __class__ . __new__ ( self . __class__ ) <EOL> s . __dict__ = self . __dict__ . copy ( ) <EOL> return s <EOL> class DDL ( DDLElement ) : <EOL> """<STR_LIT>""" <EOL> __visit_name__ = "<STR_LIT>" <EOL> def __init__ ( self , statement , on = None , context = None , bind = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( statement , util . string_types ) : <EOL> raise exc . ArgumentError ( <EOL> "<STR_LIT>" % <EOL> statement ) <EOL> self . statement = statement <EOL> self . context = context or { } <EOL> self . _check_ddl_on ( on ) <EOL> self . on = on <EOL> self . _bind = bind <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> type ( self ) . __name__ , id ( self ) , <EOL> '<STR_LIT:U+002CU+0020>' . join ( [ repr ( self . statement ) ] + <EOL> [ '<STR_LIT>' % ( key , getattr ( self , key ) ) <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if getattr ( self , key ) ] ) ) <EOL> class _CreateDropBase ( DDLElement ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , element , on = None , bind = None ) : <EOL> self . element = element <EOL> self . _check_ddl_on ( on ) <EOL> self . on = on <EOL> self . bind = bind <EOL> def _create_rule_disable ( self , compiler ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> class CreateSchema ( _CreateDropBase ) : <EOL> """<STR_LIT>""" <EOL> __visit_name__ = "<STR_LIT>" <EOL> def __init__ ( self , name , quote = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> self . quote = quote <EOL> super ( CreateSchema , self ) . __init__ ( name , ** kw ) <EOL> class DropSchema ( _CreateDropBase ) : <EOL> """<STR_LIT>""" <EOL> __visit_name__ = "<STR_LIT>" <EOL> def __init__ ( self , name , quote = None , cascade = False , ** kw ) : <EOL> """<STR_LIT>""" <EOL> self . quote = quote <EOL> self . cascade = cascade <EOL> super ( DropSchema , self ) . __init__ ( name , ** kw ) <EOL> class CreateTable ( _CreateDropBase ) : <EOL> """<STR_LIT>""" <EOL> __visit_name__ = "<STR_LIT>" <EOL> def __init__ ( <EOL> self , element , on = None , bind = None , <EOL> include_foreign_key_constraints = None ) : <EOL> """<STR_LIT>""" <EOL> super ( CreateTable , self ) . __init__ ( element , on = on , bind = bind ) <EOL> self . columns = [ CreateColumn ( column ) <EOL> for column in element . columns <EOL> ] <EOL> self . include_foreign_key_constraints = include_foreign_key_constraints <EOL> class _DropView ( _CreateDropBase ) : <EOL> """<STR_LIT>""" <EOL> __visit_name__ = "<STR_LIT>" <EOL> class CreateColumn ( _DDLCompiles ) : <EOL> """<STR_LIT>""" <EOL> __visit_name__ = '<STR_LIT>' <EOL> def __init__ ( self , element ) : <EOL> self . element = element <EOL> class DropTable ( _CreateDropBase ) : <EOL> """<STR_LIT>""" <EOL> __visit_name__ = "<STR_LIT>" <EOL> class CreateSequence ( _CreateDropBase ) : <EOL> """<STR_LIT>""" <EOL> __visit_name__ = "<STR_LIT>" <EOL> class DropSequence ( _CreateDropBase ) : <EOL> """<STR_LIT>""" <EOL> __visit_name__ = "<STR_LIT>" <EOL> class CreateIndex ( _CreateDropBase ) : <EOL> """<STR_LIT>""" <EOL> __visit_name__ = "<STR_LIT>" <EOL> class DropIndex ( _CreateDropBase ) : <EOL> """<STR_LIT>""" <EOL> __visit_name__ = "<STR_LIT>" <EOL> class AddConstraint ( _CreateDropBase ) : <EOL> """<STR_LIT>""" <EOL> __visit_name__ = "<STR_LIT>" <EOL> def __init__ ( self , element , * args , ** kw ) : <EOL> super ( AddConstraint , self ) . __init__ ( element , * args , ** kw ) <EOL> element . _create_rule = util . portable_instancemethod ( <EOL> self . _create_rule_disable ) <EOL> class DropConstraint ( _CreateDropBase ) : <EOL> """<STR_LIT>""" <EOL> __visit_name__ = "<STR_LIT>" <EOL> def __init__ ( self , element , cascade = False , ** kw ) : <EOL> self . cascade = cascade <EOL> super ( DropConstraint , self ) . __init__ ( element , ** kw ) <EOL> element . _create_rule = util . portable_instancemethod ( <EOL> self . _create_rule_disable ) <EOL> class DDLBase ( SchemaVisitor ) : <EOL> def __init__ ( self , connection ) : <EOL> self . connection = connection <EOL> class SchemaGenerator ( DDLBase ) : <EOL> def __init__ ( self , dialect , connection , checkfirst = False , <EOL> tables = None , ** kwargs ) : <EOL> super ( SchemaGenerator , self ) . __init__ ( connection , ** kwargs ) <EOL> self . checkfirst = checkfirst <EOL> self . tables = tables <EOL> self . preparer = dialect . identifier_preparer <EOL> self . dialect = dialect <EOL> self . memo = { } <EOL> def _can_create_table ( self , table ) : <EOL> self . dialect . validate_identifier ( table . name ) <EOL> effective_schema = self . connection . schema_for_object ( table ) <EOL> if effective_schema : <EOL> self . dialect . validate_identifier ( effective_schema ) <EOL> return not self . checkfirst or not self . dialect . has_table ( self . connection , <EOL> table . name , schema = effective_schema ) <EOL> def _can_create_sequence ( self , sequence ) : <EOL> effective_schema = self . connection . schema_for_object ( sequence ) <EOL> return self . dialect . supports_sequences and ( <EOL> ( not self . dialect . sequences_optional or <EOL> not sequence . optional ) and <EOL> ( <EOL> not self . checkfirst or <EOL> not self . dialect . has_sequence ( <EOL> self . connection , <EOL> sequence . name , <EOL> schema = effective_schema ) <EOL> ) <EOL> ) <EOL> def visit_metadata ( self , metadata ) : <EOL> if self . tables is not None : <EOL> tables = self . tables <EOL> else : <EOL> tables = list ( metadata . tables . values ( ) ) <EOL> collection = sort_tables_and_constraints ( <EOL> [ t for t in tables if self . _can_create_table ( t ) ] ) <EOL> seq_coll = [ s for s in metadata . _sequences . values ( ) <EOL> if s . column is None and self . _can_create_sequence ( s ) ] <EOL> event_collection = [ <EOL> t for ( t , fks ) in collection if t is not None <EOL> ] <EOL> metadata . dispatch . before_create ( metadata , self . connection , <EOL> tables = event_collection , <EOL> checkfirst = self . checkfirst , <EOL> _ddl_runner = self ) <EOL> for seq in seq_coll : <EOL> self . traverse_single ( seq , create_ok = True ) <EOL> for table , fkcs in collection : <EOL> if table is not None : <EOL> self . traverse_single ( <EOL> table , create_ok = True , <EOL> include_foreign_key_constraints = fkcs , <EOL> _is_metadata_operation = True ) <EOL> else : <EOL> for fkc in fkcs : <EOL> self . traverse_single ( fkc ) <EOL> metadata . dispatch . after_create ( metadata , self . connection , <EOL> tables = event_collection , <EOL> checkfirst = self . checkfirst , <EOL> _ddl_runner = self ) <EOL> def visit_table ( <EOL> self , table , create_ok = False , <EOL> include_foreign_key_constraints = None , <EOL> _is_metadata_operation = False ) : <EOL> if not create_ok and not self . _can_create_table ( table ) : <EOL> return <EOL> table . dispatch . before_create ( <EOL> table , self . connection , <EOL> checkfirst = self . checkfirst , <EOL> _ddl_runner = self , <EOL> _is_metadata_operation = _is_metadata_operation ) <EOL> for column in table . columns : <EOL> if column . default is not None : <EOL> self . traverse_single ( column . default ) <EOL> if not self . dialect . supports_alter : <EOL> include_foreign_key_constraints = None <EOL> self . connection . execute ( <EOL> CreateTable ( <EOL> table , <EOL> include_foreign_key_constraints = include_foreign_key_constraints <EOL> ) ) <EOL> if hasattr ( table , '<STR_LIT>' ) : <EOL> for index in table . indexes : <EOL> self . traverse_single ( index ) <EOL> table . dispatch . after_create ( <EOL> table , self . connection , <EOL> checkfirst = self . checkfirst , <EOL> _ddl_runner = self , <EOL> _is_metadata_operation = _is_metadata_operation ) <EOL> def visit_foreign_key_constraint ( self , constraint ) : <EOL> if not self . dialect . supports_alter : <EOL> return <EOL> self . connection . execute ( AddConstraint ( constraint ) ) <EOL> def visit_sequence ( self , sequence , create_ok = False ) : <EOL> if not create_ok and not self . _can_create_sequence ( sequence ) : <EOL> return <EOL> self . connection . execute ( CreateSequence ( sequence ) ) <EOL> def visit_index ( self , index ) : <EOL> self . connection . execute ( CreateIndex ( index ) ) <EOL> class SchemaDropper ( DDLBase ) : <EOL> def __init__ ( self , dialect , connection , checkfirst = False , <EOL> tables = None , ** kwargs ) : <EOL> super ( SchemaDropper , self ) . __init__ ( connection , ** kwargs ) <EOL> self . checkfirst = checkfirst <EOL> self . tables = tables <EOL> self . preparer = dialect . identifier_preparer <EOL> self . dialect = dialect <EOL> self . memo = { } <EOL> def visit_metadata ( self , metadata ) : <EOL> if self . tables is not None : <EOL> tables = self . tables <EOL> else : <EOL> tables = list ( metadata . tables . values ( ) ) <EOL> try : <EOL> unsorted_tables = [ t for t in tables if self . _can_drop_table ( t ) ] <EOL> collection = list ( reversed ( <EOL> sort_tables_and_constraints ( <EOL> unsorted_tables , <EOL> filter_fn = lambda constraint : False <EOL> if not self . dialect . supports_alter <EOL> or constraint . name is None <EOL> else None <EOL> ) <EOL> ) ) <EOL> except exc . CircularDependencyError as err2 : <EOL> if not self . dialect . supports_alter : <EOL> util . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( <EOL> "<STR_LIT:U+002CU+0020>" . join ( sorted ( [ t . fullname for t in err2 . cycles ] ) ) <EOL> ) <EOL> ) <EOL> collection = [ ( t , ( ) ) for t in unsorted_tables ] <EOL> else : <EOL> util . raise_from_cause ( <EOL> exc . CircularDependencyError ( <EOL> err2 . args [ <NUM_LIT:0> ] , <EOL> err2 . cycles , err2 . edges , <EOL> msg = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( <EOL> "<STR_LIT:U+002CU+0020>" . join ( sorted ( [ t . fullname for t in err2 . cycles ] ) ) <EOL> ) <EOL> ) <EOL> ) <EOL> seq_coll = [ <EOL> s <EOL> for s in metadata . _sequences . values ( ) <EOL> if s . column is None and self . _can_drop_sequence ( s ) <EOL> ] <EOL> event_collection = [ <EOL> t for ( t , fks ) in collection if t is not None <EOL> ] <EOL> metadata . dispatch . before_drop ( <EOL> metadata , self . connection , tables = event_collection , <EOL> checkfirst = self . checkfirst , _ddl_runner = self ) <EOL> for table , fkcs in collection : <EOL> if table is not None : <EOL> self . traverse_single ( <EOL> table , drop_ok = True , _is_metadata_operation = True ) <EOL> else : <EOL> for fkc in fkcs : <EOL> self . traverse_single ( fkc ) <EOL> for seq in seq_coll : <EOL> self . traverse_single ( seq , drop_ok = True ) <EOL> metadata . dispatch . after_drop ( <EOL> metadata , self . connection , tables = event_collection , <EOL> checkfirst = self . checkfirst , _ddl_runner = self ) <EOL> def _can_drop_table ( self , table ) : <EOL> self . dialect . validate_identifier ( table . name ) <EOL> effective_schema = self . connection . schema_for_object ( table ) <EOL> if effective_schema : <EOL> self . dialect . validate_identifier ( effective_schema ) <EOL> return not self . checkfirst or self . dialect . has_table ( <EOL> self . connection , table . name , schema = effective_schema ) <EOL> def _can_drop_sequence ( self , sequence ) : <EOL> effective_schema = self . connection . schema_for_object ( sequence ) <EOL> return self . dialect . supports_sequences and ( ( not self . dialect . sequences_optional or <EOL> not sequence . optional ) and <EOL> ( not self . checkfirst or <EOL> self . dialect . has_sequence ( <EOL> self . connection , <EOL> sequence . name , <EOL> schema = effective_schema ) ) <EOL> ) <EOL> def visit_index ( self , index ) : <EOL> self . connection . execute ( DropIndex ( index ) ) <EOL> def visit_table ( self , table , drop_ok = False , _is_metadata_operation = False ) : <EOL> if not drop_ok and not self . _can_drop_table ( table ) : <EOL> return <EOL> table . dispatch . before_drop ( <EOL> table , self . connection , <EOL> checkfirst = self . checkfirst , <EOL> _ddl_runner = self , <EOL> _is_metadata_operation = _is_metadata_operation ) <EOL> for column in table . columns : <EOL> if column . default is not None : <EOL> self . traverse_single ( column . default ) <EOL> self . connection . execute ( DropTable ( table ) ) <EOL> table . dispatch . after_drop ( <EOL> table , self . connection , <EOL> checkfirst = self . checkfirst , <EOL> _ddl_runner = self , <EOL> _is_metadata_operation = _is_metadata_operation ) <EOL> def visit_foreign_key_constraint ( self , constraint ) : <EOL> if not self . dialect . supports_alter : <EOL> return <EOL> self . connection . execute ( DropConstraint ( constraint ) ) <EOL> def visit_sequence ( self , sequence , drop_ok = False ) : <EOL> if not drop_ok and not self . _can_drop_sequence ( sequence ) : <EOL> return <EOL> self . connection . execute ( DropSequence ( sequence ) ) <EOL> def sort_tables ( tables , skip_fn = None , extra_dependencies = None ) : <EOL> """<STR_LIT>""" <EOL> if skip_fn is not None : <EOL> def _skip_fn ( fkc ) : <EOL> for fk in fkc . elements : <EOL> if skip_fn ( fk ) : <EOL> return True <EOL> else : <EOL> return None <EOL> else : <EOL> _skip_fn = None <EOL> return [ <EOL> t for ( t , fkcs ) in <EOL> sort_tables_and_constraints ( <EOL> tables , filter_fn = _skip_fn , extra_dependencies = extra_dependencies ) <EOL> if t is not None <EOL> ] <EOL> def sort_tables_and_constraints ( <EOL> tables , filter_fn = None , extra_dependencies = None ) : <EOL> """<STR_LIT>""" <EOL> fixed_dependencies = set ( ) <EOL> mutable_dependencies = set ( ) <EOL> if extra_dependencies is not None : <EOL> fixed_dependencies . update ( extra_dependencies ) <EOL> remaining_fkcs = set ( ) <EOL> for table in tables : <EOL> for fkc in table . foreign_key_constraints : <EOL> if fkc . use_alter is True : <EOL> remaining_fkcs . add ( fkc ) <EOL> continue <EOL> if filter_fn : <EOL> filtered = filter_fn ( fkc ) <EOL> if filtered is True : <EOL> remaining_fkcs . add ( fkc ) <EOL> continue <EOL> dependent_on = fkc . referred_table <EOL> if dependent_on is not table : <EOL> mutable_dependencies . add ( ( dependent_on , table ) ) <EOL> fixed_dependencies . update ( <EOL> ( parent , table ) for parent in table . _extra_dependencies <EOL> ) <EOL> try : <EOL> candidate_sort = list ( <EOL> topological . sort ( <EOL> fixed_dependencies . union ( mutable_dependencies ) , tables , <EOL> deterministic_order = True <EOL> ) <EOL> ) <EOL> except exc . CircularDependencyError as err : <EOL> for edge in err . edges : <EOL> if edge in mutable_dependencies : <EOL> table = edge [ <NUM_LIT:1> ] <EOL> can_remove = [ <EOL> fkc for fkc in table . foreign_key_constraints <EOL> if filter_fn is None or filter_fn ( fkc ) is not False ] <EOL> remaining_fkcs . update ( can_remove ) <EOL> for fkc in can_remove : <EOL> dependent_on = fkc . referred_table <EOL> if dependent_on is not table : <EOL> mutable_dependencies . discard ( ( dependent_on , table ) ) <EOL> candidate_sort = list ( <EOL> topological . sort ( <EOL> fixed_dependencies . union ( mutable_dependencies ) , tables , <EOL> deterministic_order = True <EOL> ) <EOL> ) <EOL> return [ <EOL> ( table , table . foreign_key_constraints . difference ( remaining_fkcs ) ) <EOL> for table in candidate_sort <EOL> ] + [ ( None , list ( remaining_fkcs ) ) ] </s>
