public void GenerateMigrationScripts()
        {
            int count = 0;
            string outputPath = ConfigurationManager.AppSettings["SQLOutputFolder"];
            outputPath = Path.Combine(outputPath, DateTime.Now.ToString("ddMMyyyy"));
            List<string> scriptNames = new List<string>();
            var temp = new List<string>();
            while (count < _destinationDatabase.Tables.Count)
            {
                DestinationTable destinationTable = null;
                SourceTable sourceTable = null;
                try
                {
                    // Get next table to migrate
                    destinationTable = GetNextTableCanMap();
                    if (destinationTable == null)
                        break;

                    Console.WriteLine("Processing " + destinationTable.Name);

                    // Check explicit mapping for source table - destination table
                    TableMappingDefinition mappingDefinition;
                    TableMappingConfiguration mappingConfig = GetTableMappingConfig(destinationTable.Name);
                    sourceTable = GetSourceTable(destinationTable.Name, mappingConfig);
                    if(mappingConfig != null)
                    {
                        mappingDefinition = new TableMappingDefinition(sourceTable, destinationTable, mappingConfig.FieldMappings);
                        mappingDefinition.IsIdentityInsert = mappingConfig.IsIdentityInsert;
                    }
                    else
                    {
                        mappingDefinition = new TableMappingDefinition(sourceTable, destinationTable);
                    }

                    // Retain mapping definition for later use
                    _tableMappingDefinitions.Add(mappingDefinition);

                    // Check circle references needed to update
                    CheckCircleReferences(destinationTable, mappingDefinition);

                    // Generate script
                    var scriptGenerator = new TableScriptGenerator(_sourceDatabase, mappingDefinition);
                    var script = scriptGenerator.GenerateScript();
                    var fileName = string.Format("{0}.{1}-{2}.sql", count++, sourceTable.Name, destinationTable.Name);
                    Utils.SaveToFile(outputPath, fileName, script);
                    scriptNames.Add(fileName);
                    temp.Add(destinationTable.Name);

                    destinationTable.IsMapped = true;
                }
                catch (MigrationException ex)
                {
                    if (ex.ErrorCode == MigrationExceptionCodes.DATABASE_ERROR_TABLE_NOT_FOUND)
                    {
                        destinationTable.IsMapped = true;

                        // TODO: write log
                        Console.WriteLine(destinationTable.Name + " not mapped");
                    }
                }
            }

            // Generate script clear temp database
            var clearScript = string.Format(SqlScriptTemplates.TRUNCATE_TABLE, "[TempDatabase].dbo.[TrackingRecords]");
            var clearFileName = string.Format("{0}.Clear.sql", count);
            Utils.SaveToFile(outputPath, clearFileName, clearScript);
            scriptNames.Add(clearFileName);

            // Generate bat file
            string batScript = BatGenerator.GenerateSqlExecuteScript(scriptNames, _options.ServerName, _options.InstanceName, outputPath);
            Utils.SaveToFile(outputPath, "RunMigration.bat", batScript);
        }
 public TableScriptGenerator(SourceDatabase sourceDatabase, TableMappingDefinition definition)
 {
     _sourceDatabase = sourceDatabase;
     _definition = definition;
 }
        private void CheckCircleReferences(Table table, TableMappingDefinition mappingDefinition)
        {
            if (_tablesToUpdateCircleReferences.ContainsKey(table.Name))
            {
                _tablesToUpdateCircleReferences[table.Name].ForEach(r =>
                {
                    TableMappingDefinition temp = _tableMappingDefinitions.Single(d => d.DestinationTable.Name.Equals(r.OriginTableName));
                    FieldMappingDefinition fieldMappingDefinition = temp.FieldMappingDefinitions.SingleOrDefault(f => f.DestinationField.Name.Equals(r.OriginFieldName));
                    if(fieldMappingDefinition != null)
                    {
                        SourceTable sourceTable = temp.SourceTable;
                        Field sourceField = fieldMappingDefinition.SourceField;
                        DestinationTable destinationTable = temp.DestinationTable;
                        Field destinationField = fieldMappingDefinition.DestinationField;
                        FieldMappingInfo info = new FieldMappingInfo
                        {
                            DestinationField = destinationField,
                            DestinationTable = destinationTable,
                            SourceField = sourceField,
                            SourceTable = sourceTable
                        };

                        mappingDefinition.CircleReferences.Add(info);
                    }
                });
            }
        }