Lightweight 0.20251202.0
Loading...
Searching...
No Matches
Lightweight::SqlMigration::MigrationManager Class Reference

#include <SqlMigration.hpp>

Public Types

using MigrationList = std::list< MigrationBase const * >
 
using ExecuteCallback = std::function< void(MigrationBase const &, size_t, size_t)>
 

Public Member Functions

LIGHTWEIGHT_API void AddMigration (MigrationBase const *migration)
 
LIGHTWEIGHT_API MigrationList const & GetAllMigrations () const noexcept
 
LIGHTWEIGHT_API MigrationBase const * GetMigration (MigrationTimestamp timestamp) const noexcept
 
LIGHTWEIGHT_API void RemoveAllMigrations ()
 
LIGHTWEIGHT_API std::list< MigrationBase const * > GetPending () const noexcept
 
LIGHTWEIGHT_API void ApplySingleMigration (MigrationBase const &migration)
 
LIGHTWEIGHT_API void RevertSingleMigration (MigrationBase const &migration)
 
LIGHTWEIGHT_API size_t ApplyPendingMigrations (ExecuteCallback const &feedbackCallback={})
 
LIGHTWEIGHT_API void CreateMigrationHistory ()
 Create the migration history table if it does not exist.
 
LIGHTWEIGHT_API std::vector< MigrationTimestampGetAppliedMigrationIds () const
 
LIGHTWEIGHT_API DataMapperGetDataMapper ()
 Get the data mapper used for migrations.
 
LIGHTWEIGHT_API DataMapperGetDataMapper () const
 Get the data mapper used for migrations.
 
LIGHTWEIGHT_API void CloseDataMapper ()
 
LIGHTWEIGHT_API SqlTransaction Transaction ()
 
LIGHTWEIGHT_API std::vector< std::string > PreviewMigration (MigrationBase const &migration) const
 
LIGHTWEIGHT_API std::vector< std::string > PreviewPendingMigrations (ExecuteCallback const &feedbackCallback={}) const
 
LIGHTWEIGHT_API std::vector< ChecksumVerificationResultVerifyChecksums () const
 
LIGHTWEIGHT_API void MarkMigrationAsApplied (MigrationBase const &migration)
 
LIGHTWEIGHT_API RevertResult RevertToMigration (MigrationTimestamp target, ExecuteCallback const &feedbackCallback={})
 
LIGHTWEIGHT_API MigrationStatus GetMigrationStatus () const
 

Static Public Member Functions

static LIGHTWEIGHT_API MigrationManagerGetInstance ()
 

Detailed Description

Main API to use for managing SQL migrations

This class is a singleton and can be accessed using the GetInstance() method.

Definition at line 81 of file SqlMigration.hpp.

Member Typedef Documentation

◆ MigrationList

using Lightweight::SqlMigration::MigrationManager::MigrationList = std::list<MigrationBase const*>

Definition at line 84 of file SqlMigration.hpp.

◆ ExecuteCallback

using Lightweight::SqlMigration::MigrationManager::ExecuteCallback = std::function<void(MigrationBase const& , size_t , size_t )>

Definition at line 120 of file SqlMigration.hpp.

Member Function Documentation

◆ GetInstance()

static LIGHTWEIGHT_API MigrationManager & Lightweight::SqlMigration::MigrationManager::GetInstance ( )
static

Get the singleton instance of the migration manager.

Returns
Reference to the migration manager. Get the singleton instance of the migration manager.
Reference to the migration manager.

◆ AddMigration()

LIGHTWEIGHT_API void Lightweight::SqlMigration::MigrationManager::AddMigration ( MigrationBase const *  migration)

Add a migration to the manager.

Parameters
migrationPointer to the migration to add.

◆ GetAllMigrations()

LIGHTWEIGHT_API MigrationList const & Lightweight::SqlMigration::MigrationManager::GetAllMigrations ( ) const
noexcept

Get all migrations that have been added to the manager.

Returns
List of migrations.

◆ GetMigration()

LIGHTWEIGHT_API MigrationBase const * Lightweight::SqlMigration::MigrationManager::GetMigration ( MigrationTimestamp  timestamp) const
noexcept

Get a migration by timestamp.

Parameters
timestampTimestamp of the migration to get.
Returns
Pointer to the migration if found, nullptr otherwise.

◆ RemoveAllMigrations()

LIGHTWEIGHT_API void Lightweight::SqlMigration::MigrationManager::RemoveAllMigrations ( )

Remove all migrations from the manager.

This function is useful if the migration manager should be reset.

◆ GetPending()

LIGHTWEIGHT_API std::list< MigrationBase const * > Lightweight::SqlMigration::MigrationManager::GetPending ( ) const
noexcept

Get all migrations that have not been applied yet.

Returns
List of pending migrations.

◆ ApplySingleMigration()

LIGHTWEIGHT_API void Lightweight::SqlMigration::MigrationManager::ApplySingleMigration ( MigrationBase const &  migration)

Apply a single migration from a migration object.

Parameters
migrationPointer to the migration to apply.

◆ RevertSingleMigration()

LIGHTWEIGHT_API void Lightweight::SqlMigration::MigrationManager::RevertSingleMigration ( MigrationBase const &  migration)

Revert a single migration from a migration object.

Parameters
migrationPointer to the migration to revert.

◆ ApplyPendingMigrations()

LIGHTWEIGHT_API size_t Lightweight::SqlMigration::MigrationManager::ApplyPendingMigrations ( ExecuteCallback const &  feedbackCallback = {})

Apply all migrations that have not been applied yet.

Parameters
feedbackCallbackCallback to be called for each migration.
Returns
Number of applied migrations.

◆ GetAppliedMigrationIds()

LIGHTWEIGHT_API std::vector< MigrationTimestamp > Lightweight::SqlMigration::MigrationManager::GetAppliedMigrationIds ( ) const

Get all applied migration IDs.

Returns
Vector of applied migration IDs.

◆ GetDataMapper()

LIGHTWEIGHT_API DataMapper & Lightweight::SqlMigration::MigrationManager::GetDataMapper ( ) const
inline

Get the data mapper used for migrations.

Definition at line 151 of file SqlMigration.hpp.

References GetDataMapper().

◆ CloseDataMapper()

LIGHTWEIGHT_API void Lightweight::SqlMigration::MigrationManager::CloseDataMapper ( )

Close the data mapper.

This function is useful if explicitly closing the connection is desired before the migration manager is destroyed.

◆ Transaction()

LIGHTWEIGHT_API SqlTransaction Lightweight::SqlMigration::MigrationManager::Transaction ( )

Get a transaction for the data mapper.

Returns
Transaction.

◆ PreviewMigration()

LIGHTWEIGHT_API std::vector< std::string > Lightweight::SqlMigration::MigrationManager::PreviewMigration ( MigrationBase const &  migration) const

Preview SQL statements for a single migration without executing.

This is useful for dry-run mode to see what SQL would be executed.

Parameters
migrationThe migration to preview.
Returns
Vector of SQL statements that would be executed.

◆ PreviewPendingMigrations()

LIGHTWEIGHT_API std::vector< std::string > Lightweight::SqlMigration::MigrationManager::PreviewPendingMigrations ( ExecuteCallback const &  feedbackCallback = {}) const

Preview SQL statements for all pending migrations without executing.

This is useful for dry-run mode to see what SQL would be executed.

Parameters
feedbackCallbackOptional callback to be called for each migration.
Returns
Vector of all SQL statements that would be executed.

◆ VerifyChecksums()

LIGHTWEIGHT_API std::vector< ChecksumVerificationResult > Lightweight::SqlMigration::MigrationManager::VerifyChecksums ( ) const

Verify checksums of all applied migrations.

Compares the stored checksums in the database with the computed checksums of the current migration definitions. This helps detect if migrations have been modified after they were applied.

Returns
Vector of verification results for migrations with mismatched or missing checksums.

◆ MarkMigrationAsApplied()

LIGHTWEIGHT_API void Lightweight::SqlMigration::MigrationManager::MarkMigrationAsApplied ( MigrationBase const &  migration)

Mark a migration as applied without executing its Up() method.

This is useful for:

  • Baseline migrations when setting up an existing database
  • Marking migrations that were applied manually or through other means
  • Skipping migrations that are not applicable to a specific environment
Parameters
migrationThe migration to mark as applied.
Exceptions
std::runtime_errorif the migration is already applied.

◆ RevertToMigration()

LIGHTWEIGHT_API RevertResult Lightweight::SqlMigration::MigrationManager::RevertToMigration ( MigrationTimestamp  target,
ExecuteCallback const &  feedbackCallback = {} 
)

Revert all migrations applied after the target timestamp.

This method reverts migrations in reverse chronological order, rolling back from the most recent to just after the target. The target migration itself is NOT reverted.

Parameters
targetTarget timestamp to revert to. Migrations > target are reverted.
feedbackCallbackOptional callback for progress updates.
Returns
Result containing list of reverted timestamps or error information.
Note
Stops on first error. Previously reverted migrations in this call are NOT rolled back.

◆ GetMigrationStatus()

LIGHTWEIGHT_API MigrationStatus Lightweight::SqlMigration::MigrationManager::GetMigrationStatus ( ) const

Get a summary status of all migrations.

This method provides a quick overview of the migration state without needing to iterate through individual migrations.

Returns
Status struct with counts of applied, pending, and mismatched migrations.

The documentation for this class was generated from the following file: