Exemple #1
0
        public void ExecutePostDeploymentScript(string databaseName)
        {
            ProviderConfiguration config =
                (ProviderConfiguration)ConfigurationManager.GetSection("dbTestMonkey/" + ConfigurationSectionName);

            string dacpacPath = ((SqlDatabaseConfiguration)config.Databases[databaseName]).DacPacFilePath;

            _logAction("Loading Dacpac into memory");
            _logAction("current directory:" + Environment.CurrentDirectory);
            _logAction("dacpacPath:" + dacpacPath);

            using (DacPackage dacPackage = DacPackage.Load(dacpacPath, DacSchemaModelStorageType.Memory, FileAccess.Read))
            {
                ExecutePostDeploymentScript(databaseName, dacPackage);
            }
        }
Exemple #2
0
        public void DeployDacPac(string databaseName)
        {
            ProviderConfiguration config =
                (ProviderConfiguration)ConfigurationManager.GetSection("dbTestMonkey/" + ConfigurationSectionName);

            SqlDatabaseConfiguration databaseConfiguration = config.Databases[databaseName];

            string dacpacPath = databaseConfiguration.DacPacFilePath;

            _logAction("Loading Dacpac into memory");
            Stopwatch totalTimer       = Stopwatch.StartNew();
            Stopwatch loadPackageTimer = Stopwatch.StartNew();

            _logAction("current directory:" + Environment.CurrentDirectory);
            _logAction("dacpacPath:" + dacpacPath);

            using (DacPackage dacPackage = DacPackage.Load(dacpacPath, DacSchemaModelStorageType.Memory, FileAccess.Read))
            {
                databaseName = databaseName ?? dacPackage.Name;

                loadPackageTimer.Stop();
                _logAction("Package loaded, initialising DacServices");

                using (IDbConnection connection = _connectionFactory())
                {
                    try
                    {
                        connection.ChangeDatabase(databaseName);
                    }
                    catch
                    {
                        _logAction(
                            "Could not change connection to database " +
                            databaseName +
                            " before pre-deployment script. Database may not yet exist.");
                    }

                    // Execute the DAC pre-deployment script.
                    if (dacPackage.PreDeploymentScript != null)
                    {
                        using (IDbCommand command = connection.CreateCommand())
                        {
                            command.CommandText = new StreamReader(dacPackage.PreDeploymentScript).ReadToEnd();
                            command.CommandText = command.CommandText.Replace("\nGO", "");
                            command.ExecuteNonQuery();
                        }
                    }

                    _logAction("Deploying dacpac");
                    Stopwatch dacpacDeployTimer = Stopwatch.StartNew();

                    DacDeployOptions options = new DacDeployOptions()
                    {
                        CreateNewDatabase = true
                    };

                    Stopwatch   dacpacServiceTimer = Stopwatch.StartNew();
                    DacServices dacServices        = new DacServices(connection.ConnectionString);
                    dacpacServiceTimer.Stop();

                    _logAction("DacServices initialisation took " + dacpacServiceTimer.ElapsedMilliseconds + " ms");

                    dacServices.Message         += dacServices_Message;
                    dacServices.ProgressChanged += dacServices_ProgressChanged;

                    dacServices.Deploy(dacPackage, databaseName, upgradeExisting: true, options: options);

                    dacpacDeployTimer.Stop();

                    _logAction(
                        "Deploying dacpac took " +
                        dacpacDeployTimer.ElapsedMilliseconds +
                        " ms");

                    // If the user has opted to only run the post-deployment script after the DACPAC
                    // deployment and not per-test, it needs to run once.
                    if (!config.Databases[databaseName].ExecutePostDeploymentScriptPerTest)
                    {
                        ExecutePostDeploymentScript(databaseName, dacPackage);
                    }
                }
            }

            totalTimer.Stop();
            _logAction("Total dacpac time was " + totalTimer.ElapsedMilliseconds + " ms");
        }