Walkthrough: Creating an Isolated Database Development Environment

In this walkthrough, you set up an isolated development environment so that each database developer can make and test changes without affecting other members of the team. The isolated database development environment is based on the database project that contains the definitions for all the objects that are in the production database. You create the project, import the database schema from a production database, and configure project settings. Next, you create a data generation plan. Finally, you put the database project and all associated files under version control to share with your team.

You are playing an administrative role in this walkthrough, as you define the environment that each developer on the team will use to do his or her development work. Before each developer finishes a work item, he or she uses Data Generator to put realistic but fake data into the updated schema, and then runs database unit tests to verify that the schema changes work as expected. When work items are completed, each developer will check his or her changes into version control where the team can pick them up. By running tests before changes are checked in, each member of the team minimizes risk to the overall effort.

Tasks illustrated in this walkthrough include:

  1. How to create a database project by using the New Database Project Wizard.

  2. How to run the New Database Project Wizard to import the database schema, specify database project properties, and specify build and deployment properties.

  3. How to examine the results of the import operation.

  4. How to create a data generation plan and customize it to generate more realistic test data.

  5. How to add the project to version control.

Prerequisites

You must have installed Visual Studio Team System Database Edition and have access to the AdventureWorks2008 sample database for SQL Server 2008. To add your project to version control, you must have installed version control software, such as Visual Studio Team System Team Foundation Server or Visual SourceSafe.

For information about how to install the AdventureWorks2008 sample database, see this page on the Codeplex Web site: Microsoft SQL Server Product Samples.

To create a database project

  1. On the File menu, point to New, and click Project.

    The New Project dialog box appears.

  2. In Project types, expand the Database Projects node, and click SQL Server 2008.

  3. In the Templates list, click SQL Server 2008 Wizard.

  4. In Name, type AdvWorksSandbox.

  5. Accept the default Location and Solution Name.

  6. Select the Create directory for solution check box if it is not already selected.

  7. Clear the Add to Source Control check box if it is not already cleared, and click OK.

    The New Database Project Wizard appears.

    Next, you will use the wizard to configure your database project and import the initial database schema.

To configure your project and import a database schema

  1. Click Next after you read the Welcome page.

  2. Verify that A database project to manage changes to a user-defined database is selected.

  3. Under SQL Script Files, verify that By Schema is selected, and click Next.

    Important noteImportant Note:

    You cannot change a project's organization after you create it.

  4. On the Set Database Options page, in Specify the default schema to use for objects you create in your database project, type HumanResources, and click Next.

    Note

    You can change these options after the project has been created. For more information, see How to: Configure Database Properties for Database Projects.

  5. On the Import Database Schema page, select the Import existing schema check box.

  6. In the Source Database connection list, click the connection that corresponds to the server and your AdventureWorks2008 database. If the connection does not already exist, click New Connection to create it. If you do not specify a connection, the database project will be created, but no schema will be imported.

    Note

    You can import a database schema later, if the database project does not already contain database objects. For more information, see How to: Import Database Objects and Settings.

  7. Accept the default options for importing objects and settings, and click Next.

  8. On the Configure Build and Deploy page, in Target connection, click the Edit button, and specify a connection to the database server where you want to create your isolated development environment.

  9. In Target database name, type AdvWorksSandbox if it is not already specified.

  10. In the Default deployment collation list, click Use the collation of my project.

    This step makes the development environment for the target database use the same collation as the database project.

  11. Clear the Block incremental deployment if data loss might occur check box.

    Because the isolated development environment will contain only generated test data, you can allow data to be lost.

  12. Click Finish to create the database project and import the database.

    Your database project is created. This process might take a few minutes.

  13. Review the summary results, and click Finish.

    In the background, Database Edition is still analyzing your database schema. In the status bar, a message appears that displays the number of operations that must complete before your database schema is fully analyzed.

    Next, you will examine the database project that you created.

To examine the resulting project

  1. In Solution Explorer, expand the AdvWorksSandbox node, and expand the Schema Objects child node.

  2. Explore the subnodes that are underneath the Schema Objects node in the hierarchy.

    Solution Explorer contains the files that define the objects that are in the schema of the database from which you imported them.

  3. On the View menu, click Database Schema View.

  4. In Schema View, expand the AdvWorksSandbox node.

  5. Explore the subnodes that are underneath the AdvWorksSandbox node in the hierarchy.

    Schema View contains the objects that are defined in the files that appear in Solution Explorer.

  6. In Solution Explorer, click the AdvWorksSandbox node.

  7. On the Project menu, click AdvWorksSandbox Properties.

  8. On the Properties page, click the Deploy tab.

  9. In Target database name, type AdvWorksSandbox if it is not already specified.

  10. On the File menu, click Save Selected Items.

    Each developer will specify the connection string to the database server where he or she wants to host his or her copy of the database.

