U of7@sddlmZddlZddlmZddlmZddlmZddlmZddlmZddlm Z dd l m Z dd l mZdd l mZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddl!m"Z"ddl#m$Z$ddl$m%Z%ddl&m'Z'erdd lm(Z(dd!l)m*Z*dd"l)m+Z+dd#l,m-Z-dd$l.m/Z/dd%l.m0Z0dd&l1m2Z2dd'l3m4Z4dd(lm5Z5Gd)d*d*e"Z6Gd+d,d,eeZ7Gd-d.d.eeZ8e'e7d/d,d0d1d2d3d4Z9e'e8d/d.d0d1d2d5d6Z:e'ed/d7d8d1d2d9d:Z;d8d;d1d<d=d>Ze'ed/dEd8d1d2dFdGZ?e'ed/dHd8d1d2dIdJZ@e'e d/dKd8d1d2dLdMZAdS)N) annotationsN)Any)Dict)List)Optional) TYPE_CHECKING)Union)types)Column) CreateIndex) Executable) ClauseElement) AddColumn) alter_column) alter_table) ColumnDefault) ColumnName)ColumnNullable) ColumnType)format_column_name)format_server_default)format_table_name) format_type) RenameTable) DefaultImpl)util) sqla_compat)compiles)Literal) MSDDLCompiler) MSSQLCompiler) CursorResult)Index)Table) TableClause) TypeEngine)_ServerDefaultcseZdZdZdZdZejddhfZejdZddfd d Z d d d fdd Z ddddZ ddfdd Z d4ddddddddddd dd fdd Z dd dd d!d"Zd#d$d dd%fd&d' Zd5dd(ddd)fd*d+ Zd,d-Zfd.d/Zd0dd0d1d2d3ZZS)6 MSSQLImplmssqlTZGOZVARCHARZNVARCHAR)ZminvalueZmaxvalueZ nominvalueZ nomaxvaluecyclecacheNone)returncs$tj|||jd|j|_dS)NZmssql_batch_separator)super__init__Z context_optsgetbatch_separator)selfargkw __class__?/opt/hc_python/lib/python3.8/site-packages/alembic/ddl/mssql.pyr0Bs zMSSQLImpl.__init__rzOptional[CursorResult]) constructr.cs0tj|f||}|jr,|jr,||j|SN)r/_execas_sqlr2 static_output)r3r:argsr5resultr6r8r9r<Hs  zMSSQLImpl._execcCs|d|jdS)NzBEGIN TRANSACTION)r>Zcommand_terminatorr3r8r8r9 emit_beginNszMSSQLImpl.emit_begincs&t|jr"|jr"||jdSr;)r/ emit_commitr=r2r>rAr6r8r9rCQs  zMSSQLImpl.emit_commitNFstrzOptional[bool]z/Optional[Union[_ServerDefault, Literal[False]]] Optional[str]zOptional[TypeEngine]zOptional[_ServerDefault]) table_name column_namenullableserver_defaultnametype_schema existing_typeexisting_server_defaultexisting_nullabler5r.c  s(|dk r.|dk r|}d}q^|dkr^tdn0| dk rL|dk rL| }|}d}n|dk r^tdd} t|| szt|| rd} || d<| | d<tj||f||||| d| |dk r| dkr| dk s|dkr|t ||d||dk rtj||||d |dk r$tj||||d dS) NzhMS-SQL ALTER COLUMN operations with NULL or NOT NULL require the existing_type or a new type_ be passed.zMS-SQL ALTER COLUMN operations that specify type_= should also specify a nullable= or existing_nullable= argument to avoid implicit conversion of NOT NULL columns to NULL.FTrIrN)rHrKrLrMrOsys.default_constraints)rLrI)rLrJ) r CommandErrorwarnrZ_server_default_is_identityZ_server_default_is_computedr/rr<_ExecDropConstraint) r3rFrGrHrIrJrKrLrMrNrOr5Z used_defaultr6r8r9rVs   zMSSQLImpl.alter_columnr$)indexr5r.cKs`|jddpd}|jdk s t|D]$}||jjkr$|jt|tjq$| t |f|dS)N mssql_includer8) kwargsr1tableAssertionErrorc append_columnr sqltypesZNullTyper<r )r3rTr5rUcolr8r8r9 create_indexs  zMSSQLImpl.create_indexzUnion[TableClause, Table]z List[dict])rWrowsr5r.c s`|jrJ|d|jj|tj||f||d|jj|ntj||f|dS)NzSET IDENTITY_INSERT %s ONzSET IDENTITY_INSERT %s OFF)r=r<dialectZidentifier_preparerZ format_tabler/ bulk_insert)r3rWr^r5r6r8r9r`s  zMSSQLImpl.bulk_insert Column[Any])rFcolumnrLr.c s|dd}|r$|t||d||dd}|rH|t||d||dd}|rj|t|||tj||fd|i|dS)NZmssql_drop_defaultFrPZmssql_drop_checkzsys.check_constraintsZmssql_drop_foreign_keyrL)popr<rS_ExecDropFKConstraintr/ drop_column)r3rFrbrLr5Z drop_defaultZ drop_checkZdrop_fksr6r8r9res0   zMSSQLImpl.drop_columncCs4|dk rtdd|}|dk r,tdd|}||kS)Nz [\(\) \"\'])resub)r3Zinspector_columnZmetadata_columnZrendered_metadata_defaultZrendered_inspector_defaultr8r8r9compare_server_defaultsz MSSQLImpl.compare_server_defaultcsHt||\}}}|dkr>|dk r>|s>|jdk r>|jjr>d}|||fS)NF)r/_compare_identity_defaultrbZ primary_key)r3Zmetadata_identityZinspector_identitydiffZignoredZis_alterr6r8r9rjs   z#MSSQLImpl._compare_identity_defaultzDict[str, Any])reflected_objectkindr.cCs@|di}|ds&|dd|ds<|dd|S)NZdialect_optionsrUZmssql_clustered)r1copyrc)r3rlrmoptionsr8r8r9 adjust_reflected_dialect_options s     z*MSSQLImpl.adjust_reflected_dialect_options)NFNNNNNN)N)__name__ __module__ __qualname__Z __dialect__Ztransactional_ddlr2rZ type_synonymsZidentity_attrs_ignorer0r<rBrCrr]r`rerirjrp __classcell__r8r8r6r9r)3s2  *X  r)c@s&eZdZdZddddddddZd S) rSFrDzUnion[Column[Any], str]rEr-)tnamecolnamerKrLr.cCs||_||_||_||_dSr;)rurvrKrL)r3rurvrKrLr8r8r9r0sz_ExecDropConstraint.__init__NrqrrrsZ inherit_cacher0r8r8r8r9rSsrSc@s$eZdZdZdddddddZd S) rdFrDrarEr-)rurvrLr.cCs||_||_||_dSr;)rurvrL)r3rurvrLr8r8r9r0*sz_ExecDropFKConstraint.__init__Nrwr8r8r8r9rd'srdr*r"rD)elementcompilerr.cKsD|j|j|j|jf\}}}}d|||t||||r:|dnddS)Nadeclare @const_name varchar(256) select @const_name = QUOTENAME([name]) from %(type)s where parent_object_id = object_id('%(schema_dot)s%(tname)s') and col_name(parent_object_id, parent_column_id) = '%(colname)s' exec('alter table %(tname_quoted)s drop constraint ' + @const_name).rf)typerurv tname_quoted schema_dot)rLrurvrKr)rxryr5rLrurvrKr8r8r9_exec_drop_col_constraint2s  r~cKs<|j|j|j}}}d||t||||r2|dnddS)Naydeclare @const_name varchar(256) select @const_name = QUOTENAME([name]) from sys.foreign_keys fk join sys.foreign_key_columns fkc on fk.object_id=fkc.constraint_object_id where fkc.parent_object_id = object_id('%(schema_dot)s%(tname)s') and col_name(fkc.parent_object_id, fkc.parent_column_id) = '%(colname)s' exec('alter table %(tname_quoted)s drop constraint ' + @const_name)rzrf)rurvr|r})rLrurvr)rxryr5rLrurvr8r8r9_exec_drop_col_fk_constraintKs rrr!cKs$dt||j|jt||jf|fS)Nz%s %s)rrFrLmssql_add_columnrbrxryr5r8r8r9visit_add_column_srra)ryrbr.cKsd|j|f|S)NzADD %s)Zget_column_specification)ryrbr5r8r8r9rgsrrcKs6dt||j|jt||jt||j|jr.dndfS)Nz %s %s %s %sZNULLzNOT NULL)rrFrLrrGrrMrHrr8r8r9visit_column_nullablems    rrcKs*dt||j|jt||jt||jfS)Nz%s ADD DEFAULT %s FOR %s)rrFrLrdefaultrrGrr8r8r9visit_column_defaultys   rrcKs*dt||j|jt||jt||jfS)Nz$EXEC sp_rename '%s.%s', %s, 'COLUMN')rrFrLrrGZnewnamerr8r8r9visit_rename_columns   rrcKs*dt||j|jt||jt||jfS)Nz%s %s %s)rrFrLrrGrrKrr8r8r9visit_column_types   rrcKs"dt||j|jt||jdfS)NzEXEC sp_rename '%s', %s)rrFrLZnew_table_namerr8r8r9visit_rename_tables r)B __future__rrgtypingrrrrrrZ sqlalchemyr r[Zsqlalchemy.schemar r Zsqlalchemy.sql.baser Zsqlalchemy.sql.elementsr baserrrrrrrrrrrrimplrrfrrZutil.sqla_compatrr Zsqlalchemy.dialects.mssql.baser!r"Zsqlalchemy.engine.cursorr#Zsqlalchemy.sql.schemar$r%Zsqlalchemy.sql.selectabler&Zsqlalchemy.sql.type_apir'r(r)rSrdr~rrrrrrrrr8r8r8r9sv                                     e