示例#1
0
        public void Test_Init()
        {
            //act
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            //assert
            Directory.Exists(Path.Combine(_testConfiguration.WorkspacePath, "_init")).ShouldBe(true);
            File.Exists(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "_init"), "README.md")).ShouldBe(true);

            Directory.Exists(Path.Combine(_testConfiguration.WorkspacePath, "_pre")).ShouldBe(true);
            File.Exists(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "_pre"), "README.md")).ShouldBe(true);

            Directory.Exists(Path.Combine(_testConfiguration.WorkspacePath, "v0.00")).ShouldBe(true);
            File.Exists(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v0.00"), "README.md")).ShouldBe(true);

            Directory.Exists(Path.Combine(_testConfiguration.WorkspacePath, "_draft")).ShouldBe(true);
            File.Exists(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "_draft"), "README.md")).ShouldBe(true);

            Directory.Exists(Path.Combine(_testConfiguration.WorkspacePath, "_post")).ShouldBe(true);
            File.Exists(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "_post"), "README.md")).ShouldBe(true);

            Directory.Exists(Path.Combine(_testConfiguration.WorkspacePath, "_erase")).ShouldBe(true);
            File.Exists(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "_erase"), "README.md")).ShouldBe(true);

            File.Exists(Path.Combine(_testConfiguration.WorkspacePath, "README.md")).ShouldBe(true);
            File.Exists(Path.Combine(_testConfiguration.WorkspacePath, "Dockerfile")).ShouldBe(true);
            File.Exists(Path.Combine(_testConfiguration.WorkspacePath, ".gitignore")).ShouldBe(true);
        }
示例#2
0
        public void Test_Run_Database_Already_Updated()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);
            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);
            localVersionService.IncrementMinorVersion(_testConfiguration.WorkspacePath, null);

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, "v1.01", autoCreateDatabase: true);
            var versions = _testDataService.GetAllDbVersions(_testConfiguration.ConnectionString);

            versions.Count.ShouldBe(3);
            versions[0].Version.ShouldBe("v0.00");
            versions[1].Version.ShouldBe("v1.00");
            versions[2].Version.ShouldBe("v1.01");

            migrationService.Run(_testConfiguration.WorkspacePath, "v1.01", autoCreateDatabase: true);
            migrationService.Run(_testConfiguration.WorkspacePath, "v1.01", autoCreateDatabase: true);
            versions.Count.ShouldBe(3);
            versions[0].Version.ShouldBe("v0.00");
            versions[1].Version.ShouldBe("v1.00");
            versions[2].Version.ShouldBe("v1.01");
        }
示例#3
0
        public void Test_Verify()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.00"), $"test_v1_00.sql"), _testDataService.GetSqlForCreateDbObject($"test_v1_00"));

            localVersionService.IncrementMinorVersion(_testConfiguration.WorkspacePath, null);
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.01"), $"test_v1_01.sql"), _testDataService.GetSqlForCreateDbObject($"test_v1_01"));

            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, "v1.01", autoCreateDatabase: true);

            localVersionService.IncrementMinorVersion(_testConfiguration.WorkspacePath, null);
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.02"), $"test_v1_02.sql"), _testDataService.GetSqlForCreateDbObject($"test_v1_02"));

            //act
            migrationService.Run(_testConfiguration.WorkspacePath, "v1.02", autoCreateDatabase: false, verifyOnly: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v1_00").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v1_01").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v1_02").ShouldBeFalse();
        }
示例#4
0
        public void Test_Create_Multiline_Script_With_Terminator_Inside_Statements()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);
            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);

            string sqlFileName    = "Test_Single_Run_Single_Standard";
            string sqlObjectName1 = "Test_Object_1";
            string sqlObjectName2 = "Test_Object_2";
            string sqlObjectName3 = "Test_Object_3";

            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.00"), $"{sqlFileName}.sql"), _testDataService.GetSqlForMultilineWithTerminatorInsideStatements(sqlObjectName1, sqlObjectName2, sqlObjectName3));

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, "v1.00", autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, $"{sqlObjectName1}").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, $"{sqlObjectName2}").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, $"{sqlObjectName3}").ShouldBeTrue();
        }
