Fixed database test.

This commit is contained in:
Felix Ableitner 2016-07-15 21:06:57 +02:00
parent c1c7487d48
commit 338a51fea9
2 changed files with 35 additions and 21 deletions

View file

@ -77,22 +77,32 @@ class Database(path: File, settings: SettingsInterface, callbackInterface: Callb
private val db = Database.forURL(DatabasePath, driver = "org.h2.Driver")
// Create tables if database doesn't exist.
{
createDatabase()
upgradeDatabase()
/**
* Creates a new database file at [[DatabasePath]] if it doesn't already exist.
*/
private def createDatabase(): Unit = {
// H2 appends a .mv.db suffix to the path which we can't change, so we have to check that file.
val dbFile = new File(path.getAbsolutePath + ".mv.db")
if (!dbFile.exists()) {
if (dbFile.exists())
return
logger.info("Database does not exist, creating tables")
val query = (messages.schema ++ contacts.schema ++ knownDevices.schema).create
Await.result(db.run(query), Duration.Inf)
settings.put(DatabaseVersionKey, DatabaseVersion)
}
}
// Apparently, slick doesn't support ALTER TABLE, so we have to write raw SQL for this...
{
/**
* Upgrades database to new version if needed, based on [[DatabaseVersion]].
*/
private def upgradeDatabase(): Unit = {
val oldVersion = settings.get(DatabaseVersionKey, 0)
if (oldVersion != DatabaseVersion) {
if (oldVersion == DatabaseVersion)
return
logger.info(s"Upgrading database from version $oldVersion to $DatabaseVersion")
val connection = DriverManager.getConnection(DatabasePath)
if (oldVersion <= 2) {
@ -103,6 +113,9 @@ class Database(path: File, settings: SettingsInterface, callbackInterface: Callb
connection.close()
settings.put(DatabaseVersionKey, DatabaseVersion)
}
// Apparently, slick doesn't support ALTER TABLE, so we have to write raw SQL for this...
{
}
def close(): Unit = {

View file

@ -43,8 +43,9 @@ class DatabaseTest extends TestCase {
private val latch = new CountDownLatch(1)
private val database = new Database(databaseFile, new SettingsInterface {
override def get[T](key: String, default: T): T = default
override def put[T](key: String, value: T): Unit = {}
private var values = Map[String, Any]()
override def get[T](key: String, default: T): T = values.getOrElse(key, default).asInstanceOf[T]
override def put[T](key: String, value: T): Unit = values += (key -> value)
}, new CallbackInterface {
override def onConnectionsChanged(): Unit = {}
override def onContactsUpdated(): Unit = {