schema upgrade

This commit is contained in:
skilion 2016-12-25 19:23:33 +01:00
parent c9016845c3
commit 6913c87d5c
3 changed files with 26 additions and 14 deletions

View file

@ -16,7 +16,7 @@ final class Config
{ {
refreshTokenFilePath = configDirName ~ "/refresh_token"; refreshTokenFilePath = configDirName ~ "/refresh_token";
statusTokenFilePath = configDirName ~ "/status_token"; statusTokenFilePath = configDirName ~ "/status_token";
databaseFilePath = configDirName ~ "/items.db"; databaseFilePath = configDirName ~ "/items.sqlite3";
uploadStateFilePath = configDirName ~ "/resume_upload"; uploadStateFilePath = configDirName ~ "/resume_upload";
userConfigFilePath = configDirName ~ "/config"; userConfigFilePath = configDirName ~ "/config";
} }

View file

@ -33,21 +33,25 @@ final class ItemDatabase
this(const(char)[] filename) this(const(char)[] filename)
{ {
db = Database(filename); db = Database(filename);
db.exec("CREATE TABLE IF NOT EXISTS item ( if (db.getVersion() == 0) {
id TEXT PRIMARY KEY, db.exec("CREATE TABLE item (
name TEXT NOT NULL, id TEXT PRIMARY KEY,
type TEXT NOT NULL, name TEXT NOT NULL,
eTag TEXT NOT NULL, type TEXT NOT NULL,
cTag TEXT, eTag TEXT NOT NULL,
mtime TEXT NOT NULL, cTag TEXT,
parentId TEXT, mtime TEXT NOT NULL,
crc32 TEXT, parentId TEXT,
FOREIGN KEY (parentId) REFERENCES item (id) ON DELETE CASCADE crc32 TEXT,
)"); FOREIGN KEY (parentId) REFERENCES item (id) ON DELETE CASCADE
db.exec("CREATE INDEX IF NOT EXISTS name_idx ON item (name)"); )");
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 foreign_keys = ON");
db.exec("PRAGMA recursive_triggers = 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 (?, ?, ?, ?, ?, ?, ?, ?)"); insertItemStmt = db.prepare("INSERT OR REPLACE INTO item (id, name, type, eTag, cTag, mtime, parentId, crc32) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
updateItemStmt = db.prepare(" updateItemStmt = db.prepare("

View file

@ -46,6 +46,14 @@ int main(string[] args)
if (!exists(configDirName)) mkdir(configDirName); if (!exists(configDirName)) mkdir(configDirName);
auto cfg = new config.Config(configDirName); auto cfg = new config.Config(configDirName);
cfg.init(); cfg.init();
// upgrades
if (exists(configDirName ~ "/items.db")) {
remove(configDirName ~ "/items.db");
log.log("Database schema changed, resync needed");
resync = true;
}
if (resync || logout) { if (resync || logout) {
log.log("Deleting the saved status ..."); log.log("Deleting the saved status ...");
safeRemove(cfg.databaseFilePath); safeRemove(cfg.databaseFilePath);