Example #1
0
        public void Update_Destination()
        {
            var destinationFields = new string[][]
            {
                new string[] { "Column0", "Column1", "Column2" },
                new string[] { "Row0Value0", "Row0Value1", "Row0Value2" },
                new string[] { "Row1Value0", "Row1Value1", "Row1Value2" },
                new string[] { "Row2Value0", "Row2Value1", "Row2Value2" }
            };

            var destination = XDatabase.FromFields("Destination", destinationFields, "Id");

            var sourceFields = new string[][]
            {
                new string[] { "Id", "Column0", "Column1", "Column2" },
                // Existing record 0 and 1 values will be updated.
                new string[] { destination.Records[0].Id.ToString(), "Row0Value0Update", "Row0Value1Update", "Row0Value2Update" },
                new string[] { destination.Records[1].Id.ToString(), "Row1Value0", "Row1Value1", "Row1Value2" },
                // Existing record 2 will be removed if not present in source.
                // New record will be added.
                new string[] { "e450006a-cda2-46fc-b475-89335a87e2f8", "Row3Value0", "Row3Value1", "Row3Value2" },
                // New record will be added.
                new string[] { "410b0378-8ea5-4a21-8260-9aa929b2a57b", "Row4Value0", "Row4Value1", "Row4Value2" }
            };

            var source = XDatabase.FromFields("Source", sourceFields, "Id");

            ImmutableArray <XRecord> .Builder target;
            bool isDirty = XDatabase.Update(destination, source, out target);

            Assert.True(isDirty);
            Assert.NotNull(target);

            Assert.Equal(4, target.Count);

            Assert.Equal(destination.Records[0].Id.ToString(), target[0].Id.ToString());
            Assert.Equal("Row0Value0Update", target[0].Values[0].Content);
            Assert.Equal("Row0Value1Update", target[0].Values[1].Content);
            Assert.Equal("Row0Value2Update", target[0].Values[2].Content);

            Assert.Equal(destination.Records[1].Id.ToString(), target[1].Id.ToString());
            Assert.Equal("Row1Value0", target[1].Values[0].Content);
            Assert.Equal("Row1Value1", target[1].Values[1].Content);
            Assert.Equal("Row1Value2", target[1].Values[2].Content);

            Assert.Equal("e450006a-cda2-46fc-b475-89335a87e2f8", target[2].Id.ToString());
            Assert.Equal("Row3Value0", target[2].Values[0].Content);
            Assert.Equal("Row3Value1", target[2].Values[1].Content);
            Assert.Equal("Row3Value2", target[2].Values[2].Content);

            Assert.Equal("410b0378-8ea5-4a21-8260-9aa929b2a57b", target[3].Id.ToString());
            Assert.Equal("Row4Value0", target[3].Values[0].Content);
            Assert.Equal("Row4Value1", target[3].Values[1].Content);
            Assert.Equal("Row4Value2", target[3].Values[2].Content);
        }
Example #2
0
        /// <summary>
        /// Update the destination database using data from source database.
        /// </summary>
        /// <param name="project">The project instance.</param>
        /// <param name="destination">The destination database.</param>
        /// <param name="source">The source database.</param>
        public static void UpdateDatabase(this XProject project, XDatabase destination, XDatabase source)
        {
            if (destination != null && source != null)
            {
                ImmutableArray <XRecord> .Builder records;
                bool isDirty = XDatabase.Update(destination, source, out records);

                if (isDirty && records != null)
                {
                    var builder = project.Databases.ToBuilder();
                    var index   = builder.IndexOf(destination);
                    destination.Records = records.ToImmutable();
                    builder[index]      = destination;

                    var previous = project.Databases;
                    var next     = builder.ToImmutable();
                    project?.History?.Snapshot(previous, next, (p) => project.Databases = p);
                    project.Databases = next;
                }
            }
        }