Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ApplicationLockId property #304

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Evolve/Dialect/Cassandra/CassandraCluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override IEvolveMetadata GetMetadataTable(string schema, string tableName
/// Will check for a predefined keyspace and table to see if there is a lock.
/// Otherwise, always returns true, because the lock is granted at table level.
/// </summary>
public override bool TryAcquireApplicationLock()
public override bool TryAcquireApplicationLock(object? lockId = null)
{
string clusterLockKeyspaceName = "cluster_lock";
string clusterLockTableName = "lock";
Expand All @@ -54,6 +54,9 @@ public override bool TryAcquireApplicationLock()
clusterLockTableName = clusterLock!.GetValue("defaultClusterLockTable", clusterLockTableName)!;
}

// Input parameter overrides both the above variants
clusterLockTableName = lockId?.ToString() ?? clusterLockTableName;

try
{
return WrappedConnection.QueryForLong($"select count(locked) from {clusterLockKeyspaceName}.{clusterLockTableName}") == 0;
Expand All @@ -71,7 +74,7 @@ public override bool TryAcquireApplicationLock()
/// <summary>
/// Returns always true, because the lock is released at table level.
/// </summary>
public override bool ReleaseApplicationLock() => true;
public override bool ReleaseApplicationLock(object? lockId = null) => true;

public override SqlStatementBuilderBase SqlStatementBuilder { get; } = new CqlStatementBuilder();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Evolve/Dialect/CockroachDB/CockroachDBCluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public CockroachDBCluster(WrappedConnection wrappedConnection) : base(wrappedCon

public override Schema GetSchema(string schemaName) => new CockroachDBDatabase(schemaName, WrappedConnection);

public override bool TryAcquireApplicationLock() => true;
public override bool TryAcquireApplicationLock(object? lockId = null) => true;

public override bool ReleaseApplicationLock() => true;
public override bool ReleaseApplicationLock(object? lockId = null) => true;
}
}
4 changes: 2 additions & 2 deletions src/Evolve/Dialect/DatabaseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ protected DatabaseHelper(WrappedConnection wrappedConnection)

public abstract IEvolveMetadata GetMetadataTable(string schema, string tableName);

public abstract bool TryAcquireApplicationLock();
public abstract bool TryAcquireApplicationLock(object? lockId = null);

public abstract bool ReleaseApplicationLock();
public abstract bool ReleaseApplicationLock(object? lockId = null);

public void Dispose()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Evolve/Dialect/MySQL/MySQLDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public MySQLDatabase(WrappedConnection wrappedConnection) : base(wrappedConnecti

public override Schema GetSchema(string schemaName) => new MySQLSchema(schemaName, WrappedConnection);

public override bool TryAcquireApplicationLock() => WrappedConnection.QueryForLong($"SELECT GET_LOCK('{LOCK_ID}', 0);") == 1;
public override bool TryAcquireApplicationLock(object? lockId = null) => WrappedConnection.QueryForLong($"SELECT GET_LOCK('{lockId?? LOCK_ID}', 0);") == 1;

public override bool ReleaseApplicationLock() => WrappedConnection.QueryForLong($"SELECT RELEASE_LOCK('{LOCK_ID}');") == 1;
public override bool ReleaseApplicationLock(object? lockId = null) => WrappedConnection.QueryForLong($"SELECT RELEASE_LOCK('{lockId ?? LOCK_ID}');") == 1;
}
}
4 changes: 2 additions & 2 deletions src/Evolve/Dialect/PostgreSQL/PostgreSQLDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public PostgreSQLDatabase(WrappedConnection wrappedConnection) : base(wrappedCon

public override Schema GetSchema(string schemaName) => new PostgreSQLSchema(schemaName, WrappedConnection);

public override bool TryAcquireApplicationLock() => WrappedConnection.QueryForBool($"SELECT pg_try_advisory_lock({LOCK_ID})");
public override bool TryAcquireApplicationLock(object? lockId = null) => WrappedConnection.QueryForBool($"SELECT pg_try_advisory_lock({lockId ?? LOCK_ID})");

public override bool ReleaseApplicationLock() => WrappedConnection.QueryForBool($"SELECT pg_advisory_unlock({LOCK_ID})");
public override bool ReleaseApplicationLock(object? lockId = null) => WrappedConnection.QueryForBool($"SELECT pg_advisory_unlock({lockId ?? LOCK_ID})");

private string CleanSchemaName(string schemaName)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Evolve/Dialect/SQLServer/SQLServerDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public SQLServerDatabase(WrappedConnection wrappedConnection) : base(wrappedConn

public override Schema GetSchema(string schemaName) => new SQLServerSchema(schemaName, WrappedConnection);

public override bool TryAcquireApplicationLock()
public override bool TryAcquireApplicationLock(object? lockId = null)
{
return WrappedConnection.ExecuteDbCommand("sp_getapplock", cmd =>
{
Expand All @@ -37,7 +37,7 @@ public override bool TryAcquireApplicationLock()

var inParam1 = cmd.CreateParameter();
inParam1.ParameterName = "@Resource";
inParam1.Value = LOCK_ID;
inParam1.Value = lockId ?? LOCK_ID;
inParam1.DbType = DbType.String;
inParam1.Direction = ParameterDirection.Input;
cmd.Parameters.Add(inParam1);
Expand Down Expand Up @@ -70,7 +70,7 @@ public override bool TryAcquireApplicationLock()
}) >= 0;
}

public override bool ReleaseApplicationLock()
public override bool ReleaseApplicationLock(object? lockId = null)
{
return WrappedConnection.ExecuteDbCommand("sp_releaseapplock", cmd =>
{
Expand All @@ -84,7 +84,7 @@ public override bool ReleaseApplicationLock()

var inParam1 = cmd.CreateParameter();
inParam1.ParameterName = "@Resource";
inParam1.Value = LOCK_ID;
inParam1.Value = lockId ?? LOCK_ID;
inParam1.DbType = DbType.String;
inParam1.Direction = ParameterDirection.Input;
cmd.Parameters.Add(inParam1);
Expand Down
4 changes: 2 additions & 2 deletions src/Evolve/Dialect/SQLite/SQLiteDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public SQLiteDatabase(WrappedConnection wrappedConnection) : base(wrappedConnect
/// Not supported in SQLite.
/// </summary>
/// <returns> Always true </returns>
public override bool TryAcquireApplicationLock() => true;
public override bool TryAcquireApplicationLock(object? lockId = null) => true;

/// <summary>
/// Not supported in SQLite.
/// </summary>
/// <returns> Always true </returns>
public override bool ReleaseApplicationLock() => true;
public override bool ReleaseApplicationLock(object? lockId = null) => true;
}
}
5 changes: 3 additions & 2 deletions src/Evolve/Evolve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public string MetadataTableSchema
public bool RetryRepeatableMigrationsUntilNoError { get; set; }
public TransactionKind TransactionMode { get; set; } = TransactionKind.CommitEach;
public bool SkipNextMigrations { get; set; } = false;
public object? ApplicationLockId { get; set; } = null;

private IMigrationLoader? _migrationLoader;
public IMigrationLoader MigrationLoader
Expand Down Expand Up @@ -772,7 +773,7 @@ private void InternalExecuteCommand(Action<DatabaseHelper> commandAction)
if (EnableClusterMode)
{
var metadata = db.GetMetadataTable(MetadataTableSchema, MetadataTableName);
if (!db.ReleaseApplicationLock() || !metadata.ReleaseLock())
if (!db.ReleaseApplicationLock(ApplicationLockId) || !metadata.ReleaseLock())
{
_log("Error trying to release Evolve lock.");
}
Expand Down Expand Up @@ -881,7 +882,7 @@ private void WaitForApplicationLock(DatabaseHelper db)
{
while (true)
{
if (db.TryAcquireApplicationLock())
if (db.TryAcquireApplicationLock(ApplicationLockId))
{
break;
}
Expand Down
20 changes: 20 additions & 0 deletions test/Evolve.Tests/Integration/PostgreSQL/Scenario010.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using EvolveDb.Tests.Infrastructure;
using Xunit;
using Xunit.Abstractions;

namespace EvolveDb.Tests.Integration.PostgreSql
{
[Collection("PostgreSql collection")]
public class Scenario010 : Scenario<PostgreSqlFixture>
{
public Scenario010(PostgreSqlFixture dbContainer, ITestOutputHelper output) : base(dbContainer, output) {}

[Fact]
[Category(Test.PostgreSQL, Test.Sceanario)]
public void Scenario_lockIdSpecific()
{
Evolve.ApplicationLockId = 27893423;
Evolve.Migrate();
}
}
}