Upgrade refactoring
This commit is contained in:
parent
4a9f08cbb7
commit
b93c8bc124
1 changed files with 28 additions and 26 deletions
|
|
@ -57,18 +57,13 @@ public class HalContext {
|
||||||
|
|
||||||
// Init DB
|
// Init DB
|
||||||
File dbFile = FileUtil.find(DB_FILE);
|
File dbFile = FileUtil.find(DB_FILE);
|
||||||
boolean firstTime = false;
|
|
||||||
if(dbFile == null){
|
|
||||||
firstTime = true;
|
|
||||||
logger.info("No database file found, creating new DB...");
|
|
||||||
}
|
|
||||||
db = new DBConnection(DBConnection.DBMS.SQLite, DB_FILE);
|
db = new DBConnection(DBConnection.DBMS.SQLite, DB_FILE);
|
||||||
|
if(dbFile == null){
|
||||||
//Read DB conf
|
logger.info("No database file found, creating new DB...");
|
||||||
if(firstTime){
|
dbConf = new Properties();
|
||||||
dbConf = new Properties();
|
}
|
||||||
}else{
|
else{
|
||||||
dbConf = db.exec("SELECT * FROM conf", new PropertiesSQLResult());
|
dbConf = db.exec("SELECT * FROM conf", new PropertiesSQLResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upgrade DB needed?
|
// Upgrade DB needed?
|
||||||
|
|
@ -83,7 +78,7 @@ public class HalContext {
|
||||||
logger.info("DB version: "+ dbVersion);
|
logger.info("DB version: "+ dbVersion);
|
||||||
if(defaultDBVersion > dbVersion ) {
|
if(defaultDBVersion > dbVersion ) {
|
||||||
logger.info("Starting DB upgrade...");
|
logger.info("Starting DB upgrade...");
|
||||||
if(!firstTime){
|
if(dbFile != null){
|
||||||
File backupDB = FileUtil.getNextFile(dbFile);
|
File backupDB = FileUtil.getNextFile(dbFile);
|
||||||
logger.fine("Backing up DB to: "+ backupDB);
|
logger.fine("Backing up DB to: "+ backupDB);
|
||||||
FileUtil.copy(dbFile, backupDB);
|
FileUtil.copy(dbFile, backupDB);
|
||||||
|
|
@ -123,28 +118,35 @@ public class HalContext {
|
||||||
new SQLResultHandler<Object>() {
|
new SQLResultHandler<Object>() {
|
||||||
@Override
|
@Override
|
||||||
public Object handleQueryResult(Statement stmt, ResultSet result) throws SQLException {
|
public Object handleQueryResult(Statement stmt, ResultSet result) throws SQLException {
|
||||||
|
boolean clearExternalAggrData = false;
|
||||||
|
boolean clearInternalAggrData = false;
|
||||||
while(result.next()){
|
while(result.next()){
|
||||||
if(result.getBoolean("clear_external_aggr_data")){
|
if(result.getBoolean("clear_external_aggr_data"))
|
||||||
logger.fine("Clearing external aggregate data");
|
clearExternalAggrData = true;
|
||||||
db.exec("DELETE FROM sensor_data_aggr WHERE sensor_id = "
|
if(result.getBoolean("clear_internal_aggr_data"))
|
||||||
+ "(SELECT sensor.id FROM user, sensor WHERE user.external == 1 AND sensor.user_id = user.id)");
|
clearInternalAggrData = true;
|
||||||
}
|
|
||||||
if(result.getBoolean("clear_internal_aggr_data")){
|
|
||||||
logger.fine("Clearing local aggregate data");
|
|
||||||
db.exec("DELETE FROM sensor_data_aggr WHERE sensor_id = "
|
|
||||||
+ "(SELECT sensor.id FROM user, sensor WHERE user.external == 0 AND sensor.user_id = user.id)");
|
|
||||||
//update all internal sensors aggregation version to indicate for peers that they need to re-sync all data
|
|
||||||
db.exec("UPDATE sensor SET aggr_version = (aggr_version+1) WHERE id = "
|
|
||||||
+ "(SELECT sensor.id FROM user, sensor WHERE user.external == 0 AND sensor.user_id = user.id)");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(clearExternalAggrData){
|
||||||
|
logger.fine("Clearing external aggregate data");
|
||||||
|
db.exec("DELETE FROM sensor_data_aggr WHERE sensor_id = "
|
||||||
|
+ "(SELECT sensor.id FROM user, sensor WHERE user.external == 1 AND sensor.user_id = user.id)");
|
||||||
|
}
|
||||||
|
if(clearInternalAggrData){
|
||||||
|
logger.fine("Clearing local aggregate data");
|
||||||
|
db.exec("DELETE FROM sensor_data_aggr WHERE sensor_id = "
|
||||||
|
+ "(SELECT sensor.id FROM user, sensor WHERE user.external == 0 AND sensor.user_id = user.id)");
|
||||||
|
//update all internal sensors aggregation version to indicate for peers that they need to re-sync all data
|
||||||
|
db.exec("UPDATE sensor SET aggr_version = (aggr_version+1) WHERE id = "
|
||||||
|
+ "(SELECT sensor.id FROM user, sensor WHERE user.external == 0 AND sensor.user_id = user.id)");
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Check if there is a local user
|
// Check if there is a local user
|
||||||
User localUser = User.getLocalUser(db);
|
User localUser = User.getLocalUser(db);
|
||||||
if (localUser == null){
|
if (localUser == null){
|
||||||
logger.info("Creating local user.");
|
logger.fine("Creating local user.");
|
||||||
localUser = new User();
|
localUser = new User();
|
||||||
localUser.setExternal(false);
|
localUser.setExternal(false);
|
||||||
localUser.save(db);
|
localUser.save(db);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue