public static void Register()
            {
                new Construct(DynamicTypeOperation.Create)
                {
                    Construct = (_) => new DynamicTypeEntity {
                        BaseType = DynamicBaseType.Entity
                    },
                }.Register();

                new ConstructFrom <DynamicTypeEntity>(DynamicTypeOperation.Clone)
                {
                    Construct = (e, _) => {
                        var def    = e.GetDefinition();
                        var result = new DynamicTypeEntity {
                            TypeName = null
                        };
                        result.SetDefinition(def);
                        return(result);
                    },
                }.Register();

                new Execute(DynamicTypeOperation.Save)
                {
                    AllowsNew = true,
                    Lite      = false,
                    Execute   = (e, _) => {
                        if (!e.IsNew)
                        {
                            var old = e.ToLite().Retrieve();
                            if (e.TypeName != old.TypeName)
                            {
                                DynamicSqlMigrationLogic.AddDynamicRename(Replacements.KeyTables, old.TypeName, e.TypeName);
                            }

                            var newDef = e.GetDefinition();
                            var oldDef = old.GetDefinition();

                            var pairs = newDef.Properties
                                        .Join(oldDef.Properties, n => n.UID, o => o.UID, (n, o) => new { n, o })
                                        .Where(a => a.n.Type == a.o.Type);

                            foreach (var a in pairs.Where(a => a.n.Name != a.o.Name))
                            {
                                DynamicSqlMigrationLogic.AddDynamicRename(Replacements.KeyColumnsForTable(old.TypeName),
                                                                          a.o.Name, a.n.Name);
                            }
                        }
                    },
                }.Register();

                new Delete(DynamicTypeOperation.Delete)
                {
                    Delete = (e, _) =>
                    {
                        e.Delete();
                    }
                }.Register();
            }
Beispiel #2
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;
                        }
                    }
                }
Beispiel #3
0
            public static void Register()
            {
                new Construct(DynamicTypeOperation.Create)
                {
                    Construct = (_) => new DynamicTypeEntity {
                        BaseType = DynamicBaseType.Entity
                    },
                }.Register();

                new ConstructFrom <DynamicTypeEntity>(DynamicTypeOperation.Clone)
                {
                    Construct = (e, _) =>
                    {
                        var def    = e.GetDefinition();
                        var result = new DynamicTypeEntity {
                            TypeName = null !, BaseType = e.BaseType
                        };
                        result.SetDefinition(def);
                        return(result);
                    },
                }.Register();

                new Execute(DynamicTypeOperation.Save)
                {
                    CanBeNew      = true,
                    CanBeModified = true,
                    Execute       = (e, _) =>
                    {
                        var newDef = e.GetDefinition();
                        var duplicatePropertyNames = newDef.Properties
                                                     .GroupToDictionary(a => a.Name.ToLower())
                                                     .Where(a => a.Value.Count() > 1)
                                                     .ToList();

                        if (duplicatePropertyNames.Any())
                        {
                            throw new InvalidOperationException(ValidationMessage._0HasSomeRepeatedElements1.NiceToString(e.TypeName, duplicatePropertyNames.Select(a => a.Key.FirstUpper()).Comma(", ")));
                        }

                        if (!e.IsNew)
                        {
                            var old = e.ToLite().RetrieveAndRemember();
                            if (e.TypeName != old.TypeName)
                            {
                                DynamicSqlMigrationLogic.AddDynamicRename(TypeNameKey, old.TypeName, e.TypeName);
                            }

                            if (e.BaseType == DynamicBaseType.ModelEntity)
                            {
                                return;
                            }

                            var oldDef  = old.GetDefinition();
                            var newName = GetTableName(e, newDef);
                            var oldName = GetTableName(old, oldDef);

                            if (newName != oldName)
                            {
                                DynamicSqlMigrationLogic.AddDynamicRename(Replacements.KeyTables, oldName, newName);
                            }

                            var pairs = newDef.Properties
                                        .Join(oldDef.Properties, n => n.UID, o => o.UID, (n, o) => new { n, o })
                                        .Where(a => a.n !.Type == a.o !.Type); /*CSBUG*/

                            {
                                string ColName(DynamicProperty dp) => dp.ColumnName ?? dp.Name;

                                string replacementKey = (e.BaseType != DynamicBaseType.Entity || old.BaseType != DynamicBaseType.Entity) ? UnknownColumnKey : Replacements.KeyColumnsForTable(oldName);
                                foreach (var a in pairs.Where(a => ColName(a.n !) != ColName(a.o !)))
                                {
                                    DynamicSqlMigrationLogic.AddDynamicRename(replacementKey, ColName(a.o !), ColName(a.n !));
                                }
                            }

                            {
                                string replacementKey = (e.BaseType != DynamicBaseType.Entity || old.BaseType != DynamicBaseType.Entity) ? UnknownPropertyKey : PropertyRouteLogic.PropertiesFor.FormatWith(old.TypeName);
                                foreach (var a in pairs.Where(a => a.n !.Name != a.o !.Name))
                                {
                                    DynamicSqlMigrationLogic.AddDynamicRename(replacementKey, a.o !.Name, a.n !.Name);
                                }
                            }
                        }
                    },
                }.Register();