Skip to content

Commit

Permalink
Bug fix for "there is already another table or index with this name: …
Browse files Browse the repository at this point in the history
…rush_co_uk_rushorm_android_RushJSONFile_temp #9"
  • Loading branch information
Stuart-campbell committed Feb 25, 2015
1 parent 9332a65 commit b7fc20a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SampleApplication extends Application {
public void onCreate() {
super.onCreate();
// Commented out because it was interfering with unit tests
//RushAndroid.initialize(getApplicationContext());
// RushAndroid.initialize(getApplicationContext());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ public interface Logger {

public void log(String message);
public void logSql(String sql);
public void logError(String sql);
public void logError(String error);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void initialize(RushClassFinder rushClassFinder, RushStatementRunn

RushColumns rushColumns = new RushColumnsImplementation(columns);

RushUpgradeManager rushUpgradeManager = new ReflectionUpgradeManager();
RushUpgradeManager rushUpgradeManager = new ReflectionUpgradeManager(logger);
Map<Class, AnnotationCache> annotationCache = new HashMap<>();
RushSaveStatementGenerator saveStatementGenerator = new ReflectionSaveStatementGenerator();
RushConflictSaveStatementGenerator conflictSaveStatementGenerator = new ConflictSaveStatementGenerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;
import java.util.List;

import co.uk.rushorm.core.Logger;
import co.uk.rushorm.core.Rush;
import co.uk.rushorm.core.RushStatementRunner;
import co.uk.rushorm.core.RushUpgradeManager;
Expand Down Expand Up @@ -46,6 +47,7 @@ private PotentialMapping(String[] oldNames, String newName) {
}
}

private static final String TEMP_PREFIX = "_temp";
private static final String COLUMNS_INFO = "PRAGMA table_info(%s)";
private static final String RENAME_TABLE = "ALTER TABLE %s RENAME TO %s";
private static final String TABLE_INFO = "SELECT name FROM sqlite_master WHERE type='table';";
Expand All @@ -60,6 +62,12 @@ private PotentialMapping(String[] oldNames, String newName) {

private static final String DELETE_INDEX = "DROP INDEX %s;";

private final Logger logger;

public ReflectionUpgradeManager(Logger logger) {
this.logger = logger;
}

@Override
public void upgrade(List<Class> classList, UpgradeCallback callback) {

Expand All @@ -69,6 +77,8 @@ public void upgrade(List<Class> classList, UpgradeCallback callback) {
List<String> currentTables = currentTables(callback);
List<TableMapping> tableMappings = new ArrayList<>();

dropAnyObsoleteTempTables(currentTables, callback);

for(Class clazz : classList) {
PotentialTableMapping potentialTableMapping = potentialMapping(clazz, potentialJoinMappings);
String tableName = nameExists(currentTables, potentialTableMapping.name.oldNames);
Expand Down Expand Up @@ -101,13 +111,12 @@ public void upgrade(List<Class> classList, UpgradeCallback callback) {
joinMapping.indexes.add(tableName + "_idx");
joinMapping.isJoin = true;
tableMappings.add(joinMapping);

}
}

for (TableMapping tableMapping : tableMappings) {
if(tableMapping.name.oldName.equals(tableMapping.name.newName)) {
tableMapping.name.oldName = tableMapping.name.oldName + "_temp";
tableMapping.name.oldName = tableMapping.name.oldName + TEMP_PREFIX;
renameTable(tableMapping.name.oldName, tableMapping.name.newName, callback);
}
for(String index : tableMapping.indexes) {
Expand All @@ -129,7 +138,6 @@ public void upgrade(List<Class> classList, UpgradeCallback callback) {
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

}

private String nameExists(List<String> columns, String[] names) {
Expand All @@ -143,6 +151,20 @@ private String nameExists(List<String> columns, String[] names) {
return null;
}

private void dropAnyObsoleteTempTables(List<String> tables, UpgradeCallback callback) {
List<String> tablesToRemove = new ArrayList<>();
for (String table : tables) {
if(table.endsWith(TEMP_PREFIX)) {
logger.logError("Dropping tmp table \"" + table + "\" from last upgrade, this implies something when wrong during the last upgrade.");
callback.runRaw(String.format(DROP, table));
tablesToRemove.add(table);
}
}
for (String table : tablesToRemove) {
tables.remove(table);
}
}

private List<String> currentTables(UpgradeCallback callback) {
RushStatementRunner.ValuesCallback values = callback.runStatement(TABLE_INFO);
List<String> tables = new ArrayList<>();
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>co.uk.rushorm</groupId>
<artifactId>rushandroid</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<packaging>jar</packaging>
<name>RushAndroid</name>
<properties>
Expand Down

0 comments on commit b7fc20a

Please sign in to comment.