コード例 #1
0
        public void DeployScript(string pathToScript, TestNodes mainTestNode, DatabaseHandler db)
        {
            try
            {
                var msScriptParser = new MsSqlScriptParser();

                var deployTestNode = ObjectComparator.CreateTestNode(new List <TestResult>(), ObjectType.ScriptTests,
                                                                     "Set of tests for Script");

                var scriptFromFile = File.ReadAllText(Path.Combine(pathToScript));

                var parsedScript = msScriptParser.GetScriptArray(scriptFromFile);

                var dbCreated = msScriptParser.ExecuteTransactionScript(parsedScript, db.Database);

                if (dbCreated)
                {
                    return;
                }

                ObjectComparator.AddTestResult($"Creation of Database objects failed. Databaes: {db.Database.ConnectionString}",
                                               ErrorTypes.CreationScriptFailed,
                                               ObjectType.Script,
                                               $"Script: {pathToScript}",
                                               deployTestNode.Results);

                ObjectComparator.SetResultLevel(deployTestNode);

                mainTestNode.Nodes.Add(deployTestNode);

                var resultPath = Settings.SettingsObject.ResultPath;

                Xml xmlCreator = new Xml();

                var xmlContent = xmlCreator.GetXml(mainTestNode);

                xmlCreator.SaveResultTree(resultPath, xmlContent);
                Environment.Exit((int)ExitCodes.ScriptFailed);
            }
            catch (IOException ex)
            {
                Logger.Error(ex, "Unable to finish script file.");
                Environment.Exit((int)ExitCodes.ScriptFailed);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Error when executing script file.");
                Environment.Exit((int)ExitCodes.ScriptFailed);
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            //Create Test Tree
            var mainTestNode = ObjectComparator.CreateTestNode(new List <TestResult>(), ObjectType.Root, "Root node");

            try
            {
                if (args.Length == 4 && args[2].ToLower() == "delete")
                {
                    // Read arguments
                    string       connectionString = args[0];
                    string       databaseType     = args[1].ToLower();
                    string       resultPath       = args[3];
                    DatabaseType dbType           = BaseDatabase.GetDatabaseType(databaseType);
                    _settings = new Settings(connectionString, resultPath, databaseType);
                    try
                    {
                        switch (dbType)
                        {
                        case DatabaseType.SqlServer:
                            var deploy            = new MsSqlDeploy();
                            var connStringBuilder = Settings.GetMsSqlStringBuilder(connectionString);
                            var dbName            = connStringBuilder.InitialCatalog;
                            connStringBuilder.Remove("Initial Catalog");
                            var connStringWithoutCatalog = connStringBuilder.ConnectionString;
                            deploy.DeleteDatabase(dbName, connStringWithoutCatalog, dbType);
                            break;

                        case DatabaseType.MySql:
                            var deploy1                   = new MySqlDeploy();
                            var connStringBuilder1        = Settings.GetMySqlStringBuilder(connectionString);
                            var dbName1                   = connStringBuilder1.Database;
                            var connStringWithoutCatalog1 = connStringBuilder1.ConnectionString;
                            deploy1.DeleteDatabase(dbName1, connStringWithoutCatalog1, dbType);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                    catch (ArgumentOutOfRangeException ex)
                    {
                        Logger.Error(ex, $"Unsupported database type {databaseType}");
                        Environment.Exit((int)ExitCodes.InvalidArguments);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex, $"Unexpected exception occured.");
                        Environment.Exit((int)ExitCodes.UnsupportedDbType);
                    }

                    Environment.Exit((int)ExitCodes.Success);
                }
                else if (args.Length == 4)
                {
                    Logger.Info("Running deploying script task.");
                    // Read arguments
                    string connectionString = args[0];


                    string       databaseType = args[1];
                    DatabaseType dbType       = BaseDatabase.GetDatabaseType(databaseType);

                    string pathToScript = args[2];
                    string resultPath   = args[3];

                    Logger.Debug($"Arguments passed \narg[0]:{connectionString} \narg[1]:{databaseType} \narg[2]:{pathToScript} \narg[3]:{resultPath}.");

                    _settings = new Settings(connectionString, resultPath, databaseType);
                    try
                    {
                        switch (dbType)
                        {
                        case DatabaseType.SqlServer:
                            var deploy            = new MsSqlDeploy();
                            var connStringBuilder = Settings.GetMsSqlStringBuilder(connectionString);
                            var dbName            = connStringBuilder.InitialCatalog;
                            connStringBuilder.Remove("Initial Catalog");
                            var connStringWithoutCatalog = connStringBuilder.ConnectionString;
                            deploy.DeployDatabase(mainTestNode, connectionString, dbName, connStringWithoutCatalog, dbType, pathToScript);
                            break;

                        case DatabaseType.MySql:
                            var deploy1            = new MySqlDeploy();
                            var connStringBuilder1 = Settings.GetMySqlStringBuilder(connectionString);
                            var dbName1            = connStringBuilder1.Database;
                            connStringBuilder1.Remove("Database");
                            var connStringWithoutCatalog1 = connStringBuilder1.ConnectionString;
                            deploy1.DeployDatabase(mainTestNode, connectionString, dbName1, connStringWithoutCatalog1, dbType, pathToScript);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                        Logger.Info("Task successful.");
                        Environment.Exit((int)ExitCodes.Success);
                    }
                    catch (ArgumentOutOfRangeException ex)
                    {
                        Logger.Error(ex, $"Unsupported database type {databaseType}");
                        Environment.Exit((int)ExitCodes.InvalidArguments);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex, "Unexpected exception occured.");
                        Environment.Exit((int)ExitCodes.UnexpectedError);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex,
                             $"Invalid input argument count [{args.Length}]. Exactly arguments 3 required.");
                Environment.Exit((int)ExitCodes.InvalidArguments);
            }
        }