Skip to content

Commit 36aa6e0

Browse files
committed
SQLSchemaSynchronizer now orders columns to prevent unexpected column actions
1 parent e121d9a commit 36aa6e0

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

api/src/main/java/me/zort/sqllib/api/model/TableSchema.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ public ColumnDefinition getDefinitionDetails(int index) {
1414
return definitions[index];
1515
}
1616

17+
public ColumnDefinition getDefinitionDetails(String name) {
18+
for (ColumnDefinition definition : definitions) {
19+
if (definition.getName().equals(name)) return definition;
20+
}
21+
return null;
22+
}
23+
1724
public String getDefinitionName(int index) {
1825
return getDefinitionDetails(index).getName();
1926
}

core/src/main/java/me/zort/sqllib/model/schema/SQLSchemaSynchronizer.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)