Beispiel #1
0
        public static void Start(SchemaBuilder sb)
        {
            if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                sb.Include <DynamicRenameEntity>()
                .WithQuery(() => e => new
                {
                    Entity = e,
                    e.Id,
                    e.CreationDate,
                    e.ReplacementKey,
                    e.OldName,
                    e.NewName,
                    IsApplied = e.IsApplied(),
                });

                sb.Include <DynamicSqlMigrationEntity>()
                .WithQuery(() => e => new
                {
                    Entity = e,
                    e.Id,
                    e.CreationDate,
                    e.CreatedBy,
                    e.ExecutionDate,
                    e.ExecutedBy,
                    e.Comment,
                });

                new Graph <DynamicSqlMigrationEntity> .Construct(DynamicSqlMigrationOperation.Create)
                {
                    Construct = args =>
                    {
                        if (DynamicLogic.CodeGenError != null)
                        {
                            throw new InvalidOperationException(DynamicSqlMigrationMessage.PreventingGenerationNewScriptBecauseOfErrorsInDynamicCodeFixErrorsAndRestartServer.NiceToString());
                        }

                        var old = Replacements.AutoReplacement;

                        var lastRenames = Database.Query <DynamicRenameEntity>()
                                          .Where(a => !a.IsApplied())
                                          .OrderBy(a => a.CreationDate)
                                          .ToList();

                        try
                        {
                            if (Replacements.AutoReplacement == null)
                            {
                                Replacements.AutoReplacement = ctx =>
                                {
                                    var currentName =
                                        ctx.ReplacementKey.StartsWith(Replacements.KeyEnumsForTable("")) ? AutoReplacementEnums(ctx):
                                        ctx.ReplacementKey.StartsWith(PropertyRouteLogic.PropertiesFor.FormatWith("")) ? DynamicAutoReplacementsProperties(ctx, lastRenames) :
                                        ctx.ReplacementKey.StartsWith(Replacements.KeyColumnsForTable("")) ? DynamicAutoReplacementsColumns(ctx, lastRenames) :
                                        ctx.ReplacementKey == Replacements.KeyTables ? DynamicAutoReplacementsSimple(ctx, lastRenames, Replacements.KeyTables) :
                                        ctx.ReplacementKey == typeof(OperationSymbol).Name ? DynamicAutoReplacementsOperations(ctx, lastRenames) :
                                        ctx.ReplacementKey == QueryLogic.QueriesKey ? DynamicAutoReplacementsSimple(ctx, lastRenames, DynamicTypeLogic.TypeNameKey) :
                                        DynamicAutoReplacementsSimple(ctx, lastRenames, ctx.ReplacementKey);

                                    if (currentName != null)
                                    {
                                        return(new Replacements.Selection(ctx.OldValue, currentName));
                                    }

                                    return(new Replacements.Selection(ctx.OldValue, null));
                                }
                            }
                            ;

                            var script = Schema.Current.SynchronizationScript(interactive: false, replaceDatabaseName: SqlMigrationRunner.DatabaseNameReplacement);

                            return(new DynamicSqlMigrationEntity
                            {
                                CreationDate = TimeZoneManager.Now,
                                CreatedBy = UserEntity.Current.ToLite(),
                                Script = script?.ToString() ?? "",
                            });
                        }
                        finally
                        {
                            Replacements.AutoReplacement = old;
                        }
                    }
                }