示例#5
0
        public void Test_Create_Multiline_Script_With_Error_Must_Rollback()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);
            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);

            string sqlFileName    = "Test_Single_Run_Failed_Script_Must_Rollback";
            string sqlObjectName1 = "Test_Object_1";
            string sqlObjectName2 = "Test_Object_2";

            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.00"), $"{sqlFileName}.sql"), _testDataService.GetSqlForMultilineWithError(sqlObjectName1, sqlObjectName2));

            //act
            try
            {
                var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);
                migrationService.Initialize(_testConfiguration.ConnectionString);
                migrationService.Run(_testConfiguration.WorkspacePath, "v1.00", autoCreateDatabase: true);
            }
            catch (Exception ex)
            {
                //used try/catch this instead of Assert.ThrowsException because different vendors
                //throws different exception type and message content
                ex.Message.ShouldNotBeNullOrEmpty();
            }

            //assert
            _testDataService.GetCurrentDbVersion(_testConfiguration.ConnectionString).ShouldBeNull();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, $"{sqlObjectName1}").ShouldBeFalse();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, $"{sqlObjectName2}").ShouldBeFalse();
        }
示例#6
0
        //https://github.com/commandlineparser/commandline
        //https://github.com/dotnet/command-line-api

        public static int Main(string[] args)
        {
            var environmentService      = new EnvironmentService();
            var traceService            = new FileTraceService();
            var localVersionService     = new LocalVersionService(traceService);
            var migrationServiceFactory = new CLI.MigrationServiceFactory(traceService);
            var commandLineService      = new CommandLineService(migrationServiceFactory, localVersionService, environmentService, traceService);

            var resultCode = Parser.Default.ParseArguments <
                InitOption,
                RunOption,
                ListOption,
                NextVersionOption,
                VerifyOption,
                EraseOption,
                BaselineOption,
                RebaseOption>(args)
                             .MapResult(
                (InitOption opts) => Dispatch(commandLineService.RunInitOption, opts, traceService),
                (RunOption opts) => Dispatch(commandLineService.RunMigration, opts, traceService),
                (NextVersionOption opts) => Dispatch(commandLineService.IncrementVersion, opts, traceService),
                (ListOption opts) => Dispatch(commandLineService.RunListOption, opts, traceService),
                (VerifyOption opts) => Dispatch(commandLineService.RunVerify, opts, traceService),
                (EraseOption opts) => Dispatch(commandLineService.RunEraseOption, opts, traceService),
                (BaselineOption opts) => Dispatch(commandLineService.RunBaselineOption, opts, traceService),
                (RebaseOption opts) => Dispatch(commandLineService.RunRebaseOption, opts, traceService),
                (ArchiveOption opts) => Dispatch(commandLineService.RunArchiveOption, opts, traceService),
                errs => 1);

            return(resultCode);
        }
示例#7
0
        public void Test_Run_Fail_Migration_When_Version_Directory_Has_Files()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            var transactionDirectory = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "v0.00", "_transaction")).FullName;

            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v0.00"), $"test_v0_00.sql"), _testDataService.GetSqlForCreateDbObject($"test_v0_00"));

            //act & assert
            try
            {
                var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);
                migrationService.Initialize(_testConfiguration.ConnectionString);
                migrationService.Run(_testConfiguration.WorkspacePath, autoCreateDatabase: true);
            }
            catch (Exception ex)
            {
                //used try/catch this instead of Assert.ThrowsException because different vendors
                //throws different exception type and message content
                ex.Message.ShouldNotBeNullOrEmpty();
            }
        }
