public ExecuteCommandAlterTableName(IActivateItems activator, TableInfo tableInfo) : base(activator, tableInfo)
        {
            if (IsImpossible)
            {
                return;
            }

            _refactorer = new SelectSQLRefactorer();

            if (!_refactorer.IsRefactorable(TableInfo))
            {
                SetImpossible("Cannot rename table because " + _refactorer.GetReasonNotRefactorable(TableInfo));
                return;
            }
        }
Exemple #2
0
        public void Check(ICheckNotifier notifier)
        {
            if (TargetDatabase == null)
            {
                notifier.OnCheckPerformed(new CheckEventArgs("No TargetDatabase has been set", CheckResult.Fail));
            }
            else
            if (!TargetDatabase.Exists())
            {
                notifier.OnCheckPerformed(new CheckEventArgs("TargetDatabase '" + TargetDatabase + "' does not exist", CheckResult.Fail));
            }

            var toMigrateTables = TableInfos.Except(SkippedTables).ToArray();

            if (!toMigrateTables.Any())
            {
                notifier.OnCheckPerformed(new CheckEventArgs("There are no TableInfos selected for anonymisation", CheckResult.Fail));
            }

            try
            {
                var joinInfos = GetJoinInfosRequiredCatalogue();
                notifier.OnCheckPerformed(new CheckEventArgs("Generated Catalogue SQL succesfully", CheckResult.Success));

                foreach (JoinInfo joinInfo in joinInfos)
                {
                    notifier.OnCheckPerformed(new CheckEventArgs("Found required JoinInfo '" + joinInfo + "' that will have to be migrated", CheckResult.Success));
                }

                foreach (Lookup lookup in GetLookupsRequiredCatalogue())
                {
                    notifier.OnCheckPerformed(new CheckEventArgs("Found required Lookup '" + lookup + "' that will have to be migrated", CheckResult.Success));

                    //for each key involved in the lookup
                    foreach (ColumnInfo c in new[] { lookup.ForeignKey, lookup.PrimaryKey, lookup.Description })
                    {
                        //lookup / table has already been migrated
                        if (SkippedTables.Any(t => t.ID == c.TableInfo_ID))
                        {
                            continue;
                        }

                        //make sure that the plan is sensible
                        if (GetPlanForColumnInfo(c).Plan != Plan.PassThroughUnchanged)
                        {
                            notifier.OnCheckPerformed(new CheckEventArgs("ColumnInfo '" + c + "' is part of a Lookup so must PassThroughUnchanged", CheckResult.Fail));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                notifier.OnCheckPerformed(new CheckEventArgs("Failed to generate Catalogue SQL", CheckResult.Fail, ex));
            }

            if (DateColumn != null)
            {
                var dateColumnPlan = GetPlanForColumnInfo(DateColumn);
                if (dateColumnPlan.Plan != Plan.PassThroughUnchanged)
                {
                    if (notifier.OnCheckPerformed(new CheckEventArgs("Plan for " + DateColumn + " must be PassThroughUnchanged", CheckResult.Fail, null, "Set plan to PassThroughUnchanged")))
                    {
                        dateColumnPlan.Plan = Plan.PassThroughUnchanged;
                    }
                }

                //get a count of the number of non lookup used tables
                var usedTables = TableInfos.Except(SkippedTables).Count(t => !t.IsLookupTable());

                if (usedTables > 1)
                {
                    notifier.OnCheckPerformed(
                        new CheckEventArgs(
                            "You cannot have a date based migration because you are trying to migrate " + usedTables +
                            " TableInfos at once", CheckResult.Fail));
                }
            }

            if (Plans.Any(p => p.Value.Plan == Plan.Dilute))
            {
                if (GetIdentifierDumpServer() == null)
                {
                    notifier.OnCheckPerformed(new CheckEventArgs("No default Identifier Dump server has been configured", CheckResult.Fail));
                }
            }

            var refactorer = new SelectSQLRefactorer();

            foreach (ExtractionInformation e in _allExtractionInformations)
            {
                if (!refactorer.IsRefactorable(e))
                {
                    notifier.OnCheckPerformed(new CheckEventArgs("ExtractionInformation '" + e + "' is a not refactorable due to reason:" + refactorer.GetReasonNotRefactorable(e), CheckResult.Fail));
                }
            }

            notifier.OnCheckPerformed(new CheckEventArgs($"Preparing to evaluate {toMigrateTables.Length}' tables ({string.Join(",",toMigrateTables.Select(t=>t.GetFullyQualifiedName()))})", CheckResult.Success));

            foreach (TableInfo tableInfo in toMigrateTables)
            {
                notifier.OnCheckPerformed(new CheckEventArgs("Evaluating TableInfo '" + tableInfo + "'", CheckResult.Success));

                if (TargetDatabase != null && TargetDatabase.ExpectTable(tableInfo.GetRuntimeName()).Exists())
                {
                    notifier.OnCheckPerformed(new CheckEventArgs("Table '" + tableInfo + "' already exists in Database '" + TargetDatabase + "'", CheckResult.Fail));
                }

                var pks = tableInfo.ColumnInfos.Where(c => c.IsPrimaryKey).ToArray();

                if (!pks.Any())
                {
                    notifier.OnCheckPerformed(new CheckEventArgs("TableInfo '" + tableInfo + "' does not have any Primary Keys, it cannot be anonymised", CheckResult.Fail));
                }

                if (tableInfo.IsTableValuedFunction)
                {
                    notifier.OnCheckPerformed(new CheckEventArgs("TableInfo '" + tableInfo + "' is an IsTableValuedFunction so cannot be anonymised", CheckResult.Fail));
                }

                EnsureNotAlreadySharedLocally(notifier, tableInfo);
                EnsureNotAlreadySharedLocally(notifier, Catalogue);
            }

            //check the column level plans
            foreach (var p in Plans.Values)
            {
                p.Check(notifier);
            }
        }