To deploy the project to your isolated development environment

  1. In Solution Explorer, click the AdvWorksSandbox node.

  2. If you will deploy this project to the same database server that is running the original AdventureWorks2008 database, you must change a file name to avoid conflicts. If you instead deploy to a server that does not contain the original AdventureWorks2008 database, you can go to step 7.

  3. Expand the Schema Objects folder, expand the Database Level Objects folder, expand the Storage Folder, and expand the Files folder.

  4. Double-click FileStreamDocuments.sqlfile.sql.

    The file opens in the Transact-SQL (T-SQL) editor.

  5. Modify the definition of the file to match the following:

    ALTER DATABASE [$(DatabaseName)]
        ADD FILE (NAME = [FileStreamDocuments], FILENAME = '$(DefaultDataPath)SandboxDocuments') TO FILEGROUP [DocumentFileStreamGroup];
    
  6. On the File menu, click Save FileStreamDocuments.sqlfile.sql.

  7. On the Project menu, click AdvWorksSandbox Properties.

    The Project Properties window appears.

  8. Click the Deploy tab.

  9. In the Deploy action list, click Create a deployment script (.sql) and deploy to the database.

    Important noteImportant Note:

    If you do not change the deploy action, you will only generate the deploy script; nothing will be deployed to the target database.

  10. On the Build menu, click Deploy ProjectName.

    A build script is generated for your database project and then deployed to the database server where you chose to host your isolated development environment. When deployment is completed, Deployment Succeeded appears in the Output window.

    Next, you will specify a data generation plan to fill the isolated development database with realistic test data.

Generate Test Data

Now that you have an isolated database against which you can develop, you need test data that can drive your unit tests. First, you create a default data generation plan, and then you customize that plan to produce more realistic data. In this example, you configure data generation for only a subset of the tables and columns in the database. You will configure data generation for the Employee table and the StateProvince table.

The original data in the StateProvince table is not sensitive; therefore, you can use the original data to populate the isolated development database.

However, you should make the following changes to the default generation plan for the Employee table:

  • Title should be one of the following: Developer, Sr. Developer, Tester, Sr. Tester, Project Manager, Sales Associate, or Sr. Sales Associate.

  • BirthDate must be at least 18 years ago and no earlier than January 1, 1930. This specification matches the constraint on the column in the database.

  • Marital Status must be "M" or "S".

  • Gender must be "M" or "F".

  • HireDate must be on or before today's date and on or after January 1, 2000.

  • VacationHours should have a typical distribution over the range -10 to 160.

  • ModifiedDate must be on or before today's date and on or after January 1, 2000. Most of the records will have been updated recently.

  • AdditionalContactInfo and Demographics in the Person table must come from the source AdventureWorks database.

  • OrganizationalNode in the Employee table must come from the source AdventureWorks database.

To create a data generation plan

  1. In Solution Explorer, expand the AdvWorksSandbox node, and click the Data Generation Plans folder.

  2. On the Project menu, click Add New Item.

    The Add New Item dialog box appears.

  3. In the Categories list, click Data Generation Plan.

  4. In the Templates list, click Data Generation Plan.

  5. In Name, type AWGenPlan.dgen, and then click Add.

    A data generation plan named AWGenPlan is added to the database project and opened in the editor. The database schema is imported into the data generation plan.

    Next, you will modify AWGenPlan to generate more realistic test data.

To restrict data generation to the tables of interest

  1. Right-click in the data generation plan, and click Exclude All Tables From Data Generation.

  2. In the data generation plan, select the check boxes for the following tables: HumanResources.Employee, Person.Person, Person.CountryRegion, and Person.StateProvince.

    Next, you will define the data generation plan for the StateProvince table.

