diff --git a/src/config.d b/src/config.d index 23bcdb12..66ed19a1 100644 --- a/src/config.d +++ b/src/config.d @@ -16,7 +16,7 @@ final class Config { refreshTokenFilePath = configDirName ~ "/refresh_token"; statusTokenFilePath = configDirName ~ "/status_token"; - databaseFilePath = configDirName ~ "/items.db"; + databaseFilePath = configDirName ~ "/items.sqlite3"; uploadStateFilePath = configDirName ~ "/resume_upload"; userConfigFilePath = configDirName ~ "/config"; } diff --git a/src/itemdb.d b/src/itemdb.d index 8db47ee2..9b52eee0 100644 --- a/src/itemdb.d +++ b/src/itemdb.d @@ -33,21 +33,25 @@ final class ItemDatabase this(const(char)[] filename) { db = Database(filename); - db.exec("CREATE TABLE IF NOT EXISTS item ( - id TEXT PRIMARY KEY, - name TEXT NOT NULL, - type TEXT NOT NULL, - eTag TEXT NOT NULL, - cTag TEXT, - mtime TEXT NOT NULL, - parentId TEXT, - crc32 TEXT, - FOREIGN KEY (parentId) REFERENCES item (id) ON DELETE CASCADE - )"); - db.exec("CREATE INDEX IF NOT EXISTS name_idx ON item (name)"); + if (db.getVersion() == 0) { + db.exec("CREATE TABLE item ( + id TEXT PRIMARY KEY, + name TEXT NOT NULL, + type TEXT NOT NULL, + eTag TEXT NOT NULL, + cTag TEXT, + mtime TEXT NOT NULL, + parentId TEXT, + crc32 TEXT, + FOREIGN KEY (parentId) REFERENCES item (id) ON DELETE CASCADE + )"); + db.exec("CREATE INDEX name_idx ON item (name)"); + db.setVersion(itemDatabaseVersion); + } else if (db.getVersion() != itemDatabaseVersion) { + throw new Exception("The item database is incompatible, please resync manually"); + } db.exec("PRAGMA foreign_keys = ON"); db.exec("PRAGMA recursive_triggers = ON"); - db.setVersion(itemDatabaseVersion); insertItemStmt = db.prepare("INSERT OR REPLACE INTO item (id, name, type, eTag, cTag, mtime, parentId, crc32) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); updateItemStmt = db.prepare(" diff --git a/src/main.d b/src/main.d index 241a096f..4e657081 100644 --- a/src/main.d +++ b/src/main.d @@ -46,6 +46,14 @@ int main(string[] args) if (!exists(configDirName)) mkdir(configDirName); auto cfg = new config.Config(configDirName); cfg.init(); + + // upgrades + if (exists(configDirName ~ "/items.db")) { + remove(configDirName ~ "/items.db"); + log.log("Database schema changed, resync needed"); + resync = true; + } + if (resync || logout) { log.log("Deleting the saved status ..."); safeRemove(cfg.databaseFilePath);