From c29cd2a8ee72b7155bd18349b47e733888478d98 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Thu, 8 Mar 2018 22:15:49 +0100 Subject: [PATCH] A DatabaseUpdate can now contain a List of create_query-Strings that are used if the database is to be created from scratch. --- .../telegram_backup/DatabaseUpdates.kt | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt b/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt index 953744d..849bf7b 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt @@ -41,7 +41,7 @@ class DatabaseUpdates(protected var conn: Connection, protected var db: Database logger.debug("DatabaseUpdate.doUpdates running") logger.debug("Getting current database version") - val version: Int + var version: Int logger.debug("Checking if table database_versions exists") rs = stmt.executeQuery("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'") rs.next() @@ -60,6 +60,26 @@ class DatabaseUpdates(protected var conn: Connection, protected var db: Database System.out.println("Database version: " + version) logger.debug("Max available database version is {}", maxPossibleVersion) + if (version == 0) { + logger.debug("Looking for DatabaseUpdate with create_query...") + // This is a fresh database - so we search for the latest available version with a create_query + // and use this as a shortcut. + var update: DatabaseUpdate? = null + for (i in maxPossibleVersion downTo 1) { + update = getUpdateToVersion(i) + logger.trace("Looking at DatabaseUpdate version {}", update.version) + if (update.create_query != null) break + update = null + } + + if (update != null) { + logger.debug("Found DatabaseUpdate version {} with create_query.", update.version) + for (query in update.create_query!!) stmt.execute(query) + stmt.execute("INSERT INTO database_versions (version) VALUES (${update.version})") + version = update.version + } + } + if (version < maxPossibleVersion) { logger.debug("Update is necessary. {} => {}.", version, maxPossibleVersion) var backup = false @@ -151,6 +171,8 @@ internal abstract class DatabaseUpdate(protected var conn: Connection, protected companion object { protected val logger = LoggerFactory.getLogger(DatabaseUpdate::class.java) } + + open val create_query: List? = null } internal class DB_Update_1(conn: Connection, db: Database) : DatabaseUpdate(conn, db) { @@ -376,6 +398,15 @@ internal class DB_Update_9(conn: Connection, db: Database) : DatabaseUpdate(conn override val version: Int get() = 9 override val needsBackup = true + + override val create_query = listOf( + "CREATE TABLE \"chats\" (id INTEGER PRIMARY KEY ASC, name TEXT, type TEXT);", + "CREATE TABLE \"users\" (id INTEGER PRIMARY KEY ASC, first_name TEXT, last_name TEXT, username TEXT, type TEXT, phone TEXT);", + "CREATE TABLE database_versions (version INTEGER);", + "CREATE TABLE runs (id INTEGER PRIMARY KEY ASC, time INTEGER, start_id INTEGER, end_id INTEGER, count_missing INTEGER);", + "CREATE TABLE \"messages\" (id INTEGER PRIMARY KEY AUTOINCREMENT,message_id INTEGER,message_type TEXT,source_type TEXT,source_id INTEGER,sender_id INTEGER,fwd_from_id INTEGER,text TEXT,time INTEGER,has_media BOOLEAN,media_type TEXT,media_file TEXT,media_size INTEGER,media_json TEXT,markup_json TEXT,data BLOB,api_layer INTEGER);", + "CREATE UNIQUE INDEX unique_messages ON messages (source_type, source_id, message_id);" + ) @Throws(SQLException::class) override fun _doUpdate() {