TARAXA
migration_base.hpp
Go to the documentation of this file.
1 #pragma once
2 #include "storage/storage.hpp"
3 
5 class Base {
6  public:
7  Base(std::shared_ptr<DbStorage> db) : db_(std::move(db)), batch_(db_->createWriteBatch()) {}
8  virtual ~Base() = default;
9  virtual std::string id() = 0;
10  // We need to specify version here, so in case of major version change(db reindex) we won't apply unneeded migrations
11  virtual uint32_t dbVersion() = 0;
12 
13  bool isApplied() { return db_->lookup_int<bool>(id(), DbStorage::Columns::migrations).has_value(); }
14 
15  void apply(logger::Logger& log) {
16  migrate(log);
17  setApplied();
18  db_->commitWriteBatch(batch_);
19  }
20 
21  protected:
22  // Method with custom logic. All db changes should be made using `batch_`
23  virtual void migrate(logger::Logger& log) = 0;
24 
25  void setApplied() { db_->insert(batch_, DbStorage::Columns::migrations, id(), true); }
26 
27  std::shared_ptr<DbStorage> db_;
29 };
30 } // namespace taraxa::storage::migration
Definition: migration_base.hpp:5
virtual uint32_t dbVersion()=0
virtual std::string id()=0
std::shared_ptr< DbStorage > db_
Definition: migration_base.hpp:27
void apply(logger::Logger &log)
Definition: migration_base.hpp:15
virtual void migrate(logger::Logger &log)=0
Base(std::shared_ptr< DbStorage > db)
Definition: migration_base.hpp:7
Batch batch_
Definition: migration_base.hpp:28
void setApplied()
Definition: migration_base.hpp:25
bool isApplied()
Definition: migration_base.hpp:13
std::hash for asio::adress
Definition: FixedHash.h:483
boost::log::sources::severity_channel_logger_mt<> Logger
Definition: logger.hpp:11
Definition: final_chain_header.hpp:6
rocksdb::WriteBatch Batch
Definition: storage.hpp:73