@@ -133,13 +133,17 @@ public string GetComparisonDDLStatements(params Type[] entityTypes) {
133133 if ( tbcol . Attribute . IsIdentity == true && tbcol . Attribute . DbType . IndexOf ( "AUTO_INCREMENT" , StringComparison . CurrentCultureIgnoreCase ) == - 1 ) sb . Append ( " AUTO_INCREMENT" ) ;
134134 sb . Append ( "," ) ;
135135 }
136- if ( tb . Primarys . Any ( ) == false )
137- sb . Remove ( sb . Length - 1 , 1 ) ;
138- else {
136+ if ( tb . Primarys . Any ( ) ) {
139137 sb . Append ( " \r \n PRIMARY KEY (" ) ;
140138 foreach ( var tbcol in tb . Primarys ) sb . Append ( _commonUtils . QuoteSqlName ( tbcol . Attribute . Name ) ) . Append ( ", " ) ;
141- sb . Remove ( sb . Length - 2 , 2 ) . Append ( ")" ) ;
139+ sb . Remove ( sb . Length - 2 , 2 ) . Append ( "), " ) ;
142140 }
141+ foreach ( var uk in tb . Uniques ) {
142+ sb . Append ( " \r \n UNIQUE KEY " ) . Append ( _commonUtils . QuoteSqlName ( uk . Key ) ) . Append ( "(" ) ;
143+ foreach ( var tbcol in uk . Value ) sb . Append ( _commonUtils . QuoteSqlName ( tbcol . Attribute . Name ) ) . Append ( ", " ) ;
144+ sb . Remove ( sb . Length - 2 , 2 ) . Append ( ")," ) ;
145+ }
146+ sb . Remove ( sb . Length - 1 , 1 ) ;
143147 sb . Append ( "\r \n ) Engine=InnoDB CHARACTER SET utf8;\r \n " ) ;
144148 continue ;
145149 }
@@ -176,7 +180,7 @@ from information_schema.columns a
176180 } , StringComparer . CurrentCultureIgnoreCase ) ;
177181
178182 if ( istmpatler == false ) {
179- var existsPrimary = ExecuteScalar ( tbname [ 0 ] , "select 1 from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where table_schema={0} and table_name={1} limit 1" . FormatMySql ( tbname ) ) ;
183+ var existsPrimary = ExecuteScalar ( tbname [ 0 ] , "select 1 from information_schema.key_column_usage where table_schema={0} and table_name={1} and constraint_name = 'PRIMARY' limit 1" . FormatMySql ( tbname ) ) ;
180184 foreach ( var tbcol in tb . Columns . Values ) {
181185 var isIdentityChanged = tbcol . Attribute . IsIdentity == true && tbcol . Attribute . DbType . IndexOf ( "AUTO_INCREMENT" , StringComparison . CurrentCultureIgnoreCase ) == - 1 ;
182186 if ( tbstruct . TryGetValue ( tbcol . Attribute . Name , out var tbstructcol ) ||
@@ -202,6 +206,23 @@ from information_schema.columns a
202206 if ( isIdentityChanged ) sbalter . Append ( " AUTO_INCREMENT" ) . Append ( existsPrimary == null ? "" : ", DROP PRIMARY KEY" ) . Append ( ", ADD PRIMARY KEY(" ) . Append ( _commonUtils . QuoteSqlName ( tbcol . Attribute . Name ) ) . Append ( ")" ) ;
203207 sbalter . Append ( ";\r \n " ) ;
204208 }
209+ var dsuksql = @"
210+ select
211+ a.column_name,
212+ a.constraint_name 'index_id'
213+ from information_schema.key_column_usage a
214+ where a.constraint_schema IN ({0}) and a.table_name IN ({1})" . FormatMySql ( tboldname ?? tbname ) ;
215+ var dsuk = _orm . Ado . ExecuteArray ( CommandType . Text , dsuksql ) . Select ( a => new [ ] { string . Concat ( a [ 0 ] ) , string . Concat ( a [ 1 ] ) } ) ;
216+ foreach ( var uk in tb . Uniques ) {
217+ if ( uk . Key == "PRIMARY" || string . IsNullOrEmpty ( uk . Key ) || uk . Value . Any ( ) == false ) continue ;
218+ var dsukfind1 = dsuk . Where ( a => string . Compare ( a [ 1 ] , uk . Key , true ) == 0 ) . ToArray ( ) ;
219+ if ( dsukfind1 . Any ( ) == false || dsukfind1 . Length != uk . Value . Count || dsukfind1 . Where ( a => uk . Value . Where ( b => string . Compare ( b . Attribute . Name , a [ 0 ] , true ) == 0 ) . Any ( ) ) . Count ( ) != uk . Value . Count ) {
220+ if ( dsukfind1 . Any ( ) ) sbalter . Append ( "ALTER TABLE " ) . Append ( _commonUtils . QuoteSqlName ( $ "{ tbname [ 0 ] } .{ tbname [ 1 ] } ") ) . Append ( " DROP INDEX " ) . Append ( _commonUtils . QuoteSqlName ( uk . Key ) ) . Append ( ";\r \n " ) ;
221+ sbalter . Append ( "ALTER TABLE " ) . Append ( _commonUtils . QuoteSqlName ( $ "{ tbname [ 0 ] } .{ tbname [ 1 ] } ") ) . Append ( " ADD CONSTRAINT " ) . Append ( _commonUtils . QuoteSqlName ( uk . Key ) ) . Append ( " UNIQUE(" ) ;
222+ foreach ( var tbcol in uk . Value ) sbalter . Append ( _commonUtils . QuoteSqlName ( tbcol . Attribute . Name ) ) . Append ( ", " ) ;
223+ sbalter . Remove ( sbalter . Length - 2 , 2 ) . Append ( ");\r \n " ) ;
224+ }
225+ }
205226 }
206227 if ( istmpatler == false ) {
207228 sb . Append ( sbalter ) ;
@@ -219,13 +240,17 @@ from information_schema.columns a
219240 if ( tbcol . Attribute . IsIdentity == true && tbcol . Attribute . DbType . IndexOf ( "AUTO_INCREMENT" , StringComparison . CurrentCultureIgnoreCase ) == - 1 ) sb . Append ( " AUTO_INCREMENT" ) ;
220241 sb . Append ( "," ) ;
221242 }
222- if ( tb . Primarys . Any ( ) == false )
223- sb . Remove ( sb . Length - 1 , 1 ) ;
224- else {
243+ if ( tb . Primarys . Any ( ) ) {
225244 sb . Append ( " \r \n PRIMARY KEY (" ) ;
226245 foreach ( var tbcol in tb . Primarys ) sb . Append ( _commonUtils . QuoteSqlName ( tbcol . Attribute . Name ) ) . Append ( ", " ) ;
227- sb . Remove ( sb . Length - 2 , 2 ) . Append ( ")" ) ;
246+ sb . Remove ( sb . Length - 2 , 2 ) . Append ( ")," ) ;
247+ }
248+ foreach ( var uk in tb . Uniques ) {
249+ sb . Append ( " \r \n UNIQUE KEY " ) . Append ( _commonUtils . QuoteSqlName ( uk . Key ) ) . Append ( "(" ) ;
250+ foreach ( var tbcol in uk . Value ) sb . Append ( _commonUtils . QuoteSqlName ( tbcol . Attribute . Name ) ) . Append ( ", " ) ;
251+ sb . Remove ( sb . Length - 2 , 2 ) . Append ( ")," ) ;
228252 }
253+ sb . Remove ( sb . Length - 1 , 1 ) ;
229254 sb . Append ( "\r \n ) Engine=InnoDB CHARACTER SET utf8;\r \n " ) ;
230255 sb . Append ( "INSERT INTO " ) . Append ( tmptablename ) . Append ( " (" ) ;
231256 foreach ( var tbcol in tb . Columns . Values )
0 commit comments