コード例 #1
0
    public void BuildDropScript(BuildSqlVariant sqlVariant)
    {
        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, sqlVariant, writer);
        }
        var script = builder.ToString();

        if (sqlVariant == BuildSqlVariant.MsSqlServer)
        {
            SqlValidator.Validate(script);
        }

        using (ApprovalResults.ForScenario(sqlVariant))
        {
            Approvals.Verify(script);
        }
    }
コード例 #2
0
 public ConfigureEndpointHelper(EndpointConfiguration configuration, string tablePrefix, Func <DbConnection> connectionBuilder, BuildSqlVariant sqlVariant, Func <Exception, bool> exceptionFilter = null)
 {
     this.tablePrefix       = tablePrefix;
     this.connectionBuilder = connectionBuilder;
     this.sqlVariant        = sqlVariant;
     this.exceptionFilter   = exceptionFilter;
     sagaDefinitions        = RuntimeSagaDefinitionReader.GetSagaDefinitions(configuration, sqlVariant).ToList();
     using (var connection = connectionBuilder())
     {
         connection.Open();
         foreach (var definition in sagaDefinitions)
         {
             connection.ExecuteCommand(SagaScriptBuilder.BuildDropScript(definition, sqlVariant), tablePrefix, exceptionFilter);
             try
             {
                 connection.ExecuteCommand(SagaScriptBuilder.BuildCreateScript(definition, sqlVariant), tablePrefix);
             }
             catch (Exception ex)
             {
                 if (!ex.Message.Contains("Can't DROP"))
                 {
                     throw; //ignore cleanup exceptions caused by async database operations
                 }
             }
         }
         connection.ExecuteCommand(TimeoutScriptBuilder.BuildDropScript(sqlVariant), tablePrefix, exceptionFilter);
         connection.ExecuteCommand(TimeoutScriptBuilder.BuildCreateScript(sqlVariant), tablePrefix);
         connection.ExecuteCommand(SubscriptionScriptBuilder.BuildDropScript(sqlVariant), tablePrefix, exceptionFilter);
         connection.ExecuteCommand(SubscriptionScriptBuilder.BuildCreateScript(sqlVariant), tablePrefix);
         connection.ExecuteCommand(OutboxScriptBuilder.BuildDropScript(sqlVariant), tablePrefix, exceptionFilter);
         connection.ExecuteCommand(OutboxScriptBuilder.BuildCreateScript(sqlVariant), tablePrefix);
     }
 }
コード例 #3
0
 void Write(ModuleDefinition moduleDefinition, BuildSqlVariant sqlVariant, string scriptPath)
 {
     Directory.CreateDirectory(scriptPath);
     SagaWriter.WriteSagaScripts(scriptPath, moduleDefinition, sqlVariant, logError);
     TimeoutWriter.WriteTimeoutScript(scriptPath, sqlVariant);
     SubscriptionWriter.WriteSubscriptionScript(scriptPath, sqlVariant);
     OutboxWriter.WriteOutboxScript(scriptPath, sqlVariant);
 }
コード例 #4
0
    public static string ReadResource(BuildSqlVariant sqlVariant, string prefix)
    {
        var text = $"NServiceBus.Persistence.Sql.{prefix}_{sqlVariant}.sql";

        using (var stream = assembly.GetManifestResourceStream(text))
            using (var streamReader = new StreamReader(stream))
            {
                return(streamReader.ReadToEnd());
            }
    }
コード例 #5
0
        public static string BuildDropScript(BuildSqlVariant sqlVariant)
        {
            var stringBuilder = new StringBuilder();

            using (var stringWriter = new StringWriter(stringBuilder))
            {
                BuildDropScript(stringWriter, sqlVariant);
            }
            return(stringBuilder.ToString());
        }
コード例 #6
0
        public static string BuildCreateScript(SagaDefinition saga, BuildSqlVariant sqlVariant)
        {
            var stringBuilder = new StringBuilder();

            using (var stringWriter = new StringWriter(stringBuilder))
            {
                BuildCreateScript(saga, sqlVariant, stringWriter);
            }
            return(stringBuilder.ToString());
        }
コード例 #7
0
        public static void BuildDropScript(SagaDefinition saga, BuildSqlVariant sqlVariant, TextWriter writer)
        {
            var sqlVariantWriter = GetSqlVariantWriter(sqlVariant, writer, saga);

            WriteComment(writer, "TableNameVariable");
            sqlVariantWriter.WriteTableNameVariable();

            WriteComment(writer, "DropTable");
            sqlVariantWriter.WriteDropTable();
        }