To define the data generation plan for the StateProvince table

  1. Click [Person].[StateProvince] in the data generation plan.

  2. In the Column Details pane, click StateProvinceCode, and set the Generator field to Sequential Data Bound Generator.

    This step indicates that you want to populate this column with the results of a query.

  3. On the View menu, click Properties Window.

  4. In the Properties window, in the Generator section, in the Connection Information property, click the connection that corresponds to the database from which you imported the database schema in a previous procedure.

    This step specifies the database connection that you want to use to run the query that will generate data for this column.

  5. In the Properties window, in the Generator section, in the Select Query property, set the query string to SELECT [StateProvinceCode] FROM [Person].[StateProvince].

  6. In the Column Details pane, click Modified Date.

  7. In the Properties window, in the Generator section, set the Max property to today's date because records could not have been modified in the future.

  8. In the Properties window, in the Generator section, set the Min property to 1/1/2000 12:00:00 AM. For this example, assume that the company started collecting data at the start of the year 2000.

    Next, you will specify the data generation plan for the Person table.

To define the data generation plan for the Person table

  1. Click [Person].[Person] in the data generation plan.

  2. In the Column Details pane, click BusinessEntityID, and set the Generator field to SmallInt.

  3. In the Properties window, in the Generator section, set the Max property to 51 and the Min property to 2.

  4. In the Column Details pane, click PersonType, and set the Generator field to Regular Expression.

  5. In the Properties window, in the Generator section, set the Expression property to (GC|SP|EM|IN|VC|SC).

  6. In the Column Details pane, click AdditionalContactInfo, and set the Generator field to Sequential Data Bound Generator.

    This step indicates that you want to fill this column with the results of a query.

  7. On the View menu, click Properties Window.

  8. In the Properties window, in the Generator section, in the Connection Information property, click the connection that corresponds to the database from which you imported the database schema in a previous procedure.

    This step specifies the database connection that you want to use to run the query that will generate data for this column.

  9. In the Properties window, in the Generator section, in the Query Select Query property, set the query string to SELECT * FROM [Person].[Person].

  10. In the Column Details pane, in the Generator Output field for the AdditionalContactInfo, click [AdditionalContactInfo].

    This step selects the column in the result set that you want to associate with this column.

  11. In the Column Details pane, click Demographics, and set the Generator field to Sequential Data Bound Generator.

    This step indicates that you want to fill this column with the results of a query.

  12. On the View menu, click Properties Window.

  13. In the Properties window, in the Generator section, in the Connection Information property, click the connection that corresponds to the database from which you imported the database schema in a previous procedure.

    This step specifies the database connection that you want to use to run the query that will generate data for this column.

  14. In the Properties window, in the Generator section, in the Query Select Query property, set the query string to SELECT * FROM [Person].[Person].

  15. In the Column Details pane, in the Generator Output field for the Demographics, click [Demographics].

    This step selects the column in the result set that you want to associate with this column.

  16. In the Column Details pane, click Modified Date.

  17. In the Properties window, in the Generator section, set the Max property to today's date because records could not have been modified in the future.

  18. In the Properties window, in the Generator section, set the Min property to 1/1/2000 12:00:00 AM. For this example, assume that the company started collecting data at the start of the year 2000.

    Next, you will specify the data generation plan for the Employee table.

