6#include "DataMapper/DataMapper.hpp"
7#include "SqlQuery/Migrate.hpp"
8#include "SqlSchema.hpp"
9#include "SqlTransaction.hpp"
149 std::function<
void(
MigrationBase const& ,
size_t ,
size_t )>;
219 [[nodiscard]] LIGHTWEIGHT_API std::vector<ChecksumVerificationResult>
VerifyChecksums()
const;
277 [[nodiscard]] LIGHTWEIGHT_API std::vector<MigrationRelease>
const&
GetAllReleases() const noexcept;
321 bool ifNotExists =
false;
326 std::map<SqlSchema::FullyQualifiedTableName, TableState>
tables;
376 SqlQueryFormatter const& formatter, std::optional<MigrationTimestamp> upToInclusive = std::nullopt)
const;
385 std::vector<MigrationTimestamp>
const& applied)
const;
388 std::vector<MigrationRelease> _releases;
395#define LIGHTWEIGHT_MIGRATION_PLUGIN() \
397 extern "C" LIGHTWEIGHT_EXPORT Lightweight::SqlMigration::MigrationManager* AcquireMigrationManager() \
399 return &Lightweight::SqlMigration::MigrationManager::GetInstance(); \
416 _timestamp { timestamp },
465 [[nodiscard]]
virtual std::string_view
GetAuthor() const noexcept
491 [[nodiscard]] std::string_view
GetTitle() const noexcept
508 std::string_view _title;
532 template <u
int64_t TsValue>
533 class Migration:
public MigrationBase
540 static constexpr MigrationTimestamp TimeStamp { TsValue };
547 Migration(std::string_view title,
548 std::function<
void(SqlMigrationQueryBuilder&)>
const& up,
549 std::function<
void(SqlMigrationQueryBuilder&)>
const& down = {}):
562 Migration(std::string_view title,
563 MigrationMetadata metadata,
564 std::function<
void(SqlMigrationQueryBuilder&)>
const& up,
565 std::function<
void(SqlMigrationQueryBuilder&)>
const& down = {}):
569 _metadata { std::move(metadata) }
576 void Up(SqlMigrationQueryBuilder& builder)
const override
584 void Down(SqlMigrationQueryBuilder& builder)
const override
593 [[nodiscard]]
bool HasDownImplementation() const noexcept
override
595 return static_cast<bool>(_down);
599 [[nodiscard]] std::vector<MigrationTimestamp> GetDependencies()
const override
601 return _metadata.dependencies;
605 [[nodiscard]] std::string_view GetAuthor() const noexcept
override
607 return _metadata.author;
611 [[nodiscard]] std::string_view GetDescription() const noexcept
override
613 return _metadata.description;
617 std::function<void(SqlMigrationQueryBuilder&)> _up;
618 std::function<void(SqlMigrationQueryBuilder&)> _down;
619 MigrationMetadata _metadata {};
634 template <
typename T>
635 inline constexpr MigrationTimestamp TimestampOf = T::TimeStamp;
643 struct ReleaseRegistrar
646 ReleaseRegistrar(std::string version, MigrationTimestamp highestTimestamp)
657#define _LIGHTWEIGHT_CONCATENATE(s1, s2) s1##s2
658#define _LIGHTWEIGHT_CONCATENATE_INNER(s1, s2) _LIGHTWEIGHT_CONCATENATE(s1, s2)
664#define LIGHTWEIGHT_MIGRATION_INSTANCE(timestamp) migration_##timestamp
684#define LIGHTWEIGHT_SQL_MIGRATION(timestamp, description) \
685 struct Migration_##timestamp: public Lightweight::SqlMigration::MigrationBase \
688 static constexpr Lightweight::SqlMigration::MigrationTimestamp TimeStamp { static_cast<uint64_t>(timestamp) }; \
689 explicit Migration_##timestamp(): \
690 Lightweight::SqlMigration::MigrationBase(TimeStamp, description) \
694 void Up(Lightweight::SqlMigrationQueryBuilder& plan) const override; \
695 void Down(Lightweight::SqlMigrationQueryBuilder& ) const override {} \
698 static Migration_##timestamp _LIGHTWEIGHT_CONCATENATE(migration_, timestamp); \
700 void Migration_##timestamp::Up(Lightweight::SqlMigrationQueryBuilder& plan) const
721#define LIGHTWEIGHT_SQL_RELEASE(version, highestTimestamp) \
722 static ::Lightweight::SqlMigration::detail::ReleaseRegistrar _LIGHTWEIGHT_CONCATENATE_INNER(_lw_release_, __COUNTER__) \
724 (version), ::Lightweight::SqlMigration::MigrationTimestamp \
Main API for mapping records to and from the database using high level C++ syntax.
Query builder for building SQL migration queries.
virtual void Up(SqlMigrationQueryBuilder &plan) const =0
MigrationTimestamp GetTimestamp() const noexcept
std::string_view GetTitle() const noexcept
virtual std::vector< MigrationTimestamp > GetDependencies() const
virtual std::string_view GetAuthor() const noexcept
virtual std::string_view GetDescription() const noexcept
MigrationBase(MigrationBase const &)=default
Default copy constructor.
LIGHTWEIGHT_API std::string ComputeChecksum(SqlQueryFormatter const &formatter) const
MigrationBase(MigrationTimestamp timestamp, std::string_view title)
Constructs a migration with the given timestamp and title.
virtual void Down(SqlMigrationQueryBuilder &plan) const
virtual bool HasDownImplementation() const noexcept
MigrationBase & operator=(MigrationBase const &)=default
Default copy assignment operator.
LIGHTWEIGHT_API void MarkMigrationAsApplied(MigrationBase const &migration)
std::function< void(MigrationBase const &, size_t, size_t)> ExecuteCallback
Callback type invoked during migration execution to report progress.
LIGHTWEIGHT_API DataMapper & GetDataMapper()
Get the data mapper used for migrations.
LIGHTWEIGHT_API RevertResult RevertToMigration(MigrationTimestamp target, ExecuteCallback const &feedbackCallback={})
LIGHTWEIGHT_API void AddMigration(MigrationBase const *migration)
std::list< MigrationBase const * > MigrationList
Type alias for a list of migration pointers.
LIGHTWEIGHT_API MigrationList const & GetAllMigrations() const noexcept
LIGHTWEIGHT_API void RemoveAllMigrations()
LIGHTWEIGHT_API std::vector< std::string > PreviewPendingMigrations(ExecuteCallback const &feedbackCallback={}) const
LIGHTWEIGHT_API void RevertSingleMigration(MigrationBase const &migration)
LIGHTWEIGHT_API void CloseDataMapper()
LIGHTWEIGHT_API DataMapper & GetDataMapper() const
Get the data mapper used for migrations.
LIGHTWEIGHT_API void RemoveAllReleases()
Remove all registered releases. Useful for resetting state in tests.
LIGHTWEIGHT_API PlanFoldingResult FoldRegisteredMigrations(SqlQueryFormatter const &formatter, std::optional< MigrationTimestamp > upToInclusive=std::nullopt) const
Pure plan-walk that folds the effect of every registered migration.
LIGHTWEIGHT_API SqlTransaction Transaction()
LIGHTWEIGHT_API void RegisterRelease(std::string version, MigrationTimestamp highestTimestamp)
LIGHTWEIGHT_API MigrationBase const * GetMigration(MigrationTimestamp timestamp) const noexcept
LIGHTWEIGHT_API std::list< MigrationBase const * > GetPending() const noexcept
LIGHTWEIGHT_API std::vector< ChecksumVerificationResult > VerifyChecksums() const
LIGHTWEIGHT_API MigrationRelease const * FindReleaseByVersion(std::string_view version) const noexcept
LIGHTWEIGHT_API MigrationRelease const * FindReleaseForTimestamp(MigrationTimestamp timestamp) const noexcept
LIGHTWEIGHT_API MigrationList GetMigrationsForRelease(std::string_view version) const
LIGHTWEIGHT_API std::vector< std::string > PreviewMigration(MigrationBase const &migration) const
LIGHTWEIGHT_API size_t ApplyPendingMigrations(ExecuteCallback const &feedbackCallback={})
LIGHTWEIGHT_API void CreateMigrationHistory()
Create the migration history table if it does not exist.
LIGHTWEIGHT_API MigrationStatus GetMigrationStatus() const
LIGHTWEIGHT_API void ValidateDependencies() const
LIGHTWEIGHT_API std::vector< MigrationRelease > const & GetAllReleases() const noexcept
Get all registered releases, sorted ascending by highestTimestamp.
LIGHTWEIGHT_API void ApplySingleMigration(MigrationBase const &migration)
static LIGHTWEIGHT_API MigrationManager & GetInstance()
LIGHTWEIGHT_API std::vector< MigrationTimestamp > GetAppliedMigrationIds() const
std::variant< SqlCreateTablePlan, SqlAlterTablePlan, SqlDropTablePlan, SqlCreateIndexPlan, SqlRawSqlPlan, SqlInsertDataPlan, SqlUpdateDataPlan, SqlDeleteDataPlan > SqlMigrationPlanElement
Represents a single SQL migration plan element.
MigrationTimestamp timestamp
The timestamp of the verified migration.
std::string computedChecksum
The checksum computed from the current migration definition.
std::string storedChecksum
The checksum stored in the database.
bool matches
Whether the stored and computed checksums match.
std::string_view title
The title of the verified migration.
One data step (INSERT/UPDATE/DELETE/RawSql) tagged with its source migration.
std::string sourceTitle
Title of the migration that contributed this data step.
SqlMigrationPlanElement element
The plan element to replay (INSERT/UPDATE/DELETE/RawSql).
MigrationTimestamp sourceTimestamp
Timestamp of the migration that contributed this data step.
Per-table state: ordered column declarations + per-table FK list.
std::vector< SqlColumnDeclaration > columns
Ordered column declarations as they should appear in the emitted CREATE TABLE.
std::vector< SqlCompositeForeignKeyConstraint > compositeForeignKeys
Composite (multi-column) foreign keys declared on this table.
Snapshot of the schema the registered migrations intend to produce.
std::vector< SqlCreateIndexPlan > indexes
Indexes that survive folding (created on tables still present at end).
std::map< SqlSchema::FullyQualifiedTableName, TableState > tables
std::vector< MigrationRelease > releases
Releases declared via LIGHTWEIGHT_SQL_RELEASE that fall within the fold range.
std::vector< DataStep > dataSteps
std::vector< SqlSchema::FullyQualifiedTableName > creationOrder
std::vector< std::pair< MigrationTimestamp, std::string > > foldedMigrations
MigrationTimestamp highestTimestamp
Highest migration timestamp contained in this release (inclusive).
std::string version
Human-readable version string, e.g. "6.7.0".
size_t mismatchCount
Number of applied migrations with checksum mismatches.
size_t appliedCount
Number of migrations that have been applied.
size_t totalRegistered
Total number of registered migrations.
size_t pendingCount
Number of migrations waiting to be applied.
size_t unknownAppliedCount
Number of applied migrations not found in registered list.
constexpr std::weak_ordering operator<=>(MigrationTimestamp const &other) const noexcept=default
Three-way comparison operator.
uint64_t value
The numeric timestamp value identifying the migration.
std::vector< MigrationTimestamp > revertedTimestamps
Successfully reverted migrations.
std::optional< MigrationTimestamp > failedAt
Migration that failed, if any.
std::string errorMessage
Error message if failed.