示例#8
0
        public void Test_Run_Ok_With_Explicit_Transaction()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);
            var v1_00_transactionDirectory = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "v1.00", "_transaction")).FullName;

            _testDataService.CreateScriptFile(Path.Combine(v1_00_transactionDirectory, $"test_v1_00.sql"), _testDataService.GetSqlForCreateDbObject($"test_v1_00"));

            localVersionService.IncrementMinorVersion(_testConfiguration.WorkspacePath, null);
            var v1_01_transactionDirectory = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "v0.01", "_transaction")).FullName;

            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.01"), $"test_v1_01.sql"), _testDataService.GetSqlForCreateDbObject($"test_v1_01"));

            localVersionService.IncrementMinorVersion(_testConfiguration.WorkspacePath, null);
            var v1_02_transactionDirectory = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "v0.02", "_transaction")).FullName;

            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.02"), $"test_v1_02.sql"), _testDataService.GetSqlForCreateDbObject($"test_v1_02"));

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v1_00").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v1_01").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v1_02").ShouldBeTrue();
        }
示例#9
0
        public void Test_Run_With_Faulty_Script_Throws_Error_Must_Rollback_All_Changes()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.00"), $"test_v1_00.sql"), _testDataService.GetSqlForCreateDbObject($"test_v1_00"));
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.00"), $".sql"), _testDataService.GetSqlForCreateBulkTable("TestCsv"));
            File.Copy(Path.Combine(Path.Combine(Environment.CurrentDirectory, "Core"), "TestCsv.csv"), Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.00"), "TestCsv.csv"));
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.00"), $"test_v1_00_error.sql"), _testDataService.GetSqlForCreateDbObjectWithError($"test_v1_00_error"));

            //act
            try
            {
                var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);
                migrationService.Initialize(_testConfiguration.ConnectionString);
                migrationService.Run(_testConfiguration.WorkspacePath, "v1.00", autoCreateDatabase: true);
            }
            catch (Exception ex)
            {
                //used try/catch this instead of Assert.ThrowsException because different vendors
                //throws different exception type and message content
                ex.Message.ShouldNotBeNullOrEmpty();
            }

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v1_00").ShouldBeFalse();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "TestCsv").ShouldBeFalse();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v1_00_error").ShouldBeFalse();
        }
示例#10
0
        public void Test_Run_Draft_Always_Executed()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v0.00"), $"test_v0_00.sql"), _testDataService.GetSqlForCreateDbObject($"test_v0_00"));

            //act, run with _draft is empty at this point
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, null, autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v0_00").ShouldBeTrue();

            //arrange
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "_draft"), $"test_draft_01.sql"), _testDataService.GetSqlForCreateDbObject($"test_draft_01"));
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "_draft"), $"test_draft_02.sql"), _testDataService.GetSqlForCreateDbObject($"test_draft_02"));

            //act - runs again with _draft holding two script files
            migrationService.Run(_testConfiguration.WorkspacePath, null, autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_draft_01").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_draft_02").ShouldBeTrue();
        }
        public void Test_Bulk_Import_With_Null_Word_Value()
        {
            //arrange - prepare bulk destination table
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);
            string v100Directory = Path.Combine(_testConfiguration.WorkspacePath, "v1.00");

            _testDataService.CreateScriptFile(Path.Combine(v100Directory, $"TestCsvNullWordValue.sql"), _testDataService.GetSqlForCreateBulkTable("TestCsvNullWordValue"));

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, "v1.00", autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "TestCsvNullWordValue").ShouldBeTrue();

            //arrange - add new minor version with csv files
            localVersionService.IncrementMinorVersion(_testConfiguration.WorkspacePath, null);
            string v101Directory = Path.Combine(_testConfiguration.WorkspacePath, "v1.01");

            File.Copy(Path.Combine(Path.Combine(Environment.CurrentDirectory, "Core"), "TestCsvNullWordValue.csv"), Path.Combine(v101Directory, "TestCsvNullWordValue.csv"));

            //act
            migrationService.Run(_testConfiguration.WorkspacePath, "v1.01", autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "TestCsvNullWordValue").ShouldBeTrue();

            var results      = _testDataService.GetBulkTestData(_testConfiguration.ConnectionString, "TestCsvNullWordValue");
            var testDataRows = new List <BulkTestDataRow>
            {
                new BulkTestDataRow {
                    FirstName = "Jack", LastName = "Poole", BirthDate = null
                },
                new BulkTestDataRow {
                    FirstName = "Diana", LastName = "Churchill", BirthDate = null
                },
                new BulkTestDataRow {
                    FirstName = "Rebecca", LastName = "Lyman", BirthDate = null
                },
                new BulkTestDataRow {
                    FirstName = "Sam", LastName = "Macdonald", BirthDate = null
                },
                new BulkTestDataRow {
                    FirstName = "Matt", LastName = "Paige", BirthDate = null
                },
            };

            results.Count.ShouldBe(5);
            testDataRows.All(t => results.Exists(r =>
                                                 t.FirstName == r.FirstName &&
                                                 t.LastName == r.LastName &&
                                                 t.BirthDate == r.BirthDate
                                                 )).ShouldBeTrue();
        }
