private static StoreMigration GenerateInitialMigration(DesignSchema schema, List <DesignLogRecord> log)
        {
            //if (schema.Changed)
            if (log.Any(r => r.Operation == DesignOperation.ExistingTableChanged))
            {
                schema.VersionKey = Guid.NewGuid();
            }

            var mp = new StoreMigration {
                Version = schema.Version, VersionKey = schema.VersionKey, Created = DateTime.UtcNow
            };

            mp.Status = schema.Tables.Any() ? MigrationStatus.Editing : MigrationStatus.Empty;
            var cmd = new List <MigrationCommand>();

            cmd.Add(new MigrationCommand {
                Operation = MigrationOperation.CreateSchema, SchemaName = schema.Name
            });
            var tables = schema.Tables.OrderBy(t => t.Order);

            foreach (var t in tables)
            {
                var tm = new MigrationCommand {
                    Operation = MigrationOperation.CreateTable, SchemaName = schema.Name, Table = DesignSchemaConvert.ToStoreDefinition(schema, t)
                };
                tm.OperationCode = Enum.GetName(tm.Operation);
                cmd.Add(tm);
            }

            mp.Commands = cmd.ToArray();
            return(mp);
        }
        private static StoreMigration GenerateIncrementalMigration(DesignSchema schema, List <DesignLogRecord> designRecords)
        {
            var result   = new StoreMigration {
            };
            var commands = new List <MigrationCommand>();
            var i        = 0;
            var records  = designRecords.ToList();

            while (i < records.Count)
            {
                switch (records[i].Operation)
                {
                case DesignOperation.CreateTable:
                    ExtractCreateTable(schema, records[i], commands, records);
                    i--;
                    break;

                case DesignOperation.SetSchemaName:
                    GenerateRenameSchemaOperation(schema, records[i], commands, records);
                    break;

                case DesignOperation.SetTableName:
                    GenerateRenameTableOperation(schema, records[i], commands, records);
                    break;

                case DesignOperation.DeleteTable:
                    GenerateDeleteTableOperation(schema, records[i], commands, records);
                    break;

                case DesignOperation.AddColumn:
                    ExtractColumnOperation(schema, records[i], commands, records, MigrationOperation.AddColumn);
                    i--;
                    break;

                case DesignOperation.DeleteColumn:
                    GenerateDeleteColumnOperation(schema, records[i], commands, records);
                    break;

                case DesignOperation.SetColumnName:
                    GenerateRenameColumnOperation(schema, records[i], commands, records);
                    break;

                case DesignOperation.SetColumnType:
                case DesignOperation.SetColumnNullable:
                case DesignOperation.SetColumnReference:
                    ExtractColumnOperation(schema, records[i], commands, records, MigrationOperation.AlterColumn);
                    i--;
                    break;
                }

                i++;
            }

            result.Commands = commands.ToArray();
            // log moved to migration
            //designRecords.Clear();
            return(result);
        }
Exemple #3
0
        //public void LoadSchema()
        //{
        //}

        public void NewSchema()
        {
            Schema = new DesignSchema {
                Name = "New Schema", Version = "1.0", DataContextName = Parameters.DataService, Namespace = Parameters.Namespace, Changed = true, IsNew = true
            };
            SchemaMigrations = new DesignSchemaMigrations();
            var nm = new StoreMigration {
                Version = DesignSchemaMigrations.InitialVersion, Created = DateTime.UtcNow, VersionKey = Guid.NewGuid()
            };

            //SchemaMigrations.Migrations.Add(new DesignMigration { StatusText = "Empty", VersionText = "Initial 1.0" });
            SchemaMigrations.Migrations.Add(new DesignMigration(nm));
            SelectedTable = null;
            ClearLog();
            UpdateLog(DesignOperation.CreateSchema);

            UpdateDiagramTables();
        }
Exemple #4
0
        public bool TryAddMigration(bool major, out string error)
        {
            error = null;

            if (Schema.Changed)
            {
                error = $"Schema should be saved before creating a new migration.";
                return(false);
            }

            if (SchemaMigrations.Migrations.Any() && SchemaMigrations.Migrations.Last().StatusText == Enum.GetName(typeof(MigrationStatus), MigrationStatus.Empty))
            {
                error = $"You already have an empty migration that created recently.";
                return(false);
            }

            if (SchemaMigrations.Migrations.Last().Migration == null)
            {
                error = $"Fatal error. Last migration is empty.";
                return(false);
            }

            var last = SchemaMigrations.Migrations.Last();

            last.Migration.Status = MigrationStatus.Closed;

            var nm = new StoreMigration
            {
                Status  = MigrationStatus.Empty, FromVersion = last.Migration.Version, Version = GetNextVersion(last.Migration.Version, major),
                Created = DateTime.UtcNow
            };

            SchemaMigrations.Migrations.Add(new DesignMigration(nm));
            Schema.Version = nm.Version;

            return(true);
        }
Exemple #5
0
 public DesignMigration(StoreMigration s)
 {
     Migration = s;
     //StatusText = Enum.GetName(typeof(MigrationStatus), s.Status);
     //VersionText = s.Version == DesignSchemaMigrations.InitialVersion ? DesignSchemaMigrations.InitialVersionText : $"{s.FromVersion} -> {s.Version}";
 }
Exemple #6
0
 public string MigrationToString(StoreMigration migration)
 {
     return(_migrationManager.MigrationToString(migration));
 }