Beispiel #1
0
        public MigrationColumnSet(DiscoveredTable from, DiscoveredTable to, IMigrationFieldProcessor migrationFieldProcessor)
        {
            var fromCols = from.DiscoverColumns();
            var toCols   = to.DiscoverColumns();

            migrationFieldProcessor.ValidateFields(fromCols, toCols);

            SourceTable      = from;
            DestinationTable = to;

            PrimaryKeys    = fromCols.Where(c => c.IsPrimaryKey).ToArray();
            FieldsToDiff   = new List <DiscoveredColumn>();
            FieldsToUpdate = new List <DiscoveredColumn>();

            foreach (DiscoveredColumn pk in PrimaryKeys)
            {
                if (!toCols.Any(f => f.GetRuntimeName().Equals(pk.GetRuntimeName(), StringComparison.CurrentCultureIgnoreCase)))
                {
                    throw new MissingFieldException("Column " + pk + " is missing from either the destination table");
                }
            }

            if (!PrimaryKeys.Any())
            {
                throw new Exception("There are no primary keys declared in table " + from);
            }

            //figure out things to migrate and whether they matter to diffing
            foreach (DiscoveredColumn field in fromCols)
            {
                if (
                    field.GetRuntimeName().Equals(SpecialFieldNames.DataLoadRunID, StringComparison.CurrentCultureIgnoreCase) ||
                    field.GetRuntimeName().Equals(SpecialFieldNames.ValidFrom, StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }

                if (!toCols.Any(c => c.GetRuntimeName().Equals(field.GetRuntimeName(), StringComparison.CurrentCultureIgnoreCase)))
                {
                    throw new MissingFieldException("Field " + field + " is missing from destination table");
                }

                migrationFieldProcessor.AssignFieldsForProcessing(field, FieldsToDiff, FieldsToUpdate);
            }
        }
Beispiel #2
0
        public IList <MigrationColumnSet> CreateMigrationColumnSetFromTableInfos(List <ITableInfo> tableInfos, List <ITableInfo> lookupTableInfos, IMigrationFieldProcessor migrationFieldProcessor)
        {
            //treat null values as empty
            tableInfos       = tableInfos ?? new List <ITableInfo>();
            lookupTableInfos = lookupTableInfos ?? new List <ITableInfo>();

            var columnSet = new List <MigrationColumnSet>();

            foreach (var tableInfo in tableInfos.Union(lookupTableInfos))
            {
                var fromTableName = tableInfo.GetRuntimeName(_fromBubble, _namer);
                var toTableName   = tableInfo.GetRuntimeName(_toBubble, _namer);

                DiscoveredTable fromTable = _fromDatabaseInfo.ExpectTable(fromTableName); //Staging doesn't have schema e.g. even if live schema is not dbo STAGING will be

                DiscoveredTable toTable = DataAccessPortal.GetInstance()
                                          .ExpectDatabase(tableInfo, DataAccessContext.DataLoad)
                                          .ExpectTable(toTableName, tableInfo.Schema);

                if (!fromTable.Exists())
                {
                    if (lookupTableInfos.Contains(tableInfo))//its a lookup table which doesn't exist in from (Staging) - nevermind
                    {
                        continue;
                    }
                    else
                    {
                        throw new Exception("Table " + fromTableName + " was not found on on server " + _fromDatabaseInfo.Server + " (Database " + _fromDatabaseInfo + ")"); //its not a lookup table if it isn't in STAGING thats a problem!
                    }
                }
                columnSet.Add(new MigrationColumnSet(fromTable, toTable, migrationFieldProcessor));
            }

            var sorter = new RelationshipTopologicalSort(columnSet.Select(c => c.DestinationTable));
            columnSet = columnSet.OrderBy(s => ((ReadOnlyCollection <DiscoveredTable>)sorter.Order).IndexOf(s.DestinationTable)).ToList();

            return(columnSet);
        }