示例#12
0
        public void Test_Run_With_Parameterized_Tokens(string versionFolder, string scriptName)
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, versionFolder), $"{scriptName}.sql"), _testDataService.GetSqlForCreateDbObjectWithTokens($"{scriptName}"));

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            List <KeyValuePair <string, string> > tokens = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("Token1", "Token1Value"),
                new KeyValuePair <string, string>("Token2", "Token2Value"),
                new KeyValuePair <string, string>("Token3", "Token3Value"),
            };

            migrationService.Run(_testConfiguration.WorkspacePath, "v1.00", autoCreateDatabase: true, tokens: tokens);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, $"{scriptName}_Token1Value_Token2Value_Token3Value").ShouldBeTrue();
        }
        public void Test_Run_Environment_Aware_Scripts_Placed_In_Sub_Directory()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            //creare environment-aware directories
            var v00_dev  = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "v0.00", "tables", "_dev")).FullName;
            var v00_test = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "v0.00", "tables", "_test")).FullName;
            var v00_prod = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "v0.00", "tables", "_prod")).FullName;

            _testDataService.CreateScriptFile(Path.Combine(v00_dev, $"v00_dev.sql"), _testDataService.GetSqlForCreateDbObject($"v00_dev"));
            _testDataService.CreateScriptFile(Path.Combine(v00_test, $"v00_test_01.sql"), _testDataService.GetSqlForCreateDbObject($"v00_test_01"));
            _testDataService.CreateScriptFile(Path.Combine(v00_test, $"v00_test_02.sql"), _testDataService.GetSqlForCreateDbObject($"v00_test_02"));
            _testDataService.CreateScriptFile(Path.Combine(v00_test, $"v00_test_03.sql"), _testDataService.GetSqlForCreateDbObject($"v00_test_03"));
            _testDataService.CreateScriptFile(Path.Combine(v00_prod, $"v00_prod.sql"), _testDataService.GetSqlForCreateDbObject($"v00_prod"));

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, "v0.00", autoCreateDatabase: true, environmentCode: "test");

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "v00_dev").ShouldBeFalse();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "v00_test_01").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "v00_test_02").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "v00_test_03").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "v00_prod").ShouldBeFalse();
        }
示例#14
0
        public void Test_Erase()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.00"), $"script1.sql"), _testDataService.GetSqlForCreateDbObject($"script1"));
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.00"), $"script2.sql"), _testDataService.GetSqlForCreateDbObject($"script2"));
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.00"), $"script3.sql"), _testDataService.GetSqlForCreateDbObject($"script3"));

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, "v1.00", autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "script1").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "script2").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "script3").ShouldBeTrue();

            //arrange
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "_erase"), $"erase.sql"), _testDataService.GetSqlForCleanup());

            //act
            migrationService.Erase(_testConfiguration.WorkspacePath);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "script1").ShouldBeFalse();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "script2").ShouldBeFalse();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "script3").ShouldBeFalse();
        }
