Skip to content

Commit 135c2f4

Browse files
committed
Fixed the reported AutoMigrator issues
1 parent 737102c commit 135c2f4

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/main/java/org/javawebstack/orm/migration/AutoMigrator.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public static void migrate(Repo<?>... repos){
1818
Map<SQL, List<String>> tables = new HashMap<>();
1919
for(Repo<?> repo : repos){
2020
if(!tables.containsKey(repo.getConnection())){
21+
repo.getConnection().setDebugMode(repo.getInfo().getConfig().isDebugMode());
2122
tables.put(repo.getConnection(), getTables(repo.getConnection()));
23+
repo.getConnection().setDebugMode(false);
2224
}
2325
migrateTable(repo.getConnection(), repo.getInfo(), tables.get(repo.getConnection()).contains(repo.getInfo().getTableName()));
2426
}
@@ -27,12 +29,12 @@ public static void migrate(Repo<?>... repos){
2729
private static void migrateTable(SQL sql, TableInfo info, boolean tableExists){
2830
List<String> addColumns = new ArrayList<>();
2931
List<String> updateColumns = new ArrayList<>();
30-
Map<String, String> columnKeys = getColumnKeys(sql, info.getTableName());
32+
Map<String, String> columnKeys = tableExists ? getColumnKeys(sql, info.getTableName()) : new HashMap<>();
3133
List<Object> addValues = new ArrayList<>();
3234
List<Object> updateValues = new ArrayList<>();
3335
for(String fieldName : info.getFields()){
3436
String columnName = info.getColumnName(fieldName);
35-
StringBuilder sb = new StringBuilder('`')
37+
StringBuilder sb = new StringBuilder("`")
3638
.append(columnName)
3739
.append("` ");
3840
sb.append(info.getType(fieldName).name());
@@ -60,12 +62,12 @@ private static void migrateTable(SQL sql, TableInfo info, boolean tableExists){
6062
}
6163
if(info.getPrimaryKey() != null) {
6264
String columnName = info.getColumnName(info.getPrimaryKey());
63-
if(!columnKeys.get(columnName).contains("PRI"))
65+
if(!columnKeys.containsKey(columnName) || !columnKeys.get(columnName).contains("PRI"))
6466
addColumns.add("PRIMARY KEY (`" + columnName + "`)");
6567
}
6668
for(String uniqueField : info.getUniqueKeys()){
6769
String columnName = info.getColumnName(uniqueField);
68-
if(!columnKeys.get(columnName).contains("UNI"))
70+
if(!columnKeys.containsKey(columnName) || !columnKeys.get(columnName).contains("UNI"))
6971
addColumns.add("UNIQUE (`" + columnName + "`)");
7072
}
7173
if(!tableExists){

0 commit comments

Comments
 (0)