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));
        }
        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);
        }