private static void GenerateRenameColumnOperation(DesignSchema schema, DesignLogRecord record, List <MigrationCommand> commands, List <DesignLogRecord> records)
        {
            var cid   = record.ColumnId;
            var tid   = record.TableId;
            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  = MigrationOperation.AlterColumnName,
                        SchemaName = schema.Name,
                        TableName  = table.Name,
                        ColumnName = record.OldValue,
                        NewValue   = record.NewValue
                    };

                    tm.OperationCode = Enum.GetName(tm.Operation);
                    commands.Add(tm);
                }
            }
        }
        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));
        }
        private static void GenerateRenameSchemaOperation(DesignSchema schema, DesignLogRecord record, List <MigrationCommand> commands, List <DesignLogRecord> records)
        {
            var tm = new MigrationCommand
            {
                Operation  = MigrationOperation.AlterSchemaName,
                SchemaName = record.OldValue,
                NewValue   = record.NewValue
            };

            tm.OperationCode = Enum.GetName(tm.Operation);
            commands.Add(tm);
        }
        private static void GenerateDeleteTableOperation(DesignSchema schema, DesignLogRecord record, List <MigrationCommand> commands, List <DesignLogRecord> records)
        {
            var tm = new MigrationCommand
            {
                Operation  = MigrationOperation.DeleteTable,
                SchemaName = schema.Name,
                TableName  = record.TableName,
            };

            tm.OperationCode = Enum.GetName(tm.Operation);
            commands.Add(tm);
        }
        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));
        }