To define the data generation plan for the Employee table

  1. Click [HumanResources].[Employee] in the data generation plan.

  2. In the Column Details pane, click BusinessEntityID, and set the Generator field to SmallInt.

  3. In the Properties window, in the Generator section, set the Max property to 51, set the Min property to 2, and set the Distribution property to Normal.

  4. In the Column Details pane, click JobTitle, and set the Generator field to RegularExpression.

  5. In the Properties window, in the Generator section, set the Expression property to (Developer|Sr\. Developer|Tester|Sr\. Tester|Project Manager|Sales Associate|Sr\. Sales Associate).

  6. In the Column Details pane, click BirthDate.

    By default, the Generator field is set to DateTime2.

  7. In the Properties window, in the Generator section, set the Max property to 18 years before today's date (for example, 10/30/1987 11:59:59 PM). Also set the Min property to 1/1/1930 12:00:00 AM.

  8. In the Column Details pane, click MaritalStatus, and set the Generator field to RegularExpression.

  9. In the Properties window, in the Generator section, set the Expression property to (M|S).

  10. In the Column Details pane, click Gender, and set the Generator field to RegularExpression.

  11. In the Properties window, in the Generator section, set the Expression property to (M|F).

  12. In the Column Details pane, click HireDate.

    By default, the Generator field is set to DateTime2.

  13. In the Properties window, in the Generator section, set the Max property to today's date (for example, 10/30/2006 11:59:59 PM). Also set the Min property to 1/1/2000 12:00:00 AM.

  14. In the Column Details pane, click VacationHours.

    By default, the Generator field is set to SmallInt.

  15. In the Properties window, in the Generator section, set the Distribution property to Normal.

  16. In the Properties window, in the Generator section, set the Max property to 160 and the Min property to -10.

  17. In the Column Details pane, click ModifiedDate.

    By default, the Generator field is set to DateTime2.

  18. In the Properties window, in the Generator section, set the Distribution property to ExponentialInverse. Also set the Max property to today's date (for example, 10/30/2006 11:59:59 PM) and the Min property to 1/1/2000 12:00:00 AM.

  19. In the Column Details pane, click OrganizationalNode, and set the Generator field to Sequential Data Bound Generator. Select the the check box for this column if it is not already selected.

    By taking this step, you indicate that you want to fill this column with the results of a query.

  20. On the View menu, click Properties Window.

  21. In the Properties window, in the Generator section, in the Connection Information property, click the connection that corresponds to the database from which you imported the database schema in a previous procedure.

    By taking this step, you specify the database connection that you want to use to run the query that will generate data for this column.

  22. In the Properties window, in the Generator section, in the Query Select Query property, set the query string to SELECT * FROM [HumanResources].[Employee].

  23. In the Column Details pane, in the Generator Output field for the OrganizationalNode column, click [OrganizationalNode].

    By taking this stepm, you specify the column in the result set that you want to associate with this column.

    Next, you will adjust the number of rows being generated for these tables.

To adjust the number of rows being generated

  1. Click [Person].[CountryRegion] in the data generation plan.

  2. In the Rows to Insert field, type 10.

  3. Click [HumanResources].[Employee] in the data generation plan.

  4. In the Rows to Insert field, type 25.

  5. In the data generation plan, click [Person].[Person].

  6. In Rows to Insert, type 25.

  7. In the data generation plan, click [Person].[StateProvince].

  8. In Related Table, click [Person].[CountryRegion].

  9. In Ratio to Related Table, type 2:1.

    By taking this step, you specify that you want to generate twice as many rows for the [Person].[StateProvince] table as for the [Person].[CountryRegion] table.

  10. On the File menu, click Save AWGenPlan.dgen.

To Preview the Data Generation and Generate Test Data

The HumanResources.Employee table contains triggers that might interfere with data generation if you run data generation more than one time. To perform data generation as part of an automated test run, you must disable (and later enable) those triggers as part of your unit tests.

To run data generation multiple times as part of this procedure, you must follow the procedure To disable and enable the triggers in the Employee table around data generation. To run data generation only one time, follow the procedure To verify the data generation plan results.

To preview the data generation and generate test data

  1. On the Data menu, point to Data Generator, and click Preview Data Generation.

    The Data Generation Preview window appears and shows a sample of the data that will be generated. If the data meets the criteria that you specified, continue to the next step. If you have to adjust the data generation plan, make the appropriate changes, and then repeat this step.

  2. On the Data menu, point to Data Generator, and click Generate Data.

    The Generate Data for Target Database dialog box appears.

  3. In Target Database, create a connection to the AdvWorksSandbox database, and click OK.

  4. When you are asked "Do you want to delete existing data from tables before you generate new data?", click Yes.

    Data is generated according to your specifications. When generation has completed, a green check mark appears in each row to indicate that all data was generated successfully. If data was not generated, errors appear in the Error List window.

  5. Skip to the procedure To verify the data generation plan results.

