From 80afd7070f2d8e57cab2fe55fef611623fdb75f0 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 26 Mar 2018 15:05:41 +0200 Subject: Make sure we initialize the environment correctly and only set the db version when creating it --- common/storage/entitystore.cpp | 18 +++++++++++++++--- common/storage/entitystore.h | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'common/storage') diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index 6021344..d5a7c5f 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp @@ -158,13 +158,23 @@ EntityStore::EntityStore(const ResourceContext &context, const Log::Context &ctx } -void EntityStore::createIfMissing() +void EntityStore::initialize() { - if (!d->exists()) { + //This function is only called in the resource code where we want to be able to write to the databse. + + //Check for the existience of the db without creating it or the envrionment. + //This is required to be able to set the database version only in the case where we create a new database. + if (!Storage::DataStore::exists(Sink::storageLocation(), d->resourceContext.instanceId())) { + //The first time we open the environment we always want it to be read/write. Otherwise subsequent tries to open a write transaction will fail. startTransaction(Sink::Storage::DataStore::ReadWrite); + //Create the database with the correct version if it wasn't existing before + SinkLogCtx(d->logCtx) << "Creating resource database."; Storage::DataStore::setDatabaseVersion(d->transaction, Sink::latestDatabaseVersion()); - commitTransaction(); + } else { + //The first time we open the environment we always want it to be read/write. Otherwise subsequent tries to open a write transaction will fail. + startTransaction(Sink::Storage::DataStore::ReadWrite); } + commitTransaction(); } void EntityStore::startTransaction(Sink::Storage::DataStore::AccessMode accessMode) @@ -383,9 +393,11 @@ void EntityStore::cleanupEntityRevisionsUntil(qint64 revision) bool EntityStore::cleanupRevisions(qint64 revision) { + Q_ASSERT(d->exists()); bool implicitTransaction = false; if (!d->transaction) { startTransaction(Sink::Storage::DataStore::ReadWrite); + Q_ASSERT(d->transaction); implicitTransaction = true; } const auto lastCleanRevision = DataStore::cleanedUpRevision(d->transaction); diff --git a/common/storage/entitystore.h b/common/storage/entitystore.h index 985e7f9..003a2ca 100644 --- a/common/storage/entitystore.h +++ b/common/storage/entitystore.h @@ -39,7 +39,7 @@ public: EntityStore(const ResourceContext &resourceContext, const Sink::Log::Context &); ~EntityStore() = default; - void createIfMissing(); + void initialize(); //Only the pipeline may call the following functions outside of tests bool add(const QByteArray &type, ApplicationDomain::ApplicationDomainType newEntity, bool replayToSource); -- cgit v1.2.3