示例#15
0
        public void Test_Run_With_Missing_Directories_In_Workspace_Must_Throw_Exception()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v0.00"), $"test_v0_00.sql"), _testDataService.GetSqlForCreateDbObject($"test_v0_00"));

            Directory.Delete(Path.Combine(_testConfiguration.WorkspacePath, "_init"), true);
            Directory.Delete(Path.Combine(_testConfiguration.WorkspacePath, "_post"), true);

            //act
            var exception = Assert.ThrowsException <YuniqlMigrationException>(() =>
            {
                var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);
                migrationService.Initialize(_testConfiguration.ConnectionString);
                migrationService.Run(_testConfiguration.WorkspacePath, "v0.00", autoCreateDatabase: true);
            });

            //assert
            exception.Message.Contains("At least one Yuniql directory is missing in your project.").ShouldBeTrue();
            exception.Message.Contains($"{Path.Combine(_testConfiguration.WorkspacePath, "_init")} / Missing").ShouldBeTrue();
            exception.Message.Contains($"{Path.Combine(_testConfiguration.WorkspacePath, "_pre")} / Found").ShouldBeTrue();
            exception.Message.Contains($"{Path.Combine(_testConfiguration.WorkspacePath, "v0.00*")} / Found").ShouldBeTrue();
            exception.Message.Contains($"{Path.Combine(_testConfiguration.WorkspacePath, "_draft")} / Found").ShouldBeTrue();
            exception.Message.Contains($"{Path.Combine(_testConfiguration.WorkspacePath, "_post")} / Missing").ShouldBeTrue();
            exception.Message.Contains($"{Path.Combine(_testConfiguration.WorkspacePath, "_erase")} / Found").ShouldBeTrue();

            _testDataService.CheckIfDbExist(_testConfiguration.ConnectionString).ShouldBeFalse();
        }
        public void Test_Bulk_Import_Mismatch_Columns_But_Not_Nullable()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            string v000Directory = Path.Combine(_testConfiguration.WorkspacePath, "v0.00");

            _testDataService.CreateScriptFile(Path.Combine(v000Directory, $"test_v0_00.sql"), _testDataService.GetSqlForCreateDbObject($"test_v0_00"));
            _testDataService.CreateScriptFile(Path.Combine(v000Directory, $"test_v0_00_TestCsvMismatchColumnNotNullable.sql"), _testDataService.GetSqlForCreateBulkTable("test_v0_00_TestCsvMismatchColumnNotNullable"));
            File.Copy(Path.Combine(Path.Combine(Environment.CurrentDirectory, "Core"), "TestCsvMismatchColumnNotNullable.csv"), Path.Combine(v000Directory, "test_v0_00_TestCsvMismatchColumnNotNullable.csv"));

            //act - bulk load csv files
            try
            {
                var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);
                migrationService.Initialize(_testConfiguration.ConnectionString);
                migrationService.Run(_testConfiguration.WorkspacePath, "v1.00", autoCreateDatabase: true);
            }
            catch (Exception ex)
            {
                ex.Message.ShouldNotBeEmpty();

                //asset all changes were rolled back
                _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v0_00").ShouldBeFalse();
                _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v0_00_TestCsvMismatchColumnNotNullable").ShouldBeFalse();
            }
        }
        public void Test_Bulk_Import_Destination_Table_Does_Not_Exist_Throws_Error()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            //we simulate a importing data into TestCsvBulkTable that doesnt exist
            string v000Directory = Path.Combine(_testConfiguration.WorkspacePath, "v0.00");

            File.Copy(Path.Combine(Path.Combine(Environment.CurrentDirectory, "Core"), "TestCsv.csv"), Path.Combine(v000Directory, "TestCsv.csv"));

            //act - bulk load csv files
            try
            {
                var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);
                migrationService.Initialize(_testConfiguration.ConnectionString);
                migrationService.Run(_testConfiguration.WorkspacePath, "v1.00", autoCreateDatabase: true);
            }
            catch (Exception ex)
            {
                //assert
                ex.Message.ShouldNotBeEmpty();
            }
        }
        public void Test_Bulk_Import_With_Utf8()
        {
            //arrange - pre-create destination bulk tables
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);
            string v100Directory = Path.Combine(_testConfiguration.WorkspacePath, "v1.00");

            _testDataService.CreateScriptFile(Path.Combine(v100Directory, $"TestCsvUtf8.sql"), _testDataService.GetSqlForCreateBulkTable("TestCsvUtf8"));

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, "v1.00", autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "TestCsvUtf8").ShouldBeTrue();

            //arrange - add new version with csv files
            localVersionService.IncrementMinorVersion(_testConfiguration.WorkspacePath, null);
            string v101Directory = Path.Combine(_testConfiguration.WorkspacePath, "v1.01");

            File.Copy(Path.Combine(Path.Combine(Environment.CurrentDirectory, "Core"), "TestCsvUtf8.csv"), Path.Combine(v101Directory, "TestCsvUtf8.csv"));

            //act - bulk load csv files
            migrationService.Run(_testConfiguration.WorkspacePath, "v1.01", autoCreateDatabase: true);

            //assert
            var results      = _testDataService.GetBulkTestData(_testConfiguration.ConnectionString, "TestCsvUtf8");
            var testDataRows = new List <BulkTestDataRow>
            {
                new BulkTestDataRow {
                    FirstName = "Allan", LastName = "Søgaard", BirthDate = new DateTime(1980, 1, 1)
                },
                new BulkTestDataRow {
                    FirstName = "Martin", LastName = "Bæk", BirthDate = new DateTime(1980, 1, 1)
                },
                new BulkTestDataRow {
                    FirstName = "Gitte", LastName = "Jürgensen", BirthDate = new DateTime(1980, 1, 1)
                },
                new BulkTestDataRow {
                    FirstName = "Maria", LastName = "Østergård", BirthDate = new DateTime(1980, 1, 1)
                },
                new BulkTestDataRow {
                    FirstName = "Peter", LastName = "Langkjær", BirthDate = new DateTime(1980, 1, 1)
                },
            };

            results.Count.ShouldBe(5);
            testDataRows.All(t => results.Exists(r =>
                                                 t.FirstName == r.FirstName &&
                                                 t.LastName == r.LastName &&
                                                 t.BirthDate == r.BirthDate
                                                 )).ShouldBeTrue();
        }
