Added DB initialization logic and upgrade needed detection
Former-commit-id: 7a8b4d8157529a10c242ea995c0d2a4939b4d142
This commit is contained in:
parent
4419be3661
commit
4ff6ae2b4c
3 changed files with 77 additions and 16 deletions
BIN
hal-default.db
BIN
hal-default.db
Binary file not shown.
|
|
@ -1,40 +1,68 @@
|
||||||
package se.koc.hal;
|
package se.koc.hal;
|
||||||
|
|
||||||
import zutil.db.DBConnection;
|
import zutil.db.DBConnection;
|
||||||
|
import zutil.db.handler.PropertiesSQLHandler;
|
||||||
|
import zutil.io.file.FileUtil;
|
||||||
|
import zutil.log.LogUtil;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class HalContext {
|
public class HalContext {
|
||||||
|
private static final Logger logger = LogUtil.getLogger();
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
private static final String CONF_FILE = "hal.conf";
|
private static final String CONF_FILE = "hal.conf";
|
||||||
|
private static final String DB_FILE = "hal.db";
|
||||||
|
private static final String DEFAULT_DB_FILE = "hal-default.db";
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
private static DBConnection db;
|
private static DBConnection db;
|
||||||
|
|
||||||
|
private static Properties defaultFileConf;
|
||||||
private static Properties fileConf;
|
private static Properties fileConf;
|
||||||
private static Properties defaultConf;
|
private static Properties dbConf;
|
||||||
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
defaultConf = new Properties();
|
defaultFileConf = new Properties();
|
||||||
defaultConf.setProperty("http_port", ""+8080);
|
defaultFileConf.setProperty("http_port", ""+8080);
|
||||||
defaultConf.setProperty("sync_port", ""+6666);
|
defaultFileConf.setProperty("sync_port", ""+6666);
|
||||||
|
|
||||||
HalContext.initialize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void initialize(){
|
public static void initialize(){
|
||||||
try {
|
try {
|
||||||
// Read conf
|
// Read conf
|
||||||
fileConf = new Properties(defaultConf);
|
fileConf = new Properties(defaultFileConf);
|
||||||
FileReader in = new FileReader(CONF_FILE);
|
FileReader in = new FileReader(CONF_FILE);
|
||||||
fileConf.load(in);
|
fileConf.load(in);
|
||||||
in.close();
|
in.close();
|
||||||
|
|
||||||
// Init DB
|
// Init DB
|
||||||
db = new DBConnection(DBConnection.DBMS.SQLite, "hal.db");
|
if(FileUtil.find(DB_FILE) == null){
|
||||||
|
logger.info("Creating new DB...");
|
||||||
|
resetDB();
|
||||||
|
}
|
||||||
|
db = new DBConnection(DBConnection.DBMS.SQLite, DB_FILE);
|
||||||
|
|
||||||
|
// Read DB conf
|
||||||
|
dbConf = new Properties();
|
||||||
|
db.exec("SELECT * FROM conf", new PropertiesSQLHandler(dbConf));
|
||||||
|
|
||||||
|
// Upgrade DB needed?
|
||||||
|
DBConnection defaultDB = new DBConnection(DBConnection.DBMS.SQLite, DEFAULT_DB_FILE);
|
||||||
|
Properties defaultDBConf =
|
||||||
|
db.exec("SELECT * FROM conf", new PropertiesSQLHandler(new Properties()));
|
||||||
|
if(defaultDBConf.getProperty("db_version").compareTo(dbConf.getProperty("db_version")) > 0) {
|
||||||
|
logger.info("Upgrading DB (from: v"+dbConf.getProperty("db_version") +", to: v"+defaultDBConf.getProperty("db_version"));
|
||||||
|
upgradeDB();
|
||||||
|
}
|
||||||
|
defaultDB.close();
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
@ -54,4 +82,34 @@ public class HalContext {
|
||||||
public static DBConnection getDB(){
|
public static DBConnection getDB(){
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*************************************************************************/
|
||||||
|
|
||||||
|
private static void resetDB() throws IOException {
|
||||||
|
FileUtil.copy(DEFAULT_DB_FILE, DB_FILE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Todo:
|
||||||
|
private static void upgradeDB() throws SQLException {
|
||||||
|
/*
|
||||||
|
- beginTransaction
|
||||||
|
- run a table creation with if not exists (we are doing an upgrade, so the table might not exists yet, it will fail alter and drop)
|
||||||
|
- put in a list the existing columns List<String> columns = DBUtils.GetColumns(db, TableName);
|
||||||
|
- backup table (ALTER table " + TableName + " RENAME TO 'temp_" + TableName)
|
||||||
|
- create new table (the newest table creation schema)
|
||||||
|
- get the intersection with the new columns, this time columns taken from the upgraded table (columns.retainAll(DBUtils.GetColumns(db, TableName));)
|
||||||
|
- restore data (String cols = StringUtils.join(columns, ",");
|
||||||
|
db.execSQL(String.format(
|
||||||
|
"INSERT INTO %s (%s) SELECT %s from temp_%s",
|
||||||
|
TableName, cols, cols, TableName));
|
||||||
|
)
|
||||||
|
- remove backup table (DROP table 'temp_" + TableName)
|
||||||
|
- setTransactionSuccessful
|
||||||
|
*/
|
||||||
|
logger.severe("DB Upgrade not implemented yes!");
|
||||||
|
//db.exec("BeginTransaction");
|
||||||
|
|
||||||
|
//db.exec("EndTransaction");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,19 +23,22 @@ import java.util.logging.Level;
|
||||||
public class PowerChallenge {
|
public class PowerChallenge {
|
||||||
|
|
||||||
|
|
||||||
private static HalDaemon[] daemons = new HalDaemon[]{
|
private static HalDaemon[] daemons;
|
||||||
new DataAggregatorDaemon(),
|
|
||||||
new DataSynchronizationDaemon(),
|
|
||||||
new DataSynchronizationClient()
|
|
||||||
};
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
// init logging
|
||||||
|
LogUtil.setGlobalLevel(Level.ALL);
|
||||||
|
LogUtil.setGlobalFormatter(new CompactLogFormatter());
|
||||||
|
|
||||||
// init logging
|
// init DB and other configurations
|
||||||
LogUtil.setGlobalLevel(Level.ALL);
|
HalContext.initialize();
|
||||||
LogUtil.setGlobalFormatter(new CompactLogFormatter());
|
|
||||||
|
|
||||||
// init daemons
|
// init daemons
|
||||||
|
daemons = new HalDaemon[]{
|
||||||
|
new DataAggregatorDaemon(),
|
||||||
|
new DataSynchronizationDaemon(),
|
||||||
|
new DataSynchronizationClient()
|
||||||
|
};
|
||||||
Timer daemonTimer = new Timer();
|
Timer daemonTimer = new Timer();
|
||||||
for(HalDaemon daemon : daemons){
|
for(HalDaemon daemon : daemons){
|
||||||
daemon.initiate(daemonTimer);
|
daemon.initiate(daemonTimer);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue