Fixed database test.
This commit is contained in:
parent
c1c7487d48
commit
338a51fea9
2 changed files with 35 additions and 21 deletions
|
@ -77,32 +77,45 @@ class Database(path: File, settings: SettingsInterface, callbackInterface: Callb
|
||||||
|
|
||||||
private val db = Database.forURL(DatabasePath, driver = "org.h2.Driver")
|
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.
|
// 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")
|
val dbFile = new File(path.getAbsolutePath + ".mv.db")
|
||||||
if (!dbFile.exists()) {
|
if (dbFile.exists())
|
||||||
logger.info("Database does not exist, creating tables")
|
return
|
||||||
val query = (messages.schema ++ contacts.schema ++ knownDevices.schema).create
|
|
||||||
Await.result(db.run(query), Duration.Inf)
|
logger.info("Database does not exist, creating tables")
|
||||||
settings.put(DatabaseVersionKey, DatabaseVersion)
|
val query = (messages.schema ++ contacts.schema ++ knownDevices.schema).create
|
||||||
|
Await.result(db.run(query), Duration.Inf)
|
||||||
|
settings.put(DatabaseVersionKey, DatabaseVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upgrades database to new version if needed, based on [[DatabaseVersion]].
|
||||||
|
*/
|
||||||
|
private def upgradeDatabase(): Unit = {
|
||||||
|
val oldVersion = settings.get(DatabaseVersionKey, 0)
|
||||||
|
if (oldVersion == DatabaseVersion)
|
||||||
|
return
|
||||||
|
|
||||||
|
logger.info(s"Upgrading database from version $oldVersion to $DatabaseVersion")
|
||||||
|
val connection = DriverManager.getConnection(DatabasePath)
|
||||||
|
if (oldVersion <= 2) {
|
||||||
|
connection.createStatement().executeUpdate("ALTER TABLE MESSAGES ADD COLUMN (tokens INT);")
|
||||||
|
connection.commit()
|
||||||
|
Await.result(db.run(knownDevices.schema.create), Duration.Inf)
|
||||||
}
|
}
|
||||||
|
connection.close()
|
||||||
|
settings.put(DatabaseVersionKey, DatabaseVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apparently, slick doesn't support ALTER TABLE, so we have to write raw SQL for this...
|
// Apparently, slick doesn't support ALTER TABLE, so we have to write raw SQL for this...
|
||||||
{
|
{
|
||||||
val oldVersion = settings.get(DatabaseVersionKey, 0)
|
|
||||||
if (oldVersion != DatabaseVersion) {
|
|
||||||
logger.info(s"Upgrading database from version $oldVersion to $DatabaseVersion")
|
|
||||||
val connection = DriverManager.getConnection(DatabasePath)
|
|
||||||
if (oldVersion <= 2) {
|
|
||||||
connection.createStatement().executeUpdate("ALTER TABLE MESSAGES ADD COLUMN (tokens INT);")
|
|
||||||
connection.commit()
|
|
||||||
Await.result(db.run(knownDevices.schema.create), Duration.Inf)
|
|
||||||
}
|
|
||||||
connection.close()
|
|
||||||
settings.put(DatabaseVersionKey, DatabaseVersion)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def close(): Unit = {
|
def close(): Unit = {
|
||||||
|
|
|
@ -43,8 +43,9 @@ class DatabaseTest extends TestCase {
|
||||||
private val latch = new CountDownLatch(1)
|
private val latch = new CountDownLatch(1)
|
||||||
|
|
||||||
private val database = new Database(databaseFile, new SettingsInterface {
|
private val database = new Database(databaseFile, new SettingsInterface {
|
||||||
override def get[T](key: String, default: T): T = default
|
private var values = Map[String, Any]()
|
||||||
override def put[T](key: String, value: T): Unit = {}
|
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 {
|
}, new CallbackInterface {
|
||||||
override def onConnectionsChanged(): Unit = {}
|
override def onConnectionsChanged(): Unit = {}
|
||||||
override def onContactsUpdated(): Unit = {
|
override def onContactsUpdated(): Unit = {
|
||||||
|
|
Reference in a new issue