public void does_build_merge_for_table_with_no_schema_defined()
 {
     var tableRepository = new TableRepository(GetPath());
     var mergeRepository = new MergeStatementRepository(tableRepository, Path.Combine(Directories.GetSampleSolution(), @"NestedProjects\Nested\ABC\DEF\Script.PostDeploy.sql"));
     var merge = mergeRepository.Get().FirstOrDefault(p => p.Name.Value == "NoSchema");
     Assert.IsNotNull(merge);
 }
 public void does_not_build_merge_for_table_with_no_inline_table()
 {
     var tableRepository = new TableRepository(Path.Combine(Directories.GetSampleSolution(), @"NestedProjects\Nested\bin\Debug\Nested.dacpac"));
     var mergeRepository = new MergeStatementRepository(tableRepository, Path.Combine(Directories.GetSampleSolution(), @"NestedProjects\Nested\ABC\DEF\Script.PostDeploy.sql"));
     var merge = mergeRepository.Get().FirstOrDefault(p => p.Name.Value == "NoInlineTable");
     Assert.IsNull(merge);
 }
 public void can_parse_generated_merge_statement()
 {
     var tableRepository = new TableRepository(Path.Combine(Directories.GetSampleSolution(), @"NestedProjects\Nested\bin\Debug\Nested.dacpac"));
     var mergeRepository = new MergeStatementRepository(tableRepository, Path.Combine(Directories.GetSampleSolution(), @"NestedProjects\Nested\ABC\DEF\Script.PostDeploy.sql"));
     var merge = mergeRepository.Get().FirstOrDefault(p=>p.Name.Value == "TheTable");
     Assert.AreEqual(2, merge.Data.Rows.Count);
     Assert.AreEqual("Ed", merge.Data.Rows[0][1]);
     Assert.AreEqual("Ian", merge.Data.Rows[1][1]);
     Assert.IsTrue(merge.Option.HasDelete);
     Assert.IsTrue(merge.Option.HasUpdate);
     Assert.IsTrue(merge.Option.HasInsert);
 }
        public void file_offset_and_length_are_correct()
        {
            var tableRepository = new TableRepository(Path.Combine(Directories.GetSampleSolution(), @"NestedProjects\Nested\bin\Debug\Nested.dacpac"));
            var mergeRepository = new MergeStatementRepository(tableRepository, Path.Combine(Directories.GetSampleSolution(), @"NestedProjects\Nested\ABC\DEF\Script.PostDeploy.sql"));
            var merge = mergeRepository.Get().FirstOrDefault(p => p.Name.Value == "TheTable");

            using (var reader = new StreamReader(merge.ScriptDescriptor.FilePath))
            {
                var script = reader.ReadToEnd().Substring(merge.ScriptDescriptor.ScriptOffset, merge.ScriptDescriptor.ScriptLength);
                Assert.AreEqual(@"MERGE INTO dbo.TheTable
             AS TARGET
            USING (VALUES (1, 'Ed', 1), (2, 'Ian', 0)) AS SOURCE(Id, name, fun) ON SOURCE.[Id] = TARGET.[Id]
            WHEN NOT MATCHED BY TARGET THEN INSERT (Id, name, fun) VALUES (SOURCE.Id, SOURCE.name, SOURCE.fun)
            WHEN MATCHED AND (NULLIF (SOURCE.fun, TARGET.fun) IS NOT NULL
                  OR NULLIF (SOURCE.name, TARGET.name) IS NOT NULL
                  OR NULLIF (SOURCE.Id, TARGET.Id) IS NOT NULL) THEN UPDATE
            SET TARGET.Id   = SOURCE.Id,
            TARGET.name = SOURCE.name,
            TARGET.fun  = SOURCE.fun
            WHEN NOT MATCHED BY SOURCE THEN DELETE;", script);
            }
        }
        private TreeViewItem GetScriptNode(TableRepository tables, ProjectItem item)
        {
            var node = new TreeViewItem();
            node.Header = item.Name;

            //parse the merge statements...
            var repoitory = new MergeStatementRepository(tables, item.FileNames[0]);
            repoitory.Populate();

            foreach (var merge in repoitory.Get())
            {
                merge.Data.ExtendedProperties["Changed"] = false;
                var mergeNode = new TreeViewItem();
                mergeNode.Header = merge.Name.Value;
                mergeNode.Tag = merge;
                mergeNode.ContextMenu = ProjectItems.Resources["TableContext"] as ContextMenu;
                node.Items.Add(mergeNode);
            }

            node.ContextMenu = ProjectItems.Resources["FileContext"] as ContextMenu;

            if (item.Properties.Item("FullPath") == null)
            {
                MessageBox.Show("Error unable to get propert FullPath on script file, unable to build tree");
                return null;
            }


            node.Tag = new ScriptNodeTag
            {
                ScriptPath = item.Properties.Item("FullPath").Value.ToString(),
                Tables = tables
            };

            return node;
        }
        public void OriginalText_is_set_correctly()
        {
            var tableRepository = new TableRepository(GetPath());
            var mergeRepository = new MergeStatementRepository(tableRepository, Path.Combine(Directories.GetSampleSolution(), @"NestedProjects\Nested\ABC\DEF\Script.PostDeploy.sql"));
            var merge = mergeRepository.Get().FirstOrDefault(p => p.Name.Value == "TheTable");

            
                Assert.AreEqual(@"MERGE INTO dbo.TheTable
 AS TARGET
USING (VALUES (1, 'Ed', 1), (2, 'Ian', 0)) AS SOURCE(Id, name, fun) ON SOURCE.[Id] = TARGET.[Id]
WHEN NOT MATCHED BY TARGET THEN INSERT (Id, name, fun) VALUES (SOURCE.Id, SOURCE.name, SOURCE.fun)
WHEN MATCHED AND (NULLIF (SOURCE.fun, TARGET.fun) IS NOT NULL
                  OR NULLIF (SOURCE.name, TARGET.name) IS NOT NULL
                  OR NULLIF (SOURCE.Id, TARGET.Id) IS NOT NULL) THEN UPDATE 
SET TARGET.Id   = SOURCE.Id,
    TARGET.name = SOURCE.name,
    TARGET.fun  = SOURCE.fun
WHEN NOT MATCHED BY SOURCE THEN DELETE;", merge.ScriptDescriptor.OriginalText);
            
        }
 public void sets_merge_options_to_show_missing_delete_clause()
 {
     var tableRepository = new TableRepository(GetPath());
     var mergeRepository = new MergeStatementRepository(tableRepository, Path.Combine(Directories.GetSampleSolution(), @"NestedProjects\Nested\ABC\DEF\Script.PostDeploy.sql"));
     var merge = mergeRepository.Get().FirstOrDefault(p => p.Name.Value == "NoDelete");
     Assert.IsFalse(merge.Option.HasDelete);
     Assert.IsTrue(merge.Option.HasInsert);
     Assert.IsTrue(merge.Option.HasUpdate);
 }
        public void ignores_other_statements_in_file()
        {
            var tableRepository = new TableRepository(GetPath());
            var mergeRepository = new MergeStatementRepository(tableRepository, Path.Combine(Directories.GetSampleSolution(), @"NestedProjects\Nested\ABC\DEF\Script.NotIncluded.sql"));
            Assert.DoesNotThrow(() =>
            {
                mergeRepository.Get();
            });

            Assert.AreEqual(0, mergeRepository.Get().Count);
        }