public void Test_Bulk_Import_With_Default_Separated()
        {
            //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, $"TestCsv.sql"), _testDataService.GetSqlForCreateBulkTable("TestCsv"));

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

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

            //assert
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "TestCsv").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"), "TestCsv.csv"), Path.Combine(v101Directory, "TestCsv.csv"));

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

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

            var results      = _testDataService.GetBulkTestData(_testConfiguration.ConnectionString, "TestCsv");
            var testDataRows = new List <BulkTestDataRow>
            {
                new BulkTestDataRow {
                    FirstName = "Jack", LastName = "Poole", BirthDate = new DateTime(1980, 1, 1)
                },
                new BulkTestDataRow {
                    FirstName = "Diana", LastName = "Churchill", BirthDate = new DateTime(1980, 1, 1)
                },
                new BulkTestDataRow {
                    FirstName = "Rebecca", LastName = "Lyman", BirthDate = new DateTime(1980, 1, 1)
                },
                new BulkTestDataRow {
                    FirstName = "Sam", LastName = "Macdonald", BirthDate = new DateTime(1980, 1, 1)
                },
                new BulkTestDataRow {
                    FirstName = "Matt", LastName = "Paige", 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();
        }
Beispiel #2
0
        public int RunRunOption(RunOption opts)
        {
            try
            {
                //run the migration
                var configuration    = SetupRunConfiguration(opts, isVerifyOnly: false);
                var migrationService = _migrationServiceFactory.Create(configuration.Platform);
                migrationService.Run();

                _traceService.Success($"Schema migration completed successfuly on {configuration.Workspace}.");
                return(0);
            }
            catch (Exception ex)
            {
                return(OnException(ex, "Failed to execute run function", opts.IsDebug));
            }
        }
Beispiel #3
0
        public void Test_Run_Fail_Migration_When_Version_Directory_Has_Other_Directories()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

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

            //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();
            }
        }
Beispiel #4
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();
        }