示例#19
0
        public void Test_Vnext_Major_Version()
        {
            //act
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);
            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);

            //assert
            Directory.Exists(Path.Combine(_testConfiguration.WorkspacePath, "v1.00")).ShouldBe(true);
        }
示例#20
0
        public void Test_Run_Ok_Migration_With_Force_Continue_After_Failue_Option_Enabled()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v0.00"), $"test_v0_00_01.sql"), _testDataService.GetSqlForCreateDbObjectWithError($"test_v0_00_01"));
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v0.00"), $"test_v0_00_02.sql"), _testDataService.GetSqlForCreateDbObject($"test_v0_00_02"));
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v0.00"), $"test_v0_00_03.sql"), _testDataService.GetSqlForCreateDbObject($"test_v0_00_03"));

            //act & assert - where the first script has failed
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);

            try
            {
                migrationService.Run(_testConfiguration.WorkspacePath, autoCreateDatabase: true);
            }
            catch (Exception ex)
            {
                //used try/catch this instead of Assert.ThrowsException because different vendors
                //throws different exception type and message content
                ex.Message.ShouldNotBeNullOrEmpty();
            }

            //verrity status of migrations
            var versions = _testDataService.GetAllDbVersions(_testConfiguration.ConnectionString);

            versions.Count.ShouldBe(1);
            versions.Count(s => s.Status == Status.Failed).ShouldBe(1);

            var failedVersion = versions.Single();

            failedVersion.Status.ShouldBe(Status.Failed);
            failedVersion.FailedScriptPath.ShouldBe(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v0.00"), $"test_v0_00_01.sql"));
            failedVersion.FailedScriptError.ShouldNotBeNullOrEmpty();

            //act & assert - where force to continue on failure
            migrationService.Run(_testConfiguration.WorkspacePath, autoCreateDatabase: true, resumeFromFailure: NonTransactionalResolvingOption.ContinueAfterFailure);

            //verrity status of migrations
            var retriedVersions = _testDataService.GetAllDbVersions(_testConfiguration.ConnectionString);

            retriedVersions.Count.ShouldBe(1);
            retriedVersions.Count(s => s.Status == Status.Successful).ShouldBe(1);

            var version = retriedVersions.Single();

            version.Status.ShouldBe(Status.Successful);
            version.FailedScriptPath.ShouldBeNullOrEmpty();
            version.FailedScriptError.ShouldBeNullOrEmpty();
        }
