-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #362 from Particular/timeout-manager-index-check-f…
…ixes Fixing TimeoutManager index validator for quoted schemas
- Loading branch information
Showing
3 changed files
with
199 additions
and
21 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
src/NServiceBus.NHibernate.Tests/TimeoutPersister/When_checking_db_indexes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
namespace NServiceBus.NHibernate.Tests.TimeoutPersister | ||
{ | ||
using System.Data.SqlClient; | ||
using System.Threading.Tasks; | ||
using global::NHibernate.Cfg; | ||
using global::NHibernate.Dialect; | ||
using global::NHibernate.Mapping.ByCode; | ||
using global::NHibernate.Tool.hbm2ddl; | ||
using NUnit.Framework; | ||
using TimeoutPersisters.NHibernate.Config; | ||
using TimeoutPersisters.NHibernate.Installer; | ||
|
||
class When_checking_db_indexes | ||
{ | ||
SchemaExport schemaExport; | ||
|
||
string dbSchemaName; | ||
bool dbSchemaNeedsQuoting; | ||
|
||
[Test] | ||
public async Task Should_detect_existing_TimeoutEntity_index_in_default_configuration() | ||
{ | ||
var configuration = await CreateTimeoutManagerObjects(); | ||
|
||
var validationResult = new TimeoutsIndexValidator(configuration).Validate(); | ||
|
||
Assert.IsTrue(validationResult.IsValid, "Validation should pass for default db structure."); | ||
} | ||
|
||
[Test] | ||
public async Task Should_detect_missing_TimeoutEntity_index_in_unquoted_schema() | ||
{ | ||
dbSchemaName = "some_schema"; | ||
|
||
var configuration = await CreateTimeoutManagerObjects(); | ||
await DropIndex(); | ||
|
||
var validationResult = new TimeoutsIndexValidator(configuration).Validate(); | ||
|
||
Assert.IsFalse(validationResult.IsValid, "Validation should fail if the index is missing."); | ||
} | ||
|
||
[Test] | ||
public async Task Should_detect_existing_TimeoutEntity_index_in_quoted_schema() | ||
{ | ||
dbSchemaName = "quoted-schema"; | ||
dbSchemaNeedsQuoting = true; | ||
|
||
var configuration = await CreateTimeoutManagerObjects(); | ||
|
||
var validationResult = new TimeoutsIndexValidator(configuration).Validate(); | ||
|
||
Assert.IsTrue(validationResult.IsValid, "Validation should pass for existing index in quoted schema."); | ||
} | ||
|
||
[Test] | ||
public async Task Should_detect_missing_TimeoutEntity_index_in_quoted_schema() | ||
{ | ||
dbSchemaName = "quoted-schema"; | ||
dbSchemaNeedsQuoting = true; | ||
|
||
var configuration = await CreateTimeoutManagerObjects(); | ||
await DropIndex(); | ||
|
||
var validationResult = new TimeoutsIndexValidator(configuration).Validate(); | ||
|
||
Assert.IsFalse(validationResult.IsValid, "Validation should fail if index is missing in quoted schema."); | ||
} | ||
|
||
async Task<Configuration> CreateTimeoutManagerObjects() | ||
{ | ||
var configuration = new Configuration() | ||
.DataBaseIntegration(x => | ||
{ | ||
x.Dialect<MsSql2012Dialect>(); | ||
x.ConnectionString = Consts.SqlConnectionString; | ||
}); | ||
|
||
if (dbSchemaName != null) | ||
{ | ||
await CreateDbSchema(); | ||
|
||
configuration.SetProperty(Environment.DefaultSchema, dbSchemaNeedsQuoting ? $"[{dbSchemaName}]" : dbSchemaName); | ||
} | ||
|
||
var mapper = new ModelMapper(); | ||
mapper.AddMapping<TimeoutEntityMap>(); | ||
|
||
configuration.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities()); | ||
|
||
schemaExport = new SchemaExport(configuration); | ||
await schemaExport.CreateAsync(false, true); | ||
return configuration; | ||
} | ||
|
||
Task CreateDbSchema() => ExecuteCommand($"IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = N'{dbSchemaName}') EXEC('CREATE SCHEMA [{dbSchemaName}] AUTHORIZATION [dbo]');"); | ||
|
||
Task DropDbSchema() => ExecuteCommand($"DROP SCHEMA [{dbSchemaName}]"); | ||
|
||
Task DropIndex() => ExecuteCommand($"DROP INDEX {TimeoutEntityMap.EndpointIndexName} ON [{dbSchemaName}].[TimeoutEntity]"); | ||
|
||
static async Task ExecuteCommand(string commandText) | ||
{ | ||
using (var connection = new SqlConnection(Consts.SqlConnectionString)) | ||
{ | ||
await connection.OpenAsync(); | ||
|
||
await new SqlCommand(commandText, connection).ExecuteNonQueryAsync(); | ||
} | ||
} | ||
|
||
[TearDown] | ||
public async Task TearDown() | ||
{ | ||
await schemaExport.DropAsync(false, true); | ||
|
||
if (dbSchemaName != null) | ||
{ | ||
await DropDbSchema(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters