<s> import functools <EOL> from sqlalchemy . ext . compiler import compiles <EOL> from sqlalchemy . schema import DDLElement , Column <EOL> from sqlalchemy import Integer <EOL> from sqlalchemy import types as sqltypes <EOL> class AlterTable ( DDLElement ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , table_name , schema = None ) : <EOL> self . table_name = table_name <EOL> self . schema = schema <EOL> class RenameTable ( AlterTable ) : <EOL> def __init__ ( self , old_table_name , new_table_name , schema = None ) : <EOL> super ( RenameTable , self ) . __init__ ( old_table_name , schema = schema ) <EOL> self . new_table_name = new_table_name <EOL> class AlterColumn ( AlterTable ) : <EOL> def __init__ ( self , name , column_name , schema = None , <EOL> existing_type = None , <EOL> existing_nullable = None , <EOL> existing_server_default = None ) : <EOL> super ( AlterColumn , self ) . __init__ ( name , schema = schema ) <EOL> self . column_name = column_name <EOL> self . existing_type = sqltypes . to_instance ( existing_type ) if existing_type is not None else None <EOL> self . existing_nullable = existing_nullable <EOL> self . existing_server_default = existing_server_default <EOL> class ColumnNullable ( AlterColumn ) : <EOL> def __init__ ( self , name , column_name , nullable , ** kw ) : <EOL> super ( ColumnNullable , self ) . __init__ ( name , column_name , <EOL> ** kw ) <EOL> self . nullable = nullable <EOL> class ColumnType ( AlterColumn ) : <EOL> def __init__ ( self , name , column_name , type_ , ** kw ) : <EOL> super ( ColumnType , self ) . __init__ ( name , column_name , <EOL> ** kw ) <EOL> self . type_ = sqltypes . to_instance ( type_ ) <EOL> class ColumnName ( AlterColumn ) : <EOL> def __init__ ( self , name , column_name , newname , ** kw ) : <EOL> super ( ColumnName , self ) . __init__ ( name , column_name , ** kw ) <EOL> self . newname = newname <EOL> class ColumnDefault ( AlterColumn ) : <EOL> def __init__ ( self , name , column_name , default , ** kw ) : <EOL> super ( ColumnDefault , self ) . __init__ ( name , column_name , ** kw ) <EOL> self . default = default <EOL> class AddColumn ( AlterTable ) : <EOL> def __init__ ( self , name , column , schema = None ) : <EOL> super ( AddColumn , self ) . __init__ ( name , schema = schema ) <EOL> self . column = column <EOL> class DropColumn ( AlterTable ) : <EOL> def __init__ ( self , name , column , schema = None ) : <EOL> super ( DropColumn , self ) . __init__ ( name , schema = schema ) <EOL> self . column = column <EOL> @ compiles ( RenameTable ) <EOL> def visit_rename_table ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" % ( <EOL> alter_table ( compiler , element . table_name , element . schema ) , <EOL> format_table_name ( compiler , element . new_table_name , element . schema ) <EOL> ) <EOL> @ compiles ( AddColumn ) <EOL> def visit_add_column ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" % ( <EOL> alter_table ( compiler , element . table_name , element . schema ) , <EOL> add_column ( compiler , element . column , ** kw ) <EOL> ) <EOL> @ compiles ( DropColumn ) <EOL> def visit_drop_column ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" % ( <EOL> alter_table ( compiler , element . table_name , element . schema ) , <EOL> drop_column ( compiler , element . column . name , ** kw ) <EOL> ) <EOL> @ compiles ( ColumnNullable ) <EOL> def visit_column_nullable ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" % ( <EOL> alter_table ( compiler , element . table_name , element . schema ) , <EOL> alter_column ( compiler , element . column_name ) , <EOL> "<STR_LIT>" if element . nullable else "<STR_LIT>" <EOL> ) <EOL> @ compiles ( ColumnType ) <EOL> def visit_column_type ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" % ( <EOL> alter_table ( compiler , element . table_name , element . schema ) , <EOL> alter_column ( compiler , element . column_name ) , <EOL> "<STR_LIT>" % format_type ( compiler , element . type_ ) <EOL> ) <EOL> @ compiles ( ColumnName ) <EOL> def visit_column_name ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" % ( <EOL> alter_table ( compiler , element . table_name , element . schema ) , <EOL> format_column_name ( compiler , element . column_name ) , <EOL> format_column_name ( compiler , element . newname ) <EOL> ) <EOL> @ compiles ( ColumnDefault ) <EOL> def visit_column_default ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" % ( <EOL> alter_table ( compiler , element . table_name , element . schema ) , <EOL> alter_column ( compiler , element . column_name ) , <EOL> "<STR_LIT>" % <EOL> format_server_default ( compiler , element . default ) <EOL> if element . default is not None <EOL> else "<STR_LIT>" <EOL> ) <EOL> def quote_dotted ( name , quote ) : <EOL> """<STR_LIT>""" <EOL> result = '<STR_LIT:.>' . join ( [ quote ( x ) for x in name . split ( '<STR_LIT:.>' ) ] ) <EOL> return result <EOL> def format_table_name ( compiler , name , schema ) : <EOL> quote = functools . partial ( compiler . preparer . quote , force = None ) <EOL> if schema : <EOL> return quote_dotted ( schema , quote ) + "<STR_LIT:.>" + quote ( name ) <EOL> else : <EOL> return quote ( name ) <EOL> def format_column_name ( compiler , name ) : <EOL> return compiler . preparer . quote ( name , None ) <EOL> def format_server_default ( compiler , default ) : <EOL> return compiler . get_column_default_string ( <EOL> Column ( "<STR_LIT:x>" , Integer , server_default = default ) <EOL> ) <EOL> def format_type ( compiler , type_ ) : <EOL> return compiler . dialect . type_compiler . process ( type_ ) <EOL> def alter_table ( compiler , name , schema ) : <EOL> return "<STR_LIT>" % format_table_name ( compiler , name , schema ) <EOL> def drop_column ( compiler , name ) : <EOL> return '<STR_LIT>' % format_column_name ( compiler , name ) <EOL> def alter_column ( compiler , name ) : <EOL> return '<STR_LIT>' % format_column_name ( compiler , name ) <EOL> def add_column ( compiler , column , ** kw ) : <EOL> return "<STR_LIT>" % compiler . get_column_specification ( column , ** kw ) </s>
