detect old DB versions that need to upgrade

This commit is contained in:
Doug Hoyte
2023-01-13 18:54:04 -05:00
parent c31a213704
commit 27398fe54a
2 changed files with 59 additions and 1 deletions

View File

@ -1,6 +1,6 @@
appName: strfry
quadrable: true
onAppStartup: true
flatBuffers: |
include "../fbs/nostr-index.fbs";
@ -11,6 +11,15 @@ includes: |
}
tables:
## DB meta-data. Single entry, with id = 1
Meta:
tableId: 2
fields:
- name: dbVersion
- name: endianness
## Stored nostr events
Event:
tableId: 1

49
src/onAppStartup.cpp Normal file
View File

@ -0,0 +1,49 @@
#include "golpe.h"
const size_t CURR_DB_VERSION = 1;
void onAppStartup(lmdb::txn &txn, const std::string &cmd) {
auto dbTooOld = [&](uint64_t ver) {
LE << "Database version too old: " << ver << ". Expected version " << CURR_DB_VERSION;
LE << "You should 'strfry export' your events, delete (or move) the DB files, and 'strfry import' them";
throw herr("aborting: DB too old");
};
auto dbTooNew = [&](uint64_t ver) {
LE << "Database version too new: " << ver << ". Expected version " << CURR_DB_VERSION;
LE << "You should upgrade your version of 'strfry'";
throw herr("aborting: DB too new");
};
auto s = env.lookup_Meta(txn, 1);
if (!s) {
{
// The first version of the DB didn't use a Meta entry -- we consider this version 0
bool eventFound = false;
env.foreach_Event(txn, [&](auto &ev){
eventFound = true;
return false;
});
if (cmd == "export") return;
if (eventFound) dbTooOld(0);
}
env.insert_Meta(txn, CURR_DB_VERSION, 1);
return;
}
if (s->endianness() != 1) throw herr("DB was created on a machine with different endianness");
if (s->dbVersion() < CURR_DB_VERSION) {
if (cmd == "export") return;
dbTooOld(s->dbVersion());
}
if (s->dbVersion() > CURR_DB_VERSION) {
dbTooNew(s->dbVersion());
}
}