6#include "DataMapper/DataMapper.hpp"
8#include "SqlTransaction.hpp"
21struct MigrationTimestamp
25 constexpr bool operator==(MigrationTimestamp
const& other)
const noexcept
27 return value == other.value;
30 constexpr bool operator!=(MigrationTimestamp
const& other)
const noexcept
32 return !(*
this == other);
37class SqlMigrationExecutor
40 virtual ~SqlMigrationExecutor() =
default;
42 virtual void OnCreateTable(SqlCreateTablePlan
const& createTable) = 0;
45 virtual void OnDropTable(std::string_view tableName) = 0;
52 using MigrationList = std::list<MigrationBase const*>;
54 LIGHTWEIGHT_API
static MigrationManager& GetInstance()
56 static MigrationManager instance;
60 LIGHTWEIGHT_API
void AddMigration(MigrationBase
const* migration);
62 [[nodiscard]] LIGHTWEIGHT_API MigrationList
const& GetAllMigrations() const noexcept;
64 [[nodiscard]] LIGHTWEIGHT_API MigrationBase const* GetMigration(MigrationTimestamp timestamp) const;
66 LIGHTWEIGHT_API
void RemoveAllMigrations();
68 [[nodiscard]] LIGHTWEIGHT_API std::list<MigrationBase const*> GetPending() const noexcept;
70 using ExecuteCallback =
71 std::function<
void(MigrationBase const& ,
size_t ,
size_t )>;
73 LIGHTWEIGHT_API
void ApplySingleMigration(MigrationTimestamp timestamp);
74 LIGHTWEIGHT_API
void ApplySingleMigration(MigrationBase const& migration);
75 LIGHTWEIGHT_API
size_t ApplyPendingMigrations(ExecuteCallback const& feedbackCallback = {});
77 LIGHTWEIGHT_API
void CreateMigrationHistory();
78 [[nodiscard]] LIGHTWEIGHT_API std::vector<MigrationTimestamp> GetAppliedMigrationIds()
const;
80 [[nodiscard]] LIGHTWEIGHT_API
DataMapper& GetDataMapper();
82 [[nodiscard]] LIGHTWEIGHT_API
DataMapper& GetDataMapper()
const
84 return const_cast<MigrationManager*
>(
this)->GetDataMapper();
87 LIGHTWEIGHT_API
void CloseDataMapper();
92 MigrationList _migrations;
93 mutable std::optional<DataMapper> _mapper;
100 MigrationBase(MigrationTimestamp timestamp, std::string_view title):
101 _timestamp { timestamp },
104 MigrationManager::GetInstance().AddMigration(
this);
107 virtual ~MigrationBase() =
default;
111 [[nodiscard]] MigrationTimestamp GetTimestamp() const noexcept
116 [[nodiscard]] std::string_view GetTitle() const noexcept
122 MigrationTimestamp _timestamp;
123 std::string_view _title;
126class Migration:
public MigrationBase
129 Migration(MigrationTimestamp timestamp,
130 std::string_view title,
132 MigrationBase(timestamp, title),
148#define LIGHTWEIGHT_CONCATENATE(s1, s2) s1##s2
150#define LIGHTWEIGHT_MIGRATION_INSTANCE(timestamp) migration_##timestamp
158#define LIGHTWEIGHT_SQL_MIGRATION(timestamp, description) \
159 struct Migration_##timestamp: public SqlMigration::MigrationBase \
161 explicit Migration_##timestamp(): \
162 MigrationBase(SqlMigration::MigrationTimestamp { timestamp }, description) \
166 void Execute(SqlMigrationQueryBuilder& planer) const override; \
169 static Migration_##timestamp LIGHTWEIGHT_CONCATENATE(migration_, timestamp); \
171 void Migration_##timestamp::Execute(SqlMigrationQueryBuilder& plan) const
Main API for mapping records to and from the database using high level C++ syntax.
Represents a connection to a SQL database.
Query builder for building SQL migration queries.
Represents a SQL ALTER TABLE plan on a given table.