コード例 #8
0
        public static void BuildCreateScript(SagaDefinition saga, BuildSqlVariant sqlVariant, 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 sqlVariantWriter = GetSqlVariantWriter(sqlVariant, writer, saga);

            WriteComment(writer, "TableNameVariable");
            sqlVariantWriter.WriteTableNameVariable();

            WriteComment(writer, "Initialize");
            sqlVariantWriter.Initialize();

            WriteComment(writer, "CreateTable");
            sqlVariantWriter.WriteCreateTable();
            if (saga.CorrelationProperty != null)
            {
                WriteComment(writer, $"AddProperty {saga.CorrelationProperty.Name}");
                sqlVariantWriter.AddProperty(saga.CorrelationProperty);

                WriteComment(writer, $"VerifyColumnType {saga.CorrelationProperty.Type}");
                sqlVariantWriter.VerifyColumnType(saga.CorrelationProperty);

                WriteComment(writer, $"WriteCreateIndex {saga.CorrelationProperty.Name}");
                sqlVariantWriter.WriteCreateIndex(saga.CorrelationProperty);
            }
            if (saga.TransitionalCorrelationProperty != null)
            {
                WriteComment(writer, $"AddProperty {saga.TransitionalCorrelationProperty.Name}");
                sqlVariantWriter.AddProperty(saga.TransitionalCorrelationProperty);

                WriteComment(writer, $"VerifyColumnType {saga.TransitionalCorrelationProperty.Type}");
                sqlVariantWriter.VerifyColumnType(saga.TransitionalCorrelationProperty);

                WriteComment(writer, $"CreateIndex {saga.TransitionalCorrelationProperty.Name}");
                sqlVariantWriter.WriteCreateIndex(saga.TransitionalCorrelationProperty);
            }
            WriteComment(writer, "PurgeObsoleteIndex");
            sqlVariantWriter.WritePurgeObsoleteIndex();

            WriteComment(writer, "PurgeObsoleteProperties");
            sqlVariantWriter.WritePurgeObsoleteProperties();

            WriteComment(writer, "CompleteSagaScript");
            sqlVariantWriter.CreateComplete();
        }
コード例 #9
0
 public static void WriteSubscriptionScript(string scriptPath, BuildSqlVariant sqlVariant)
 {
     var createPath = Path.Combine(scriptPath, "Subscription_Create.sql");
     File.Delete(createPath);
     using (var writer = File.CreateText(createPath))
     {
         SubscriptionScriptBuilder.BuildCreateScript(writer, sqlVariant);
     }
     var dropPath = Path.Combine(scriptPath, "Subscription_Drop.sql");
     File.Delete(dropPath);
     using (var writer = File.CreateText(dropPath))
     {
         SubscriptionScriptBuilder.BuildCreateScript(writer, sqlVariant);
     }
 }
コード例 #10
0
        static ISagaScriptWriter GetSqlVariantWriter(BuildSqlVariant sqlVariant, TextWriter textWriter, SagaDefinition saga)
        {
            if (sqlVariant == BuildSqlVariant.MsSqlServer)
            {
                return(new MsSqlServerSagaScriptWriter(textWriter, saga));
            }
            if (sqlVariant == BuildSqlVariant.MySql)
            {
                return(new MySqlSagaScriptWriter(textWriter, saga));
            }
            if (sqlVariant == BuildSqlVariant.Oracle)
            {
                return(new OracleSagaScriptWriter(textWriter, saga));
            }

            throw new Exception($"Unknown SqlVariant {sqlVariant}.");
        }
コード例 #11
0
    public static SqlVariant Convert(this BuildSqlVariant sqlVariant)
    {
        switch (sqlVariant)
        {
        case BuildSqlVariant.MsSqlServer:
            return(SqlVariant.MsSqlServer);

        case BuildSqlVariant.MySql:
            return(SqlVariant.MySql);

        case BuildSqlVariant.Oracle:
            return(SqlVariant.Oracle);

        default:
            throw new Exception($"Unknown SqlVariant: {sqlVariant}.");
        }
    }
コード例 #12
0
    public static SagaDefinition GetSagaDefinition(Type sagaType, BuildSqlVariant sqlVariant)
    {
        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 (sqlVariant == BuildSqlVariant.Oracle)
        {
            tableSuffix = tableSuffix.Substring(0, Math.Min(27, tableSuffix.Length));
        }

        return(new SagaDefinition(
                   tableSuffix: tableSuffix,
                   name: sagaType.FullName,
                   correlationProperty: correlationProperty,
                   transitionalCorrelationProperty: transitional));
    }
