public ConfigureEndpointHelper(EndpointConfiguration configuration, string tablePrefix, Func <DbConnection> connectionBuilder, BuildSqlDialect sqlDialect, Func <Exception, bool> exceptionFilter = null) { this.tablePrefix = tablePrefix; this.connectionBuilder = connectionBuilder; this.sqlDialect = sqlDialect; this.exceptionFilter = exceptionFilter; sagaDefinitions = RuntimeSagaDefinitionReader.GetSagaDefinitions(configuration, sqlDialect).ToList(); using (var connection = connectionBuilder()) { connection.Open(); foreach (var definition in sagaDefinitions) { connection.ExecuteCommand(SagaScriptBuilder.BuildDropScript(definition, sqlDialect), tablePrefix, exceptionFilter); try { connection.ExecuteCommand(SagaScriptBuilder.BuildCreateScript(definition, sqlDialect), tablePrefix); } catch (Exception exception) when(exception.Message.Contains("Can't DROP")) { //ignore cleanup exceptions caused by async database operations } } connection.ExecuteCommand(TimeoutScriptBuilder.BuildDropScript(sqlDialect), tablePrefix, exceptionFilter); connection.ExecuteCommand(TimeoutScriptBuilder.BuildCreateScript(sqlDialect), tablePrefix); connection.ExecuteCommand(SubscriptionScriptBuilder.BuildDropScript(sqlDialect), tablePrefix, exceptionFilter); connection.ExecuteCommand(SubscriptionScriptBuilder.BuildCreateScript(sqlDialect), tablePrefix); connection.ExecuteCommand(OutboxScriptBuilder.BuildDropScript(sqlDialect), tablePrefix, exceptionFilter); connection.ExecuteCommand(OutboxScriptBuilder.BuildCreateScript(sqlDialect), tablePrefix); } }
public void Write() { var directory = Path.Combine(TestContext.CurrentContext.TestDirectory, "../../"); foreach (var variant in Enum.GetValues(typeof(BuildSqlVariant)).Cast <BuildSqlVariant>()) { Write(directory, variant, "TimeoutCreate", TimeoutScriptBuilder.BuildCreateScript(variant)); Write(directory, variant, "TimeoutDrop", TimeoutScriptBuilder.BuildDropScript(variant)); Write(directory, variant, "OutboxCreate", OutboxScriptBuilder.BuildCreateScript(variant)); Write(directory, variant, "OutboxDrop", OutboxScriptBuilder.BuildDropScript(variant)); Write(directory, variant, "SubscriptionCreate", SubscriptionScriptBuilder.BuildCreateScript(variant)); Write(directory, variant, "SubscriptionDrop", SubscriptionScriptBuilder.BuildDropScript(variant)); var sagaDefinition = new SagaDefinition( tableSuffix: "OrderSaga", name: "OrderSaga", correlationProperty: new CorrelationProperty( name: "OrderNumber", type: CorrelationPropertyType.Int), transitionalCorrelationProperty: new CorrelationProperty( name: "OrderId", type: CorrelationPropertyType.Guid)); Write(directory, variant, "SagaCreate", SagaScriptBuilder.BuildCreateScript(sagaDefinition, variant)); Write(directory, variant, "SagaDrop", SagaScriptBuilder.BuildDropScript(sagaDefinition, variant)); } foreach (var variant in Enum.GetValues(typeof(SqlVariant)).Cast <SqlVariant>()) { var timeoutCommands = TimeoutCommandBuilder.Build(sqlVariant: variant, tablePrefix: "EndpointName"); Write(directory, variant, "TimeoutAdd", timeoutCommands.Add); Write(directory, variant, "TimeoutNext", timeoutCommands.Next); Write(directory, variant, "TimeoutRange", timeoutCommands.Range); Write(directory, variant, "TimeoutRemoveById", timeoutCommands.RemoveById); Write(directory, variant, "TimeoutRemoveBySagaId", timeoutCommands.RemoveBySagaId); Write(directory, variant, "TimeoutPeek", timeoutCommands.Peek); var outboxCommands = OutboxCommandBuilder.Build(variant, "EndpointName"); Write(directory, variant, "OutboxCleanup", outboxCommands.Cleanup); Write(directory, variant, "OutboxGet", outboxCommands.Get); Write(directory, variant, "OutboxSetAsDispatched", outboxCommands.SetAsDispatched); Write(directory, variant, "OutboxStore", outboxCommands.Store); var subscriptionCommands = SubscriptionCommandBuilder.Build(variant, "EndpointName"); Write(directory, variant, "SubscriptionSubscribe", subscriptionCommands.Subscribe); Write(directory, variant, "SubscriptionUnsubscribe", subscriptionCommands.Unsubscribe); Write(directory, variant, "SubscriptionGetSubscribers", subscriptionCommands.GetSubscribers(new List <MessageType> { new MessageType("MessageTypeName", new Version()) })); var sagaCommandBuilder = new SagaCommandBuilder(variant, "EndpointName"); Write(directory, variant, "SagaComplete", sagaCommandBuilder.BuildCompleteCommand("SagaName")); Write(directory, variant, "SagadGetByProperty", sagaCommandBuilder.BuildGetByPropertyCommand("SagaName", "PropertyName")); Write(directory, variant, "SagaGetBySagaId", sagaCommandBuilder.BuildGetBySagaIdCommand("SagaName")); Write(directory, variant, "SagaSave", sagaCommandBuilder.BuildSaveCommand("SagaName", "CorrelationPproperty", "TransitionalCorrelationPproperty")); Write(directory, variant, "SagaUpdate", sagaCommandBuilder.BuildUpdateCommand("SagaName", "TransitionalCorrelationPproperty")); } }
public void TearDown() { using (var connection = dbConnection()) { connection.Open(); connection.ExecuteCommand(SubscriptionScriptBuilder.BuildDropScript(sqlVariant), tablePrefix, schema: schema); } }
/// <summary> /// Drops the required schema objects. /// </summary> public async Task Uninstall() { using (var connection = await connectionOpener().ConfigureAwait(false)) using (var transaction = connection.BeginTransaction()) { await sqlDialect.ExecuteTableCommand(connection, transaction, SubscriptionScriptBuilder.BuildDropScript(sqlDialect), tablePrefix); transaction.Commit(); } }
public Task Cleanup() { using (var connection = connectionBuilder()) { connection.Open(); foreach (var definition in sagaDefinitions) { connection.ExecuteCommand(SagaScriptBuilder.BuildDropScript(definition, sqlDialect), tablePrefix, exceptionFilter); } connection.ExecuteCommand(TimeoutScriptBuilder.BuildDropScript(sqlDialect), tablePrefix, exceptionFilter); connection.ExecuteCommand(SubscriptionScriptBuilder.BuildDropScript(sqlDialect), tablePrefix, exceptionFilter); connection.ExecuteCommand(OutboxScriptBuilder.BuildDropScript(sqlDialect), tablePrefix, exceptionFilter); } return(Task.FromResult(0)); }
public static void WriteSubscriptionScript(string scriptPath, BuildSqlDialect sqlDialect) { var createPath = Path.Combine(scriptPath, "Subscription_Create.sql"); File.Delete(createPath); using (var writer = File.CreateText(createPath)) { SubscriptionScriptBuilder.BuildCreateScript(writer, sqlDialect); } var dropPath = Path.Combine(scriptPath, "Subscription_Drop.sql"); File.Delete(dropPath); using (var writer = File.CreateText(dropPath)) { SubscriptionScriptBuilder.BuildDropScript(writer, sqlDialect); } }
public void BuildDropScript(BuildSqlDialect sqlDialect) { var builder = new StringBuilder(); using (var writer = new StringWriter(builder)) { SubscriptionScriptBuilder.BuildDropScript(writer, sqlDialect); } var script = builder.ToString(); if (sqlDialect == BuildSqlDialect.MsSqlServer) { SqlValidator.Validate(script); } Approver.Verify(script, scenario: "ForScenario." + sqlDialect); }
public Task Cleanup(CancellationToken cancellationToken = default) { using (var connection = connectionBuilder()) { connection.Open(); foreach (var definition in sagaDefinitions) { connection.ExecuteCommand(SagaScriptBuilder.BuildDropScript(definition, sqlDialect), tablePrefix, exceptionFilter); } connection.ExecuteCommand(TimeoutScriptBuilder.BuildDropScript(sqlDialect), tablePrefix, exceptionFilter); connection.ExecuteCommand(SubscriptionScriptBuilder.BuildDropScript(sqlDialect), tablePrefix, exceptionFilter); connection.ExecuteCommand(OutboxScriptBuilder.BuildDropScript(sqlDialect), tablePrefix, exceptionFilter); } return(Task.CompletedTask); }
public void BuildDropScript(BuildSqlVariant sqlVariant) { var builder = new StringBuilder(); using (var writer = new StringWriter(builder)) { SubscriptionScriptBuilder.BuildDropScript(writer, sqlVariant); } var script = builder.ToString(); if (sqlVariant == BuildSqlVariant.MsSqlServer) { SqlValidator.Validate(script); } using (ApprovalResults.ForScenario(sqlVariant)) { Approvals.Verify(script); } }
public void Setup() { dbConnection = GetConnection(); tablePrefix = GetTablePrefix(); persister = new SubscriptionPersister( connectionBuilder: dbConnection, tablePrefix: $"{tablePrefix}_", sqlVariant: sqlVariant.Convert(), schema: schema, cacheFor: TimeSpan.FromSeconds(10) ); using (var connection = dbConnection()) { connection.Open(); connection.ExecuteCommand(SubscriptionScriptBuilder.BuildDropScript(sqlVariant), tablePrefix, schema: schema); connection.ExecuteCommand(SubscriptionScriptBuilder.BuildCreateScript(sqlVariant), tablePrefix, schema: schema); } }
SubscriptionPersister Setup(string theSchema) { dbConnection = GetConnection(); tablePrefix = GetTablePrefix(); var persister = new SubscriptionPersister( connectionBuilder: () => dbConnection(theSchema), tablePrefix: $"{tablePrefix}_", sqlDialect: sqlDialect.Convert(theSchema), cacheFor: TimeSpan.FromSeconds(10) ); using (var connection = dbConnection(theSchema)) { connection.Open(); connection.ExecuteCommand(SubscriptionScriptBuilder.BuildDropScript(sqlDialect), tablePrefix, schema: theSchema); connection.ExecuteCommand(SubscriptionScriptBuilder.BuildCreateScript(sqlDialect), tablePrefix, schema: theSchema); } return(persister); }
public void Write() { var directory = Path.Combine(TestContext.CurrentContext.TestDirectory, "../../../"); foreach (var variant in Enum.GetValues(typeof(BuildSqlDialect)).Cast <BuildSqlDialect>()) { Write(directory, variant, "TimeoutCreate", TimeoutScriptBuilder.BuildCreateScript(variant)); Write(directory, variant, "TimeoutDrop", TimeoutScriptBuilder.BuildDropScript(variant)); Write(directory, variant, "OutboxCreate", OutboxScriptBuilder.BuildCreateScript(variant)); Write(directory, variant, "OutboxDrop", OutboxScriptBuilder.BuildDropScript(variant)); Write(directory, variant, "SubscriptionCreate", SubscriptionScriptBuilder.BuildCreateScript(variant)); Write(directory, variant, "SubscriptionDrop", SubscriptionScriptBuilder.BuildDropScript(variant)); var sagaDefinition = new SagaDefinition( tableSuffix: "OrderSaga", name: "OrderSaga", correlationProperty: new CorrelationProperty( name: "OrderNumber", type: CorrelationPropertyType.Int), transitionalCorrelationProperty: new CorrelationProperty( name: "OrderId", type: CorrelationPropertyType.Guid)); Write(directory, variant, "SagaCreate", SagaScriptBuilder.BuildCreateScript(sagaDefinition, variant)); Write(directory, variant, "SagaDrop", SagaScriptBuilder.BuildDropScript(sagaDefinition, variant)); } var dialects = new SqlDialect[] { new SqlDialect.MsSqlServer(), new SqlDialect.MySql(), new SqlDialect.Oracle(), new SqlDialect.PostgreSql() }; var assembly = typeof(SqlPersistence).Assembly; foreach (var dialect in dialects) { var timeoutCommands = GetCommand(dialect, "TimeoutCommandBuilder"); Write(directory, dialect, "TimeoutAdd", GetValue(timeoutCommands, "Add")); Write(directory, dialect, "TimeoutNext", GetValue(timeoutCommands, "Next")); Write(directory, dialect, "TimeoutRange", GetValue(timeoutCommands, "Range")); Write(directory, dialect, "TimeoutRemoveById", GetValue(timeoutCommands, "RemoveById")); Write(directory, dialect, "TimeoutRemoveBySagaId", GetValue(timeoutCommands, "RemoveBySagaId")); Write(directory, dialect, "TimeoutPeek", GetValue(timeoutCommands, "Peek")); var outboxCommands = GetCommand(dialect, "OutboxCommandBuilder"); Write(directory, dialect, "OutboxCleanup", GetValue(outboxCommands, "Cleanup")); Write(directory, dialect, "OutboxGet", GetValue(outboxCommands, "Get")); Write(directory, dialect, "OutboxSetAsDispatched", GetValue(outboxCommands, "SetAsDispatched")); Write(directory, dialect, "OutboxStore", GetValue(outboxCommands, "Store")); var subscriptionCommands = GetCommand(dialect, "SubscriptionCommandBuilder"); Write(directory, dialect, "SubscriptionSubscribe", GetValue(subscriptionCommands, "Subscribe")); Write(directory, dialect, "SubscriptionUnsubscribe", GetValue(subscriptionCommands, "Unsubscribe")); Write(directory, dialect, "SubscriptionGetSubscribers", GetSubscribersValue(subscriptionCommands, new List <MessageType> { new MessageType("MessageTypeName", new Version()) })); dynamic dialectAsDynamic = new ExposeInternalMethods(dialect); Write(directory, dialect, "SagaComplete", dialectAsDynamic.BuildCompleteCommand("EndpointName_SagaName")); Write(directory, dialect, "SagaGetByProperty", dialectAsDynamic.BuildGetByPropertyCommand("PropertyName", "EndpointName_SagaName")); Write(directory, dialect, "SagaGetBySagaId", dialectAsDynamic.BuildGetBySagaIdCommand("EndpointName_SagaName")); Write(directory, dialect, "SagaSave", dialectAsDynamic.BuildSaveCommand("CorrelationProperty", "TransitionalCorrelationProperty", "EndpointName_SagaName")); Write(directory, dialect, "SagaUpdate", dialectAsDynamic.BuildUpdateCommand("TransitionalCorrelationProperty", "EndpointName_SagaName")); // since we don't have docs on oracle saga finders if (!(dialect is SqlDialect.Oracle)) { var createSelectWithWhereClause = dialectAsDynamic.BuildSelectFromCommand("EndpointName_SagaName"); Write(directory, dialect, "SagaSelect", createSelectWithWhereClause("1 = 1")); } } }
public void Write() { var directory = Path.Combine(TestContext.CurrentContext.TestDirectory, "../../../"); foreach (var variant in Enum.GetValues(typeof(BuildSqlDialect)).Cast <BuildSqlDialect>()) { Write(directory, variant, "TimeoutCreate", TimeoutScriptBuilder.BuildCreateScript(variant)); Write(directory, variant, "TimeoutDrop", TimeoutScriptBuilder.BuildDropScript(variant)); Write(directory, variant, "OutboxCreate", OutboxScriptBuilder.BuildCreateScript(variant)); Write(directory, variant, "OutboxDrop", OutboxScriptBuilder.BuildDropScript(variant)); Write(directory, variant, "SubscriptionCreate", SubscriptionScriptBuilder.BuildCreateScript(variant)); Write(directory, variant, "SubscriptionDrop", SubscriptionScriptBuilder.BuildDropScript(variant)); var sagaDefinition = new SagaDefinition( tableSuffix: "OrderSaga", name: "OrderSaga", correlationProperty: new CorrelationProperty( name: "OrderNumber", type: CorrelationPropertyType.Int), transitionalCorrelationProperty: new CorrelationProperty( name: "OrderId", type: CorrelationPropertyType.Guid)); Write(directory, variant, "SagaCreate", SagaScriptBuilder.BuildCreateScript(sagaDefinition, variant)); Write(directory, variant, "SagaDrop", SagaScriptBuilder.BuildDropScript(sagaDefinition, variant)); } var dialects = new SqlDialect[] { new SqlDialect.MsSqlServer(), new SqlDialect.MySql(), new SqlDialect.Oracle(), new SqlDialect.PostgreSql() }; foreach (var dialect in dialects) { var timeoutCommands = TimeoutCommandBuilder.Build( sqlDialect: dialect, tablePrefix: "EndpointName"); Write(directory, dialect, "TimeoutAdd", timeoutCommands.Add); Write(directory, dialect, "TimeoutNext", timeoutCommands.Next); Write(directory, dialect, "TimeoutRange", timeoutCommands.Range); Write(directory, dialect, "TimeoutRemoveById", timeoutCommands.RemoveById); Write(directory, dialect, "TimeoutRemoveBySagaId", timeoutCommands.RemoveBySagaId); Write(directory, dialect, "TimeoutPeek", timeoutCommands.Peek); var outboxCommands = OutboxCommandBuilder.Build( tablePrefix: "EndpointName", sqlDialect: dialect); Write(directory, dialect, "OutboxCleanup", outboxCommands.Cleanup); Write(directory, dialect, "OutboxGet", outboxCommands.Get); Write(directory, dialect, "OutboxSetAsDispatched", outboxCommands.SetAsDispatched); Write(directory, dialect, "OutboxStore", outboxCommands.Store); var subscriptionCommands = SubscriptionCommandBuilder.Build( sqlDialect: dialect, tablePrefix: "EndpointName"); Write(directory, dialect, "SubscriptionSubscribe", subscriptionCommands.Subscribe); Write(directory, dialect, "SubscriptionUnsubscribe", subscriptionCommands.Unsubscribe); Write(directory, dialect, "SubscriptionGetSubscribers", subscriptionCommands.GetSubscribers(new List <MessageType> { new MessageType("MessageTypeName", new Version()) })); Write(directory, dialect, "SagaComplete", dialect.BuildCompleteCommand("EndpointName_SagaName")); Write(directory, dialect, "SagaGetByProperty", dialect.BuildGetByPropertyCommand("PropertyName", "EndpointName_SagaName")); Write(directory, dialect, "SagaGetBySagaId", dialect.BuildGetBySagaIdCommand("EndpointName_SagaName")); Write(directory, dialect, "SagaSave", dialect.BuildSaveCommand("CorrelationProperty", "TransitionalCorrelationProperty", "EndpointName_SagaName")); Write(directory, dialect, "SagaUpdate", dialect.BuildUpdateCommand("TransitionalCorrelationProperty", "EndpointName_SagaName")); // since we don't have doco on oracle saga finders if (!(dialect is SqlDialect.Oracle)) { var createSelectWithWhereClause = dialect.BuildSelectFromCommand("EndpointName_SagaName"); Write(directory, dialect, "SagaSelect", createSelectWithWhereClause("1 = 1")); } } }
public override void WriteScripts(BuildSqlDialect dialect) { WriteScript("Subscription_Create.sql", writer => SubscriptionScriptBuilder.BuildCreateScript(writer, dialect)); WriteScript("Subscription_Drop.sql", writer => SubscriptionScriptBuilder.BuildDropScript(writer, dialect)); }