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

#include <SqlMigrationLock.hpp>

Public Member Functions

LIGHTWEIGHT_API MigrationLock (SqlConnection &connection, std::string_view lockName="lightweight_migration", std::chrono::milliseconds timeout=std::chrono::seconds(30))
 
LIGHTWEIGHT_API ~MigrationLock ()
 Releases the lock on destruction.
 
 MigrationLock (MigrationLock const &)=delete
 
MigrationLockoperator= (MigrationLock const &)=delete
 
LIGHTWEIGHT_API MigrationLock (MigrationLock &&other) noexcept
 Move constructor.
 
LIGHTWEIGHT_API MigrationLockoperator= (MigrationLock &&other) noexcept
 Move assignment operator.
 
bool IsLocked () const noexcept
 
LIGHTWEIGHT_API void Release ()
 

Detailed Description

RAII-style migration lock to prevent concurrent migrations.

This class provides a distributed locking mechanism to ensure that only one process can run migrations at a time. The implementation uses database-specific advisory locks:

  • SQL Server: sp_getapplock / sp_releaseapplock
  • PostgreSQL: pg_advisory_lock / pg_advisory_unlock
  • SQLite: BEGIN IMMEDIATE transaction with PRAGMA busy_timeout
auto& connection = migrationManager.GetDataMapper().Connection();
MigrationLock lock(connection);
if (lock.IsLocked())
{
migrationManager.ApplyPendingMigrations();
}

Definition at line 33 of file SqlMigrationLock.hpp.

Constructor & Destructor Documentation

◆ MigrationLock()

LIGHTWEIGHT_API Lightweight::SqlMigration::MigrationLock::MigrationLock ( SqlConnection connection,
std::string_view  lockName = "lightweight_migration",
std::chrono::milliseconds  timeout = std::chrono::seconds(30) 
)
explicit

Acquire a migration lock.

Parameters
connectionDatabase connection to use for locking
lockNameName of the lock (default: "lightweight_migration")
timeoutMaximum time to wait for lock acquisition
Exceptions
SqlExceptionif lock cannot be acquired within timeout

Member Function Documentation

◆ IsLocked()

bool Lightweight::SqlMigration::MigrationLock::IsLocked ( ) const
inlinenoexcept

Check if lock is held.

Returns
true if the lock is currently held by this instance

Definition at line 61 of file SqlMigrationLock.hpp.

◆ Release()

LIGHTWEIGHT_API void Lightweight::SqlMigration::MigrationLock::Release ( )

Release the lock early.

This is automatically called in the destructor, but can be called manually if early release is desired.


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