다음을 통해 공유


Programmatic deployment sample

Jamie Laflen, Tech Lead for database unit testing, has put together another great sample showing you how to programmatically deploy your database project using the database unit testing framework.

 

The scenario here is that you can control the deployment of the database project to happen whenever you want it to during the database unit testing lifecycle. This gives you much more flexibility compared to the automatic deployment supported through the database unit testing UI. This can also be used for deploying your database project outside of the context of database unit testing in a programmatic way.

 

The sample is in the form of a console application that supports deployment of your database project through a command-line argument specifying the configuration file.

 

//-----------------------------------------------------------------------

// This file is part of:

// Visual Studio Team Edition for Database Professionals

//

// Copyright (C) Microsoft Corporation. All rights reserved.

//

// This source code is intended only as a supplement to Microsoft

// Development Tools and/or on-line documentation. See these other

// materials for detailed information regarding Microsoft code samples.

//

// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY

// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE

// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A

// PARTICULAR PURPOSE.

//-----------------------------------------------------------------------

using System;

using System.Collections.Generic;

using System.Text;

using System.Diagnostics;

using System.IO;

using System.Configuration;

using Microsoft.VisualStudio.TeamSystem.Data.UnitTesting.Configuration;

using Microsoft.VisualStudio.TeamSystem.Data.UnitTesting;

namespace DeployDBSample

{

    class Program

    {

        FileInfo _configFile;

        [STAThread()]

        static void Main(string[] args)

        {

            try

            {

                ConsoleTraceListener l = new ConsoleTraceListener();

                l.TraceOutputOptions = TraceOptions.Timestamp;

                Trace.Listeners.Add(l);

    Trace.AutoFlush = true;

                Debug.AutoFlush = true;

                Program p = new Program();

                p.Run(args);

            }

            catch (Exception ex)

            {

                Trace.TraceError("Unhandled exception {0}{1}", Environment.NewLine, ex);

            }

        }

        public Program()

        {

        }

        void Run(string[] args)

        {

            if (ValidArgs(args) == false)

            {

                PrintUsage();

                return;

            }

            // Load the config section

            string sectionName = "DatabaseUnitTesting";

            ExeConfigurationFileMap map = new ExeConfigurationFileMap();

            map.ExeConfigFilename = _configFile.FullName;

            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

            DatabaseUnitTestingSection section = config.GetSection(sectionName) as DatabaseUnitTestingSection;

            if (section == null)

            {

                Console.WriteLine("Could not load section {0}", sectionName);

                return;

            }

            // If the Database deployment section is defined, attempt to deploy the DBProject

            if ( section.DatabaseDeployment != null

                && string.IsNullOrEmpty(section.DatabaseDeployment.DatabaseProjectFileName) == false)

            {

                // Deploy the database project

       HelperService s = new HelperService();

                s.Deploy(

                    section.DatabaseDeployment.DatabaseProjectFileName,

                    section.DatabaseDeployment.Configuration,

                    section.PrivilegedContext.Provider,

                    section.PrivilegedContext.ConnectionString);

            }

        }

        #region Console app helpers

        void PrintUsage()

        {

            string exeName = this.GetType().Assembly.Location;

            string usage = string.Format("Usage: {0} configFile", exeName);

            Console.WriteLine(usage);

        }

        bool ValidArgs(string[] args)

        {

            bool isValid = true;

            if (args == null || args.Length != 1)

            {

                return false;

            }

            // The first argument is the configuration file

            _configFile = new FileInfo(args[0]);

            if (_configFile.Exists == false)

            {

                isValid = false;

            }

            return isValid;

        }

        #endregion

        class HelperService : DatabaseTestService

        {

            public void Deploy(

                string dbProjectFileName, string projectConfig,

                string providerName, string connectionString)

            {

                DeployDatabaseProject(dbProjectFileName, projectConfig, providerName, connectionString);

            }

        }

    }

}

 

Take it for a test drive!

 

Jamie Laflen

Sachin Rekhi

Comments

  • Anonymous
    January 09, 2007
    Tiago Pascoal on Team Foundation Server: Restricting Work Item creation based on a role. Tim Murphy...