Beispiel #5
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();
            }
        }
        public void Test_Run_Environment_Aware_Scripts_All_Directories()
        {
            //arrange
            var localVersionService = new LocalVersionService(_traceService);

            localVersionService.Init(_testConfiguration.WorkspacePath);

            //creare environment-aware directories
            var init_dev  = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "_init", "_dev")).FullName;
            var init_test = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "_init", "_test")).FullName;
            var init_prod = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "_init", "_prod")).FullName;

            _testDataService.CreateScriptFile(Path.Combine(init_dev, $"init_dev.sql"), _testDataService.GetSqlForCreateDbObject($"init_dev"));
            _testDataService.CreateScriptFile(Path.Combine(init_test, $"init_test.sql"), _testDataService.GetSqlForCreateDbObject($"init_test"));
            _testDataService.CreateScriptFile(Path.Combine(init_prod, $"init_prod.sql"), _testDataService.GetSqlForCreateDbObject($"init_prod"));

            var pre_dev  = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "_pre", "_dev")).FullName;
            var pre_test = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "_pre", "_test")).FullName;
            var pre_prod = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "_pre", "_prod")).FullName;

            _testDataService.CreateScriptFile(Path.Combine(pre_dev, $"pre_dev.sql"), _testDataService.GetSqlForCreateDbObject($"pre_dev"));
            _testDataService.CreateScriptFile(Path.Combine(pre_test, $"pre_test.sql"), _testDataService.GetSqlForCreateDbObject($"pre_test"));
            _testDataService.CreateScriptFile(Path.Combine(pre_prod, $"pre_prod.sql"), _testDataService.GetSqlForCreateDbObject($"pre_prod"));

            var v00_dev  = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "v0.00", "_dev")).FullName;
            var v00_test = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "v0.00", "_test")).FullName;
            var v00_prod = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "v0.00", "_prod")).FullName;

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

            var draft_dev  = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "_draft", "_dev")).FullName;
            var draft_test = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "_draft", "_test")).FullName;
            var draft_prod = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "_draft", "_prod")).FullName;

            _testDataService.CreateScriptFile(Path.Combine(draft_dev, $"draft_dev.sql"), _testDataService.GetSqlForCreateDbObject($"draft_dev"));
            _testDataService.CreateScriptFile(Path.Combine(draft_test, $"draft_test.sql"), _testDataService.GetSqlForCreateDbObject($"draft_test"));
            _testDataService.CreateScriptFile(Path.Combine(draft_prod, $"draft_prod.sql"), _testDataService.GetSqlForCreateDbObject($"draft_prod"));

            var post_dev  = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "_post", "_dev")).FullName;
            var post_test = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "_post", "_test")).FullName;
            var post_prod = Directory.CreateDirectory(Path.Combine(_testConfiguration.WorkspacePath, "_post", "_prod")).FullName;

            _testDataService.CreateScriptFile(Path.Combine(post_dev, $"post_dev.sql"), _testDataService.GetSqlForCreateDbObject($"post_dev"));
            _testDataService.CreateScriptFile(Path.Combine(post_test, $"post_test.sql"), _testDataService.GetSqlForCreateDbObject($"post_test"));
            _testDataService.CreateScriptFile(Path.Combine(post_prod, $"post_prod.sql"), _testDataService.GetSqlForCreateDbObject($"post_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, "init_dev").ShouldBeFalse();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "init_test").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "init_prod").ShouldBeFalse();

            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "pre_dev").ShouldBeFalse();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "pre_test").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "pre_prod").ShouldBeFalse();

            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "v00_dev").ShouldBeFalse();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "v00_test").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "v00_prod").ShouldBeFalse();

            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "draft_dev").ShouldBeFalse();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "draft_test").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "draft_prod").ShouldBeFalse();

            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "post_dev").ShouldBeFalse();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "post_test").ShouldBeTrue();
            _testDataService.CheckIfDbObjectExist(_testConfiguration.ConnectionString, "post_prod").ShouldBeFalse();
        }
        public int RunMigration(RunOption opts)
        {
            try
            {
                //if no path provided, we default into current directory
                if (string.IsNullOrEmpty(opts.Path))
                {
                    var workingPath = _environmentService.GetCurrentDirectory();
                    opts.Path = workingPath;
                }
                _traceService.Info($"Started migration from {opts.Path}.");

                //if no target platform provided, we default into sqlserver
                if (string.IsNullOrEmpty(opts.Platform))
                {
                    opts.Platform = _environmentService.GetEnvironmentVariable(ENVIRONMENT_VARIABLE.YUNIQL_TARGET_PLATFORM);
                    if (string.IsNullOrEmpty(opts.Platform))
                    {
                        opts.Platform = SUPPORTED_DATABASES.SQLSERVER;
                    }
                }

                //if no connection string provided, we default into environment variable or throw exception
                if (string.IsNullOrEmpty(opts.ConnectionString))
                {
                    opts.ConnectionString = _environmentService.GetEnvironmentVariable(ENVIRONMENT_VARIABLE.YUNIQL_CONNECTION_STRING);
                }

                //if no target version specified, we capture the latest from local folder structure
                if (string.IsNullOrEmpty(opts.TargetVersion))
                {
                    opts.TargetVersion = _localVersionService.GetLatestVersion(opts.Path);
                    _traceService.Info($"No explicit target version requested. We'll use latest available locally {opts.TargetVersion} on {opts.Path}.");
                }

                //parse tokens
                var tokens = opts.Tokens
                             .Select(t => new KeyValuePair <string, string>(t.Split("=")[0], t.Split("=")[1]))
                             .ToList();

                //run the migration
                var toolName    = "yuniql-cli";
                var toolVersion = this.GetType().Assembly.GetName().Version.ToString();

                var migrationService = _migrationServiceFactory.Create(opts.Platform);
                migrationService.Initialize(opts.ConnectionString, opts.CommandTimeout);
                migrationService.Run(opts.Path,
                                     opts.TargetVersion,
                                     autoCreateDatabase: opts.AutoCreateDatabase,
                                     tokens: tokens,
                                     verifyOnly: false,
                                     bulkSeparator: opts.BulkSeparator,
                                     metaSchemaName: opts.MetaSchema,
                                     metaTableName: opts.MetaTable,
                                     commandTimeout: opts.CommandTimeout,
                                     bulkBatchSize: opts.BulkBatchSize,
                                     appliedByTool: toolName,
                                     appliedByToolVersion: toolVersion,
                                     environmentCode: opts.Environment,
                                     opts.ContinueAfterFailure
                    ? NonTransactionalResolvingOption.ContinueAfterFailure
                    : (NonTransactionalResolvingOption?)null,
                                     opts.NoTransaction,
                                     opts.RequiredClearedDraft);

                _traceService.Success($"Schema migration completed successfuly on {opts.Path}.");
                return(0);
            } catch (Exception ex)
            {
                return(OnException(ex, "Failed to execute run function", opts.Debug, _traceService));
            }
        }
