1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-23 01:06:17 +00:00

A DatabaseUpdate can now contain a List of create_query-Strings that are used if the database is to be created from scratch.

This commit is contained in:
Fabian Schlenz 2018-03-08 22:15:49 +01:00
parent 68e5c9be2d
commit c29cd2a8ee

View File

@ -41,7 +41,7 @@ class DatabaseUpdates(protected var conn: Connection, protected var db: Database
logger.debug("DatabaseUpdate.doUpdates running") logger.debug("DatabaseUpdate.doUpdates running")
logger.debug("Getting current database version") logger.debug("Getting current database version")
val version: Int var version: Int
logger.debug("Checking if table database_versions exists") logger.debug("Checking if table database_versions exists")
rs = stmt.executeQuery("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'") rs = stmt.executeQuery("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'")
rs.next() rs.next()
@ -60,6 +60,26 @@ class DatabaseUpdates(protected var conn: Connection, protected var db: Database
System.out.println("Database version: " + version) System.out.println("Database version: " + version)
logger.debug("Max available database version is {}", maxPossibleVersion) 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) { if (version < maxPossibleVersion) {
logger.debug("Update is necessary. {} => {}.", version, maxPossibleVersion) logger.debug("Update is necessary. {} => {}.", version, maxPossibleVersion)
var backup = false var backup = false
@ -151,6 +171,8 @@ internal abstract class DatabaseUpdate(protected var conn: Connection, protected
companion object { companion object {
protected val logger = LoggerFactory.getLogger(DatabaseUpdate::class.java) protected val logger = LoggerFactory.getLogger(DatabaseUpdate::class.java)
} }
open val create_query: List<String>? = null
} }
internal class DB_Update_1(conn: Connection, db: Database) : DatabaseUpdate(conn, db) { internal class DB_Update_1(conn: Connection, db: Database) : DatabaseUpdate(conn, db) {
@ -377,6 +399,15 @@ internal class DB_Update_9(conn: Connection, db: Database) : DatabaseUpdate(conn
get() = 9 get() = 9
override val needsBackup = true 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) @Throws(SQLException::class)
override fun _doUpdate() { override fun _doUpdate() {
val logger = LoggerFactory.getLogger(DB_Update_9::class.java) val logger = LoggerFactory.getLogger(DB_Update_9::class.java)