To disable and enable triggers in the Employee table around data generation

  1. Before you generate data, open the Data menu, point to T-SQL Editor, and click New Query Connection.

  2. If a connection exists for the database in your isolated development environment, go to step 7.

  3. Click New Connection.

    The Connection Properties dialog box appears.

  4. In Server name, type or click the name of the database server to which you deployed the database, such as (local)\SQLExpress.

  5. In Log on to the Server, click Use Windows Authentication.

  6. In Connect to a database, click Select or enter a database name, type AdvWorksSandbox, and click OK.

    The connection is created and added to the list in the Connect to Database dialog box.

  7. In the Connect to Database dialog box, click the connection string that corresponds to the database in your isolated development environment, and click OK.

    The Transact-SQL (T-SQL) editor appears, with an active connection to the AdvWorksSandbox database.

  8. Click OK.

    The Transact-SQL (T-SQL) editor appears.

  9. In the T-SQL editor, type the following T-SQL:

    DISABLE TRIGGER [HumanResources].[dEmployee] ON [HumanResources].[Employee]
    GO
    
  10. On the T-SQL editor toolbar, click Execute SQL.

    On the Messages tab, the following appears:

    The command(s) completed successfully.

  11. Click the window that contains the data generation plan.

  12. On the Data menu, point to Data Generator, and click Preview Data Generation.

    The Data Generation Preview window appears and shows a sample of the data that will be generated. If the data meets the criteria that you specified, continue to the next step. If you have to adjust the data generation plan, make the appropriate changes, and then repeat this step.

  13. On the Data menu, point to Data Generator, and click Generate Data.

    The Generate Data for Target Database dialog box appears.

  14. In Target Database, click the connection to the AdvWorksSandbox database, and click OK.

  15. When you are asked "Do you want to clear the contents of the selected tables before inserting new rows?", click Yes.

    Data is generated according to your specifications. When generation has completed, a green check mark appears in each row to indicate that all data was generated successfully. If data was not generated, errors appear in the Error List window.

  16. Click the T-SQL editor window.

  17. In the T-SQL editor, replace the existing statements with the following T-SQL:

    ENABLE TRIGGER dEmployee on [HumanResources].[Employee];
    GO
    
  18. On the T-SQL editor toolbar, click Execute SQL.

    On the Messages tab, the following appears:

    The command(s) completed successfully.

    The triggers are now enabled.

To verify the data generation plan results

  1. On the Data menu, point to T-SQL Editor, and click New Query.

    The Connect to Database dialog box appears.

  2. If a connection exists for the database in your isolated development environment, go to step 8.

  3. Click New Connection.

    The Connection Properties dialog box appears.

  4. In Server name, type or click the name of the database server to which you deployed the database, such as (local)\SQLExpress.

  5. In Log on to the Server, click Use Windows Authentication.

  6. In Connect to a database, click Select or enter a database name, type AdvWorksSandbox, and click OK.

    The connection is created and added to the list in the Connect to Database dialog box.

  7. In the Connect to Database dialog box, click the connection string that corresponds to the database in your isolated development environment, and click OK.

    The Transact-SQL (T-SQL) editor appears with an active connection to the AdvWorksSandbox database.

  8. In the T-SQL editor, type the following:

    SELECT * from HumanResources.Employee;
    
  9. Press F5 to run the query.

    The results pane appears and displays the results of your data generation.

  10. On the File menu, click Close to close the T-SQL editor.

    You have now defined a database project, imported the schema from a production database, and specified data generation rules to provide realistic test data. You can now put the solution under version control to make it available to the team.

Make the Project Available to the Team

To add your project to version control

  1. In Solution Explorer, click the AdvWorksSandbox node.

  2. On the File menu, point to Source Control, and click Add Solution to Source Control.

    At this point, you interact with the installed version control software. This walkthrough provides steps to add your project to Team Foundation Server. If you want to use different version control software, substitute the equivalent steps. If you are using Team Foundation Server, the Connect to Team Foundation Server dialog box appears.

  3. In Connect to a Team Foundation Server, click the server where the team project to which you want to add your solution is found.

    Note

    If you do not have a team project to which you can add the database project, see Managing Team Projects in Team Explorer.

  4. In Team projects, click the team project to which you want to add the database project, and click OK.

    The Add Solution AdvWorksSandbox to Source Control dialog box appears.

  5. Click OK to accept the default values.

    Your database project and the files that it contains are put under version control. Initially, they are still checked out. You must check them in before other team members can access them.

  6. On the View menu, point to Other Windows, and click Pending Changes.

    The Pending Changes window appears.

  7. In the Comment field, type Initial database project creation, schema import, and data generation.

  8. In the Pending Changes window, click Check In on the toolbar.

    The Check In Progress dialog box appears as the database project and the files that it contains are checked in. The icons in Solution Explorer update to show that the files are checked in to version control.

Next Steps

With the database project checked in to version control, the next step is to establish some quality assurance measures before the team starts to make changes. In the next introductory walkthrough, Walkthrough: Establishing a Baseline for the Isolated Development Environment, you create unit tests and build, deploy, and run them in your isolated database development.

See Also

Tasks

How to: Create Database and Server Projects

Concepts

Starting Team Database Development

An Overview of Database Build and Deployment

An Overview of Database Project Settings

Terminology Overview of Database Edition