Beispiel #8
0
        public int RunMigration(RunOption opts)
        {
            try
            {
                //if no path provided, we default into current directory
                if (string.IsNullOrEmpty(opts.Path))
                {
                    var workingPath = _environmentService.GetCurrentDirectory();
                    opts.Path = workingPath;
                }
                _traceService.Info($"Started migration from {opts.Path}.");

                //if no target platform provided, we default into sqlserver
                if (string.IsNullOrEmpty(opts.Platform))
                {
                    opts.Platform = _environmentService.GetEnvironmentVariable(ENVIRONMENT_VARIABLE.YUNIQL_TARGET_PLATFORM);
                    if (string.IsNullOrEmpty(opts.Platform))
                    {
                        opts.Platform = SUPPORTED_DATABASES.SQLSERVER;
                    }
                }

                //if no connection string provided, we default into environment variable or throw exception
                if (string.IsNullOrEmpty(opts.ConnectionString))
                {
                    opts.ConnectionString = _environmentService.GetEnvironmentVariable(ENVIRONMENT_VARIABLE.YUNIQL_CONNECTION_STRING);
                }

                //if no target version specified, we capture the latest from local folder structure
                if (string.IsNullOrEmpty(opts.TargetVersion))
                {
                    opts.TargetVersion = _localVersionService.GetLatestVersion(opts.Path);
                    _traceService.Info($"No explicit target version requested. We'll use latest available locally {opts.TargetVersion} on {opts.Path}.");
                }

                //parse tokens
                var tokens = opts.Tokens.Select(t => new KeyValuePair <string, string>(t.Split("=")[0], t.Split("=")[1])).ToList();

                //run the migration
                var toolName    = "yuniql-cli";
                var toolVersion = this.GetType().Assembly.GetName().Version.ToString();

                var migrationService = _migrationServiceFactory.Create(opts.Platform);
                migrationService.Initialize(opts.ConnectionString, opts.CommandTimeout);
                migrationService.Run(
                    opts.Path,
                    opts.TargetVersion,
                    autoCreateDatabase: opts.AutoCreateDatabase,
                    tokens: tokens,
                    verifyOnly: false,
                    delimiter: opts.Delimiter,
                    schemaName: opts.Schema,
                    tableName: opts.Table,
                    commandTimeout: opts.CommandTimeout,
                    batchSize: null,
                    appliedByTool: toolName,
                    appliedByToolVersion: toolVersion,
                    environmentCode: opts.Environment
                    );
            }
            catch (Exception ex)
            {
                _traceService.Error($"Failed to execute run function. {Environment.NewLine}{ex.ToString()}");
                return(1);
            }

            return(0);
        }