@@ -26,9 +26,10 @@ public class SQLSchemaSynchronizer implements SchemaSynchronizer<SQLDatabaseConn
2626 @ Override
2727 public QueryResult synchronize (SQLDatabaseConnection source , TableSchema from , TableSchema to ) {
2828 List <String > columnQueries = new ArrayList <>();
29- for (int i = 0 ; i < Math .max (from .size (), to .size ()); i ++) {
30- ColumnDefinition fromDefinition = from .size () > i ? from .getDefinitionDetails (i ) : null ;
31- ColumnDefinition toDefinition = to .size () > i ? to .getDefinitionDetails (i ) : null ;
29+ ColumnDefinition [][] definitions = orderDefinitions (from , to );
30+ for (int i = 0 ; i < Math .max (definitions [0 ].length , definitions [1 ].length ); i ++) {
31+ final ColumnDefinition fromDefinition = definitions [0 ][i ];
32+ final ColumnDefinition toDefinition = definitions [1 ][i ];
3233
3334 if (fromDefinition == null && toDefinition != null ) {
3435 columnQueries .addAll (columnQueryBuilder .buildActionQuery (SQLColumnQueryBuilder .ColumnAction .DROP , from .getTable (), fromDefinition , toDefinition ));
@@ -55,4 +56,25 @@ public QueryResult synchronize(SQLDatabaseConnection source, TableSchema from, T
5556 }
5657 return results .stream ().allMatch (QueryResult ::isSuccessful ) ? QueryResult .successful () : new QueryResultImpl (false );
5758 }
59+
60+ private static ColumnDefinition [][] orderDefinitions (TableSchema one , TableSchema two ) {
61+ int maxSize = Math .max (one .size (), two .size ());
62+ ColumnDefinition [][] definitions = new ColumnDefinition [2 ][maxSize ];
63+ for (int i = 0 ; i < maxSize ; i ++) {
64+ if (one .size () > i && two .size () > i && two .getDefinitionDetails (one .getDefinitionName (i )) != null ) {
65+ definitions [0 ][i ] = one .getDefinitionDetails (i );
66+ definitions [1 ][i ] = two .getDefinitionDetails (one .getDefinitionName (i ));
67+ } else if (one .size () > i && two .size () > i ) {
68+ definitions [0 ][i ] = one .getDefinitionDetails (i );
69+ definitions [1 ][i ] = two .getDefinitionDetails (i );
70+ } else if (one .size () > i ) {
71+ definitions [0 ][i ] = one .getDefinitionDetails (i );
72+ definitions [1 ][i ] = null ;
73+ } else if (two .size () > i ) {
74+ definitions [1 ][i ] = two .getDefinitionDetails (i );
75+ definitions [0 ][i ] = null ;
76+ }
77+ }
78+ return definitions ;
79+ }
5880}
0 commit comments