public void BuildDropScript(BuildSqlDialect sqlDialect) { var builder = new StringBuilder(); using (var writer = new StringWriter(builder)) { var saga = new SagaDefinition( correlationProperty: new CorrelationProperty ( name: "CorrelationProperty", type: CorrelationPropertyType.String ), tableSuffix: "theSaga", name: "theSaga" ); SagaScriptBuilder.BuildDropScript(saga, sqlDialect, writer); } var script = builder.ToString(); if (sqlDialect == BuildSqlDialect.MsSqlServer) { SqlValidator.Validate(script); } #if NET452 using (ApprovalResults.ForScenario(sqlDialect)) { Approvals.Verify(script); } #endif }
public void CreateWithCorrelationAndTransitional(BuildSqlDialect sqlDialect) { var saga = new SagaDefinition( tableSuffix: "theSaga", name: "theSaga", correlationProperty: new CorrelationProperty ( name: "CorrelationProperty", type: CorrelationPropertyType.String ), transitionalCorrelationProperty: new CorrelationProperty ( name: "TransitionalProperty", type: CorrelationPropertyType.String ) ); var builder = new StringBuilder(); using (var writer = new StringWriter(builder)) { SagaScriptBuilder.BuildCreateScript(saga, sqlDialect, writer); } var script = builder.ToString(); if (sqlDialect == BuildSqlDialect.MsSqlServer) { SqlValidator.Validate(script); } Approver.Verify(script, scenario: "ForScenario." + sqlDialect); }
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 SqlTestVariant(SqlDialect dialect, BuildSqlDialect buildDialect, Func <DbConnection> connectionFactory, bool usePessimisticMode) { Dialect = dialect; BuildDialect = buildDialect; ConnectionFactory = connectionFactory; UsePessimisticMode = usePessimisticMode; }
public OutboxPersisterTests(BuildSqlDialect sqlDialect, string schema, bool pessimistic, bool transactionScope) { this.sqlDialect = sqlDialect; this.schema = schema; this.pessimistic = pessimistic; this.transactionScope = transactionScope; }
public override void WriteScripts(BuildSqlDialect dialect) { Directory.CreateDirectory(sagaPath); var metaDataReader = new AllSagaDefinitionReader(moduleDefinition); var index = 0; foreach (var saga in metaDataReader.GetSagas(logError)) { var sagaFileName = saga.TableSuffix; var maximumNameLength = 244 - ScriptPath.Length; if (sagaFileName.Length > maximumNameLength) { sagaFileName = $"{sagaFileName.Substring(0, maximumNameLength)}_{index}"; index++; } var createPath = Path.Combine(sagaPath, $"{sagaFileName}_Create.sql"); WriteScript(createPath, writer => SagaScriptBuilder.BuildCreateScript(saga, dialect, writer)); var dropPath = Path.Combine(sagaPath, $"{sagaFileName}_Drop.sql"); WriteScript(dropPath, writer => SagaScriptBuilder.BuildDropScript(saga, dialect, writer)); } }
public static string BuildCreateScript(SagaDefinition saga, BuildSqlDialect sqlDialect) { var stringBuilder = new StringBuilder(); using (var stringWriter = new StringWriter(stringBuilder)) { BuildCreateScript(saga, sqlDialect, stringWriter); } return(stringBuilder.ToString()); }
public static void BuildDropScript(SagaDefinition saga, BuildSqlDialect sqlDialect, TextWriter writer) { var sqlDialectWriter = GetSqlDialectWriter(sqlDialect, writer, saga); WriteComment(writer, "TableNameVariable"); sqlDialectWriter.WriteTableNameVariable(); WriteComment(writer, "DropTable"); sqlDialectWriter.WriteDropTable(); }
public static string BuildDropScript(BuildSqlDialect sqlDialect) { var stringBuilder = new StringBuilder(); using (var stringWriter = new StringWriter(stringBuilder)) { BuildDropScript(stringWriter, sqlDialect); } return(stringBuilder.ToString()); }
public static string ReadResource(BuildSqlDialect sqlDialect, string prefix) { var text = $"NServiceBus.Persistence.Sql.ScriptBuilder.{prefix}_{sqlDialect}.sql"; using (var stream = assembly.GetManifestResourceStream(text)) using (var streamReader = new StreamReader(stream)) { return(streamReader.ReadToEnd()); } }
public static void BuildCreateScript(SagaDefinition saga, BuildSqlDialect sqlDialect, TextWriter writer) { Guard.AgainstNull(nameof(saga), saga); Guard.AgainstNull(nameof(writer), writer); SagaDefinitionValidator.ValidateSagaDefinition( correlationProperty: saga.CorrelationProperty?.Name, sagaName: saga.Name, tableSuffix: saga.TableSuffix, transitionalProperty: saga.TransitionalCorrelationProperty?.Name); var sqlDialectWriter = GetSqlDialectWriter(sqlDialect, writer, saga); WriteComment(writer, "TableNameVariable"); sqlDialectWriter.WriteTableNameVariable(); WriteComment(writer, "Initialize"); sqlDialectWriter.Initialize(); WriteComment(writer, "CreateTable"); sqlDialectWriter.WriteCreateTable(); if (saga.CorrelationProperty != null) { WriteComment(writer, $"AddProperty {saga.CorrelationProperty.Name}"); sqlDialectWriter.AddProperty(saga.CorrelationProperty); WriteComment(writer, $"VerifyColumnType {saga.CorrelationProperty.Type}"); sqlDialectWriter.VerifyColumnType(saga.CorrelationProperty); WriteComment(writer, $"WriteCreateIndex {saga.CorrelationProperty.Name}"); sqlDialectWriter.WriteCreateIndex(saga.CorrelationProperty); } if (saga.TransitionalCorrelationProperty != null) { WriteComment(writer, $"AddProperty {saga.TransitionalCorrelationProperty.Name}"); sqlDialectWriter.AddProperty(saga.TransitionalCorrelationProperty); WriteComment(writer, $"VerifyColumnType {saga.TransitionalCorrelationProperty.Type}"); sqlDialectWriter.VerifyColumnType(saga.TransitionalCorrelationProperty); WriteComment(writer, $"CreateIndex {saga.TransitionalCorrelationProperty.Name}"); sqlDialectWriter.WriteCreateIndex(saga.TransitionalCorrelationProperty); } WriteComment(writer, "PurgeObsoleteIndex"); sqlDialectWriter.WritePurgeObsoleteIndex(); WriteComment(writer, "PurgeObsoleteProperties"); sqlDialectWriter.WritePurgeObsoleteProperties(); WriteComment(writer, "CompleteSagaScript"); sqlDialectWriter.CreateComplete(); }
public static SagaDefinition GetSagaDefinition(Type sagaType, BuildSqlDialect sqlDialect) { if (SagaTypeHasIntermediateBaseClass(sagaType)) { throw new Exception("Saga implementations must inherit from either Saga<T> or SqlSaga<T> directly. Deep class hierarchies are not supported."); } var saga = (Saga)FormatterServices.GetUninitializedObject(sagaType); var mapper = new ConfigureHowToFindSagaWithMessage(); methodInfo.Invoke(saga, new object[] { mapper }); CorrelationProperty correlationProperty = null; if (mapper.CorrelationType != null) { correlationProperty = new CorrelationProperty( name: mapper.CorrelationProperty, type: CorrelationPropertyTypeReader.GetCorrelationPropertyType(mapper.CorrelationType)); } var transitionalCorrelationPropertyName = GetSagaMetadataProperty(sagaType, saga, "TransitionalCorrelationPropertyName", att => att.TransitionalCorrelationProperty); CorrelationProperty transitional = null; if (transitionalCorrelationPropertyName != null) { var sagaDataType = sagaType.BaseType.GetGenericArguments()[0]; var transitionalProperty = sagaDataType.GetProperty(transitionalCorrelationPropertyName, AnyInstanceMember); transitional = new CorrelationProperty(transitionalCorrelationPropertyName, CorrelationPropertyTypeReader.GetCorrelationPropertyType(transitionalProperty.PropertyType)); } var tableSuffixOverride = GetSagaMetadataProperty(sagaType, saga, "TableSuffix", att => att.TableSuffix); var tableSuffix = tableSuffixOverride ?? sagaType.Name; if (sqlDialect == BuildSqlDialect.Oracle) { tableSuffix = tableSuffix.Substring(0, Math.Min(27, tableSuffix.Length)); } return(new SagaDefinition( tableSuffix: tableSuffix, name: sagaType.FullName, correlationProperty: correlationProperty, transitionalCorrelationProperty: transitional)); }
public void BuildDropScript(BuildSqlDialect sqlDialect) { var builder = new StringBuilder(); using (var writer = new StringWriter(builder)) { OutboxScriptBuilder.BuildDropScript(writer, sqlDialect); } var script = builder.ToString(); if (sqlDialect == BuildSqlDialect.MsSqlServer) { SqlValidator.Validate(script); } Approver.Verify(script, scenario: "ForScenario." + sqlDialect); }
public static void WriteOutboxScript(string scriptPath, BuildSqlDialect sqlDialect) { var createPath = Path.Combine(scriptPath, "Outbox_Create.sql"); File.Delete(createPath); using (var writer = File.CreateText(createPath)) { OutboxScriptBuilder.BuildCreateScript(writer, sqlDialect); } var dropPath = Path.Combine(scriptPath, "Outbox_Drop.sql"); File.Delete(dropPath); using (var writer = File.CreateText(dropPath)) { OutboxScriptBuilder.BuildDropScript(writer, sqlDialect); } }
public static SagaDefinition GetSagaDefinition(Type sagaType, BuildSqlDialect sqlDialect) { var saga = (Saga)FormatterServices.GetUninitializedObject(sagaType); var mapper = new ConfigureHowToFindSagaWithMessage(); methodInfo.Invoke(saga, new object[] { mapper }); CorrelationProperty correlationProperty = null; if (mapper.CorrelationType != null) { correlationProperty = new CorrelationProperty( name: mapper.CorrelationProperty, type: CorrelationPropertyTypeReader.GetCorrelationPropertyType(mapper.CorrelationType)); } var transitionalCorrelationPropertyName = (string)sagaType .GetProperty("TransitionalCorrelationPropertyName", AnyInstanceMember) .GetValue(saga); CorrelationProperty transitional = null; if (transitionalCorrelationPropertyName != null) { var sagaDataType = sagaType.BaseType.GetGenericArguments()[0]; var transitionalProperty = sagaDataType.GetProperty(transitionalCorrelationPropertyName, AnyInstanceMember); transitional = new CorrelationProperty(transitionalCorrelationPropertyName, CorrelationPropertyTypeReader.GetCorrelationPropertyType(transitionalProperty.PropertyType)); } var tableSuffixOverride = (string)sagaType.GetProperty("TableSuffix", AnyInstanceMember).GetValue(saga); var tableSuffix = tableSuffixOverride ?? sagaType.Name; if (sqlDialect == BuildSqlDialect.Oracle) { tableSuffix = tableSuffix.Substring(0, Math.Min(27, tableSuffix.Length)); } return(new SagaDefinition( tableSuffix: tableSuffix, name: sagaType.FullName, correlationProperty: correlationProperty, transitionalCorrelationProperty: transitional)); }
public static SqlDialect Convert(this BuildSqlDialect sqlDialect, string schema = null) { switch (sqlDialect) { case BuildSqlDialect.MsSqlServer: var sqlServer = new SqlDialect.MsSqlServer(); if (schema != null) { sqlServer.Schema = schema; } return(sqlServer); case BuildSqlDialect.MySql: return(new SqlDialect.MySql()); case BuildSqlDialect.PostgreSql: var postgreSql = new SqlDialect.PostgreSql { JsonBParameterModifier = parameter => { var npgsqlParameter = (Npgsql.NpgsqlParameter)parameter; npgsqlParameter.NpgsqlDbType = NpgsqlDbType.Jsonb; } }; if (schema != null) { postgreSql.Schema = schema; } return(postgreSql); case BuildSqlDialect.Oracle: var oracle = new SqlDialect.Oracle(); if (schema != null) { oracle.Schema = schema; } return(oracle); default: throw new Exception($"Unknown BuildSqlDialect: {sqlDialect}."); } }
static ISagaScriptWriter GetSqlDialectWriter(BuildSqlDialect sqlDialect, TextWriter textWriter, SagaDefinition saga) { if (sqlDialect == BuildSqlDialect.MsSqlServer) { return(new MsSqlServerSagaScriptWriter(textWriter, saga)); } if (sqlDialect == BuildSqlDialect.MySql) { return(new MySqlSagaScriptWriter(textWriter, saga)); } if (sqlDialect == BuildSqlDialect.PostgreSql) { return(new PostgreSqlSagaScriptWriter(textWriter, saga)); } if (sqlDialect == BuildSqlDialect.Oracle) { return(new OracleSagaScriptWriter(textWriter, saga)); } throw new Exception($"Unknown SqlDialect {sqlDialect}."); }
public void BuildDropScript(BuildSqlDialect sqlDialect) { var builder = new StringBuilder(); using (var writer = new StringWriter(builder)) { TimeoutScriptBuilder.BuildDropScript(writer, sqlDialect); } var script = builder.ToString(); if (sqlDialect == BuildSqlDialect.MsSqlServer) { SqlValidator.Validate(script); } #if NET452 using (ApprovalResults.ForScenario(sqlDialect)) { Approvals.Verify(script); } #endif }
public static void BuildCreateScript(TextWriter writer, BuildSqlDialect sqlDialect) { writer.Write(ResourceReader.ReadResource(sqlDialect, "Subscription.Create")); }
public OutboxPersisterTests(BuildSqlDialect sqlDialect, string schema) { this.sqlDialect = sqlDialect; this.schema = schema; connectionManager = new ConnectionManager(() => GetConnection()(schema)); }
public static void BuildDropScript(TextWriter writer, BuildSqlDialect sqlDialect) { writer.Write(ResourceReader.ReadResource(sqlDialect, "Timeout.Drop")); }
protected AdaptTransportConnectionTests(BuildSqlDialect sqlDialect) { this.sqlDialect = sqlDialect; dbConnection = () => GetConnection()(null); }
public override void WriteScripts(BuildSqlDialect dialect) { WriteScript("Outbox_Create.sql", writer => OutboxScriptBuilder.BuildCreateScript(writer, dialect)); WriteScript("Outbox_Drop.sql", writer => OutboxScriptBuilder.BuildDropScript(writer, dialect)); }
public SagaPersisterTests(BuildSqlDialect sqlDialect, string schema) { this.sqlDialect = sqlDialect; this.schema = schema; dbConnection = () => GetConnection()(schema); }
bool ShouldGenerateDialect(BuildSqlDialect dialect) { return(dialectOptions.Count == 0 || dialectOptions.Contains(dialect)); }
public TimeoutPersisterTests(BuildSqlDialect sqlDialect, string schema) { this.sqlDialect = sqlDialect; this.schema = schema; dbConnection = GetConnection(); }
public static IEnumerable <SagaDefinition> GetSagaDefinitions(EndpointConfiguration endpointConfiguration, BuildSqlDialect sqlDialect) { var sagaTypes = endpointConfiguration.GetScannedSagaTypes().ToArray(); if (!sagaTypes.Any()) { return(Enumerable.Empty <SagaDefinition>()); } var sagaAssembly = sagaTypes.First().Assembly; var exceptions = new List <Exception>(); //Validate the saga definitions using script builder compile-time validation using (var moduleDefinition = ModuleDefinition.ReadModule(sagaAssembly.Location, new ReaderParameters(ReadingMode.Deferred))) { var compileTimeReader = new AllSagaDefinitionReader(moduleDefinition); compileTimeReader.GetSagas((e, d) => { exceptions.Add(e); }); } if (exceptions.Any()) { throw new AggregateException(exceptions); } return(sagaTypes.Select(sagaType => GetSagaDefinition(sagaType, sqlDialect))); }
protected AdaptTransportConnectionTests(BuildSqlDialect sqlDialect) { this.sqlDialect = sqlDialect; connectionManager = new ConnectionManager(() => GetConnection()(null)); }
static void Write(string testDirectory, BuildSqlDialect variant, string suffix, string script) { Write(testDirectory, suffix, script, variant.ToString()); }
public SubscriptionPersisterTests(BuildSqlDialect sqlDialect, string schema) { this.sqlDialect = sqlDialect; this.schema = schema; }