示例#21
0
        public void Test_Vnext_Minor_Version_With_Template_File()
        {
            //act
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);
            localVersionService.IncrementMinorVersion(_testConfiguration.WorkspacePath, "Test.sql");

            //assert
            Directory.Exists(Path.Combine(_testConfiguration.WorkspacePath, "v0.01")).ShouldBe(true);
            File.Exists(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v0.01"), "Test.sql")).ShouldBe(true);
        }
示例#22
0
        public void Test_Run_All_Version_SubDirectories_Executed()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);
            string v1rootDirectory = Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.00"));

            _testDataService.CreateScriptFile(Path.Combine(v1rootDirectory, $"test_v1_00.sql"), _testDataService.GetSqlForCreateDbObject($"test_v1_00"));;

            string v1level1Directory = Path.Combine(v1rootDirectory, "v1.00-level1");

            Directory.CreateDirectory(v1level1Directory);
            _testDataService.CreateScriptFile(Path.Combine(v1level1Directory, $"test_v1_00_level1.sql"), _testDataService.GetSqlForCreateDbObject($"test_v1_00_level1"));

            string v1level1SubDirectory = Path.Combine(v1level1Directory, "v1.00-level1-sublevel1");

            Directory.CreateDirectory(v1level1SubDirectory);
            _testDataService.CreateScriptFile(Path.Combine(v1level1SubDirectory, $"test_v1_00_level1_sublevel1.sql"), _testDataService.GetSqlForCreateDbObject($"test_v1_00_level1_sublevel1"));

            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);
            string v2rootDirectory = Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v2.00"));

            _testDataService.CreateScriptFile(Path.Combine(v2rootDirectory, $"test_v2_00.sql"), _testDataService.GetSqlForCreateDbObject($"test_v2_00"));;

            string v2level1Directory = Path.Combine(v2rootDirectory, "v2.00-level1");

            Directory.CreateDirectory(v2level1Directory);
            _testDataService.CreateScriptFile(Path.Combine(v2level1Directory, $"test_v2_00_level1.sql"), _testDataService.GetSqlForCreateDbObject($"test_v2_00_level1"));

            string v2level1SubDirectory = Path.Combine(v2level1Directory, "v2.00-level1-sublevel1");

            Directory.CreateDirectory(v2level1SubDirectory);
            _testDataService.CreateScriptFile(Path.Combine(v2level1SubDirectory, $"test_v2_00_level1_sublevel1.sql"), _testDataService.GetSqlForCreateDbObject($"test_v2_00_level1_sublevel1"));

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, "v2.00", autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v1_00").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v1_00_level1").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v1_00_level1_sublevel1").ShouldBeTrue();

            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v2_00").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v2_00_level1").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v2_00_level1_sublevel1").ShouldBeTrue();
        }
