Exemplo n.º 1
0
        public void ConvertPrimaryKeyColumn()
        {
            //The table we created above should have a column called Condition2 in it, we will migrate this data to ANO land
            ColumnInfo condition = _columnInfos.Single(c => c.GetRuntimeName().Equals("Condition1"));
            ColumnInfoToANOTableConverter converter = new ColumnInfoToANOTableConverter(condition, _anoConditionTable);
            var ex = Assert.Throws <Exception>(() => converter.ConvertFullColumnInfo((s) => true, new ThrowImmediatelyCheckNotifier())); //say  yes to everything it proposes

            StringAssert.IsMatch(@"Could not perform transformation because column \[(.*)\]\.\.\[.*\]\.\[Condition1\] is not droppable", ex.Message);
        }
Exemplo n.º 2
0
        private void btnFinalise_Click(object sender, EventArgs e)
        {
            //if it is not pushed, push it now
            if (!ANOTable.IsTablePushed())
            {
                ANOTable.PushToANOServerAsNewTable(_columnInfo.Data_type, new ThrowImmediatelyCheckNotifier()
                {
                    ThrowOnWarning = true
                });
                ANOTable.SaveToDatabase();
            }


            ColumnInfoToANOTableConverter converter = new ColumnInfoToANOTableConverter(_columnInfo, ANOTable);

            try
            {
                bool worked;
                //there is no data in the column or the data is makey upey data
                if (dgPreview.Rows.Count == 0 || lblPreviewDataIsFictional.Visible)
                {
                    worked = converter.ConvertEmptyColumnInfo(UserAcceptSql, checksUI1);
                }
                else
                {
                    worked = converter.ConvertFullColumnInfo(UserAcceptSql, checksUI1);
                }

                if (worked)
                {
                    if (Activator.YesNo("successfully changed column to ANO, close form?", "Close form?"))
                    {
                        this.ParentForm.Close();
                    }
                }
            }
            catch (Exception exception)
            {
                checksUI1.OnCheckPerformed(new CheckEventArgs("Failed to complete migration, your table is likely to be in a sorry state now - sorry", CheckResult.Fail, exception));
            }

            //it worked (or didn't!) so notify changes to the TableInfo
            Activator.RefreshBus.Publish(this, new RefreshObjectEventArgs(ColumnInfo.TableInfo));
        }
Exemplo n.º 3
0
        public void ConvertNonPrimaryKeyColumn(string conditionColumn)
        {
            //Value and a list of the rows in which it was found on (e.g. the value 'Fish' was found on row 11, 31, 52 and 501
            Dictionary <object, List <int> > rowsObjectFoundIn = new Dictionary <object, List <int> >();

            var server = DataAccessPortal.GetInstance().ExpectServer(_tableInfo, DataAccessContext.DataLoad);

            //for each object found in the column, record all line numbers that object was seen on
            using (var con = server.GetConnection())
            {
                con.Open();
                DbCommand cmd = server.GetCommand("Select * from " + TableName, con);
                var       r   = cmd.ExecuteReader();

                for (int row = 0; row < 10000 && r.Read(); row++)
                {
                    //we have seen it before
                    if (rowsObjectFoundIn.ContainsKey(r[conditionColumn]))
                    {
                        rowsObjectFoundIn[r[conditionColumn]].Add(row);
                    }
                    else
                    {
                        rowsObjectFoundIn.Add(r[conditionColumn], new List <int>(new[] { row }));
                    }
                }
            }

            //The table we created above should have a column called Condition2 in it, we will migrate this data to ANO land
            ColumnInfo condition = _columnInfos.Single(c => c.GetRuntimeName().Equals(conditionColumn));
            ColumnInfoToANOTableConverter converter = new ColumnInfoToANOTableConverter(condition, _anoConditionTable);

            converter.ConvertFullColumnInfo((s) => true, new AcceptAllCheckNotifier()); //say  yes to everything it proposes

            //refresh the column infos
            ColumnInfo[] columnInfos = _tableInfo.ColumnInfos;

            //there should now be an ANO column in place of Condition2
            var ANOCondition2 = columnInfos.Single(c => c.GetRuntimeName().Equals("ANO" + conditionColumn));

            Assert.AreEqual("varchar(7)", ANOCondition2.GetRuntimeDataType(LoadStage.PostLoad));
            Assert.AreEqual("varchar(4)", ANOCondition2.GetRuntimeDataType(LoadStage.AdjustRaw));  //it should know that it has a different appearance and name in RAW

            Assert.AreEqual("ANO" + conditionColumn, ANOCondition2.GetRuntimeName(LoadStage.PostLoad));
            Assert.AreEqual(conditionColumn, ANOCondition2.GetRuntimeName(LoadStage.AdjustRaw));

            //the old Condition2 column shouldn't exist at all
            Assert.IsFalse(columnInfos.Any(c => c.GetRuntimeName().Equals(conditionColumn)));//this column should be gone

            //Now lets confirm that the data itself was successfully transformed and consistent such that A = X in all locations where it was A before and that Null = Null (i.e. the system didn't decided to anonymise the value Null to Y)
            using (var con = server.GetConnection())
            {
                con.Open();

                DbCommand cmd = server.GetCommand("Select * from " + TableName, con);
                var       r   = cmd.ExecuteReader();

                List <object> objectsFound = new List <object>();

                while (r.Read())
                {
                    objectsFound.Add(r["ANO" + conditionColumn]);
                }

                foreach (List <int> rowsWithIdenticalObjectExpectations in rowsObjectFoundIn.Values)
                {
                    if (rowsWithIdenticalObjectExpectations.Count > 1)
                    {
                        int firstIdx = rowsWithIdenticalObjectExpectations.First();
                        var first    = objectsFound[firstIdx];

                        foreach (int rowExpectedToHaveSameObject in rowsWithIdenticalObjectExpectations.Skip(1).ToArray())
                        {
                            Console.WriteLine("Expect row " + firstIdx + " and row " + rowExpectedToHaveSameObject + " to have the same value (because they had the same value before anonymising)");
                            Assert.AreEqual(first, objectsFound[rowExpectedToHaveSameObject]);
                            Console.WriteLine("They did, the ANO was " + (first == DBNull.Value?"DBNull.Value":first));
                        }
                    }
                }
            }
        }