コード例 #13
0
    public void BuildDropScript(BuildSqlVariant sqlVariant)
    {
        var builder = new StringBuilder();

        using (var writer = new StringWriter(builder))
        {
            TimeoutScriptBuilder.BuildDropScript(writer, sqlVariant);
        }
        var script = builder.ToString();

        if (sqlVariant == BuildSqlVariant.MsSqlServer)
        {
            SqlValidator.Validate(script);
        }
        using (ApprovalResults.ForScenario(sqlVariant))
        {
            Approvals.Verify(script);
        }
    }
コード例 #14
0
 public static void BuildDropScript(TextWriter writer, BuildSqlVariant sqlVariant)
 {
     writer.Write(ResourceReader.ReadResource(sqlVariant, "Subscription.Drop"));
 }
コード例 #15
0
    public static void WriteSagaScripts(string scriptPath, ModuleDefinition moduleDefinition, BuildSqlVariant sqlVariant, Action <string, string> logError)
    {
        var metaDataReader  = new AllSagaDefinitionReader(moduleDefinition);
        var sagasScriptPath = Path.Combine(scriptPath, "Sagas");

        Directory.CreateDirectory(sagasScriptPath);
        var index = 0;

        foreach (var saga in metaDataReader.GetSagas((exception, type) =>
        {
            logError($"Error in '{type.FullName}'. Error:{exception.Message}", type.GetFileName());
        }))
        {
            var sagaFileName      = saga.TableSuffix;
            var maximumNameLength = 244 - sagasScriptPath.Length;
            if (sagaFileName.Length > maximumNameLength)
            {
                sagaFileName = $"{sagaFileName.Substring(0, maximumNameLength)}_{index}";
                index++;
            }
            var createPath = Path.Combine(sagasScriptPath, $"{sagaFileName}_Create.sql");
            File.Delete(createPath);
            using (var writer = File.CreateText(createPath))
            {
                SagaScriptBuilder.BuildCreateScript(saga, sqlVariant, writer);
            }

            var dropPath = Path.Combine(sagasScriptPath, $"{sagaFileName}_Drop.sql");
            File.Delete(dropPath);
            using (var writer = File.CreateText(dropPath))
            {
                SagaScriptBuilder.BuildDropScript(saga, sqlVariant, writer);
            }
        }
    }
コード例 #16
0
    public static IEnumerable <SagaDefinition> GetSagaDefinitions(EndpointConfiguration endpointConfiguration, BuildSqlVariant sqlVariant)
    {
        var sagaTypes = endpointConfiguration.GetScannedSagaTypes().ToArray();

        if (!sagaTypes.Any())
        {
            return(Enumerable.Empty <SagaDefinition>());
        }
        var sagaAssembly = sagaTypes.First().Assembly;
        //Validate the saga definitions using script builder compile-time validation
        var moduleDefinition  = ModuleDefinition.ReadModule(sagaAssembly.Location, new ReaderParameters(ReadingMode.Deferred));
        var compileTimeReader = new AllSagaDefinitionReader(moduleDefinition);
        var exceptions        = new List <Exception>();

        compileTimeReader.GetSagas((e, d) =>
        {
            exceptions.Add(e);
        });
        if (exceptions.Any())
        {
            throw new AggregateException(exceptions);
        }
        return(sagaTypes.Select(sagaType => GetSagaDefinition(sagaType, sqlVariant)));
    }
コード例 #17
0
 public SagaPersisterTests(BuildSqlVariant sqlVariant, string schema)
 {
     this.sqlVariant = sqlVariant;
     this.schema     = schema;
     dbConnection    = GetConnection();
 }
コード例 #18
0
 public static void BuildCreateScript(TextWriter writer, BuildSqlVariant sqlVariant)
 {
     writer.Write(ResourceReader.ReadResource(sqlVariant, "Timeout.Create"));
 }
コード例 #19
0
 static void Write(string testDirectory, BuildSqlVariant variant, string suffix, string script)
 {
     Write(testDirectory, suffix, script, variant.ToString());
 }
コード例 #20
0
 public SubscriptionPersisterTests(BuildSqlVariant sqlVariant, string schema)
 {
     this.sqlVariant = sqlVariant;
     this.schema     = schema;
 }