示例#23
0
        public void Test_Get_Latest_Version()
        {
            //act
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);
            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);
            localVersionService.IncrementMinorVersion(_testConfiguration.WorkspacePath, null);
            localVersionService.IncrementMinorVersion(_testConfiguration.WorkspacePath, null);

            //assert
            localVersionService.GetLatestVersion(_testConfiguration.WorkspacePath).ShouldBe("v1.02");
        }
示例#24
0
        public void Test_Run_All_NonVersion_Scripts_Executed(string scriptFolder)
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, scriptFolder), $"test_{scriptFolder}.sql"), _testDataService.GetSqlForCreateDbObject($"test_{scriptFolder}"));

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, "v1.00", autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, @$ "test_{scriptFolder}").ShouldBeTrue();
        }
示例#25
0
        public void Test_Run_With_AutocreateDB()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);
            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);
            localVersionService.IncrementMinorVersion(_testConfiguration.WorkspacePath, null);

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, "v1.01", autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbExist(_testConfiguration.ConnectionString).ShouldBeTrue();
        }
示例#26
0
        public void Test_Run_With_User_Custom_Directories_In_Workspace()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);
            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v0.00"), $"test_v0_00.sql"), _testDataService.GetSqlForCreateDbObject($"test_v0_00"));

            Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "user_created_folder"));
            Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "_another_user_created_folder"));

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, "v0.00", autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "test_v0_00").ShouldBeTrue();
        }
示例#27
0
        private IMigrationService CreateNonTransactionalMigrationService(IDataService dataService, IBulkImportService bulkImportService)
        {
            var localVersionService     = new LocalVersionService(_traceService);
            var tokenReplacementService = new TokenReplacementService(_traceService);
            var directoryService        = new DirectoryService();
            var fileService             = new FileService();

            var configurationService = new ConfigurationDataService(dataService, _traceService, tokenReplacementService);

            var migrationService = new NonTransactionalMigrationService(
                localVersionService,
                dataService,
                bulkImportService,
                configurationService,
                tokenReplacementService,
                directoryService,
                fileService,
                _traceService);

            return(migrationService);
        }
示例#28
0
        public void Test_Create_SingleLine_Script_Without_Terminator()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);
            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);

            string sqlObjectName = "Test_Object_1";

            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.00"), $"{sqlObjectName}.sql"), _testDataService.GetSqlForSingleLineWithoutTerminator(sqlObjectName));

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(workingPath: _testConfiguration.WorkspacePath, targetVersion: "v1.00", autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, $"{sqlObjectName}").ShouldBeTrue();
        }
示例#29
0
        public void Test_Run_Without_AutocreateDB_Throws_Exception()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            //act and assert
            try
            {
                var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);
                migrationService.Initialize(_testConfiguration.ConnectionString);
                migrationService.Run(_testConfiguration.WorkspacePath, null, autoCreateDatabase: false);
            }
            catch (Exception ex)
            {
                //used try/catch this instead of Assert.ThrowsException because different vendors
                //throws different exception type and message content
                ex.Message.ShouldNotBeNullOrEmpty();
            }
        }
示例#30
0
        public void Test_Create_SingleLine_Empty_Script()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);
            localVersionService.IncrementMajorVersion(_testConfiguration.WorkspacePath, null);

            string sqlStatement = $@"
";

            _testDataService.CreateScriptFile(Path.Combine(Path.Combine(_testConfiguration.WorkspacePath, "v1.00"), $"Test_Single_Run_Empty.sql"), sqlStatement);

            //act
            var migrationService = _migrationServiceFactory.Create(_testConfiguration.Platform);

            migrationService.Initialize(_testConfiguration.ConnectionString);
            migrationService.Run(_testConfiguration.WorkspacePath, "v1.00", autoCreateDatabase: true);

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "Test_Single_Run_Empty").ShouldBeFalse();
        }