private static void ExtractColumnOperation(DesignSchema schema, DesignLogRecord record, List <MigrationCommand> commands, List <DesignLogRecord> records,
                                                   MigrationOperation op)
        {
            var cid = record.ColumnId;
            var tid = record.TableId;

            // because this is a new column we can use it from schema
            var table = schema.Tables.FirstOrDefault(t => t.Id == tid);

            // table can be deleted after that
            if (table != null)
            {
                var column = table.Columns.FirstOrDefault(c => c.Id == cid);

                // column can be deleted after that
                if (column != null)
                {
                    var tm = new MigrationCommand
                    {
                        Operation  = op,
                        SchemaName = schema.Name,
                        TableName  = table.Name,
                        Column     = DesignSchemaConvert.ToStoreProperty(schema, table, column)
                    };

                    tm.OperationCode = Enum.GetName(tm.Operation);
                    commands.Add(tm);
                }
                else
                {
                    // if column created and deleted in this log time
                    if (records.Any(r => r.ColumnId == cid && r.Operation == DesignOperation.AddColumn))
                    {
                        // remove all log about this colunt
                        var delLogs = records.Where(r => r.ColumnId == cid).ToList();
                        delLogs.ForEach(r => records.Remove(r));
                        return;
                    }
                }
            }

            // if column created in this log time but not deleted
            if (!records.Any(r => r.ColumnId == cid && r.Operation == DesignOperation.DeleteColumn))
            {
                if (records.Any(r => r.ColumnId == cid && r.Operation == DesignOperation.AddColumn))
                {
                    var delLogs = records.Where(r => r.ColumnId == cid).ToList();
                    delLogs.ForEach(r => records.Remove(r));
                    return;
                }
            }

            // Existed previously column - remove all logs except rename/delete
            var colLogs = records.Where(r => r.ColumnId == cid && r.Operation != DesignOperation.SetColumnName && r.Operation != DesignOperation.DeleteColumn).ToList();

            colLogs.ForEach(r => records.Remove(r));
        }
Esempio n. 2
0
        public void LoadFromFile(string path, string fileName)
        {
            var schema = _schemaStorage.LoadSchema(new StorageParameters {
                FileName = fileName, Path = path
            });

            Schema = DesignSchemaConvert.FromStoreSchema(schema);
            ClearChanged();
            ClearLog();
            LoadCloneObjects();
            UpdateDiagramTables();
        }
Esempio n. 3
0
        public void SaveMigrations(string path)
        {
            StoreSchemaMigrations package = _migrationAggregator.GenerateMigrations(Schema, SchemaMigrations, _designRecords);
            var fileName   = Path.Combine(path, GenerateMigrationFileName(path));
            var parameters = new StorageParameters {
                FileName = fileName
            };
            var schema = DesignSchemaConvert.ToStoreSchema(Schema);

            _schemaStorage.SaveMigration(package, parameters);
            SchemaMigrations = DesignSchemaMigrations.FromStoreMigrations(package);
            ClearLog();
        }
        private static void GenerateDeleteColumnOperation(DesignSchema schema, DesignLogRecord record, List <MigrationCommand> commands, List <DesignLogRecord> records)
        {
            var tm = new MigrationCommand
            {
                Operation  = MigrationOperation.DeleteColumn,
                SchemaName = schema.Name,
                TableName  = record.TableName,
                ColumnName = record.ColumnName,
                Column     = DesignSchemaConvert.ToStorePropertyMin(record.OldColumn),
            };

            tm.OperationCode = Enum.GetName(tm.Operation);
            commands.Add(tm);
        }
        private static void ExtractCreateTable(DesignSchema schema, DesignLogRecord record, List <MigrationCommand> commands, List <DesignLogRecord> records)
        {
            var id = record.TableId;

            // because this is a new table we can use it from schema
            var table = schema.Tables.FirstOrDefault(t => t.Id == id);

            // table can be created and then deleted
            if (table != null)
            {
                var tm = new MigrationCommand
                {
                    Operation  = MigrationOperation.CreateTable,
                    SchemaName = schema.Name,
                    Table      = DesignSchemaConvert.ToStoreDefinition(schema, table)
                };

                tm.OperationCode = Enum.GetName(tm.Operation);
                commands.Add(tm);
            }
            //else
            //{
            //    // table deleted - use original name
            //    var first = records.First(r => r.TableId == id);
            //    var originalName = first.OldValue ?? first.TableName;

            //    var tm = new MigrationCommand
            //    {
            //        Operation = MigrationOperation.DeleteTable,
            //        SchemaName = schema.Name,
            //        TableName = originalName,
            //    };

            //    tm.OperationCode = Enum.GetName(tm.Operation);
            //    commands.Add(tm);
            //}

            // remove all logs about this table - only if table was not deleted
            var tableLogs = records.Where(r => r.TableId == id).ToList();

            tableLogs.ForEach(r => records.Remove(r));
        }
Esempio n. 6
0
        public void SaveSchema(string path)
        {
            // Schema.VersionKey set during migration generation
            //if (Schema.Changed)
            //{
            //    Schema.VersionKey = Guid.NewGuid();
            //}

            var fileName   = Path.Combine(path, GenerateFileName(path));
            var parameters = new StorageParameters {
                FileName = fileName
            };
            var schema = DesignSchemaConvert.ToStoreSchema(Schema);

            _schemaStorage.SaveSchema(schema, parameters);

            // set all changed properties
            ClearChanged();

            // clear logs ?
        }
        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);
        }