SlideShare a Scribd company logo
1 of 25
Treating Databases as First-
Class Citizens in Development
  Rob Bagby
  Developer Evangelist
  Microsoft Corporation
  www.RobBagby.com
  Rob.Bagby@Microsoft.com
Agenda
Overview
The Database Project
(Test) Data Generation
Unit Tests
Managing Change
Q&A
Some Important Questions
• Where is the “truth” for my schema?
    – Production?
    – What about bug fixes?
    – What about version next?
    How do I version my databases?
•
    What do I do for test data?
•
    How do I test my database logic?
•
    What tools do I use to differentiate
•
    schemas, data?
Challenge                   Solution
                            • Multiple Truths, stored
• Where’s the truth?
                              offline
                            • The same way I version
• How do I version?
                              source code
• Where do I get test data? • Test Data Generator

• How do I unit test?       • The same way I test
                              source code (with a little
                              SQL love thrown in)
• What tools do I have to   • Schema Compare, Data
  manage change?              Compare, Refactoring
The “Truth” of my schema
• The production DB is only 1 version of the
  truth of your schema
• There may be bug fixes in Q/A right
  now, waiting deployment
• There may be development going on right
  now by multiple developers / teams
Project Model
• The database project represents the
  “truth” of your schema
• The project contains the schema (*.sql
  files)
• Use version control to manage different
  versions
Database Development Lifecycle

     SQL
    Server
   Database
                  Database
                   Project
    Database
     Project
    Template



      SQL
     Script
Database Development Lifecycle
              Edit

            Refactor
            Compare



   Deploy              Build
            Database
             Project




            Data Gen

               Test
            Compare
Version Control Challenge
     class class class AuctionApplication
            AuctionApplication
                  AuctionApplication
                                                          App
     (     (     (
      int id; id;
            int int      id;
      void MethodA();
            void MethodA();
                  string cacheTitle;
            void MethodB();
                  void MethodA();
     )
           )      void MethodB();
                 )



           V3                               Revision History
     V2
V1

     CREATE TABLE dbo.Auction
          ALTER ALTER TABLE dbo.Auction
                 TABLE dbo.Auction                      Database
     (     WITH CHECK CHECK ADD CONSTRAINT
                 WITH ADD CONSTRAINT
      id    INT NOT NULL, KEY (id)
            Au_PK Au_SK UNIQUE (name)
                   PRIMARY
      name VARCHAR(25) NOT NULL,
      start DATETIME NULL,
      len   INT NULL
     )
Manual Versioning
-- version 1 Add table dbo.Auction
IF OBJECT_ID (N'dbo.Auction', N'U') IS NULL
BEGIN
CREATE TABLE dbo.Auction
(
    id    INT NOT NULL,
    name VARCHAR(25) NOT NULL,
    start DATETIME NULL,
    len   INT NULL
)
END
-- version 2 Add PK Au_PK
IF NOT EXISTS (SELECT * FROM sys.key_constraints WHERE name = 'Au_PK' AND type = 'PK')
BEGIN
    ALTER TABLE Auction
    WITH CHECK ADD CONSTRAINT Au_PK PRIMARY KEY (id)
END
-- version 3 Add UC Au_SK
IF NOT EXISTS (SELECT * FROM sys.key_constraints WHERE name = 'Au_SK' AND type = ‘UQ')
BEGIN
    ALTER TABLE Auction
    WITH CHECK ADD CONSTRAINT Au_SK UNIQUE (name)
END
Version Control
                     The “Data dude” approach
     class class class AuctionApplication
            AuctionApplication
                  AuctionApplication
                                                          App
     (     (     (
      int id; id;
            int int      id;
      void MethodA();
            void MethodA();
                  string cacheTitle;
     )      void MethodB();
                  void MethodA();
           )      void MethodB();
                 )



     V2 V3                                  Revision History
V1
     CREATE TABLE TABLE TABLE dbo.Auction
          CREATE dbo.Auction
                 CREATE dbo.Auction                      Logical
     (    (      (
                                                        Database
      id    id
             INT NOT NULL, NULL PRIMARY KEY, KEY,
                  id
                   INT NOT NOT NULL PRIMARY
                        INT
      name name name VARCHAR(25) NOT NULL UNIQUE,
             VARCHAR(25) NOT NULL, NULL,
                   VARCHAR(25) NOT
      start start start DATETIME NULL,
             DATETIME NULL, NULL,
                   DATETIME
      len len NULL NULL NULL
             INT len
                   INT INT
     )    )      )
Deployment
                    The “Data dude” approach
     CREATE TABLE TABLE TABLE dbo.Auction
          CREATE dbo.Auction
                 CREATE dbo.Auction                    Logical
          (      (
     (
                                                      Database
      id    id
             INT NOT NULL, NULL PRIMARY KEY, KEY,
                  id
                   INT NOT NOT NULL PRIMARY
                        INT
      name name name VARCHAR(25) NOT NULL UNIQUE,
             VARCHAR(25) NOT NULL, NULL,
                   VARCHAR(25) NOT
      start start start DATETIME NULL,
             DATETIME NULL, NULL,
                   DATETIME
      len len NULL NULL NULL
             INT len
                   INT INT
     )    )      )



     V2 V3                                Revision History
V1


   New                                               Incremental
                CREATE TABLE dbo.Auction
                    ALTER TABLE dbo.Auction
Deployment                                           Deployment
                (
                     WITH CHECK ADD CONSTRAINT
                 id     INT NOT NULL PRIMARY KEY,
                      Au_SK UNIQUE (name)
                 name VARCHAR(25) NOT NULL UNIQUE,
                 start DATETIME NULL,
                 len    INT NULL
                )
demo
 The Project Model
Agenda
Overview
The Database Project
(Test) Data Generation
Unit Tests
Managing Change
Q&A
Test Data Q & A
• Why not production data?
  – Ahhh, it may be illegal
  – Doesn’t test edge cases
  – Doesn’t address schema changes
• What are our test data requirements?
  – “Random”
  – Deterministic
  – Appropriately distributed
  –…
Test Data Q & A (continued)
• What are the differing needs for test data?
  – Functional Test
  – Load Test
• What are the versioning implications
  – We need differing plan(s) for differing
    schemas
  – We need to version plans along side schemas
(Test) Data Generation in VSTS
Choose the following
• The Tables
• Number of rows (RowCount ratios help)
• The generator for each column
      Out of the box: string, RegEx, data bound, etc.
  –
      Write your own
  –
      Set generator-specific settings
  –
      Set the seed – provides determinism
  –
• The distribution
demo
 (Test) Data Generation
Agenda
Overview
The Database Project
(Test) Data Generation
Unit Tests
Managing Change
Q&A
Database Unit Testing
• Automatically generate unit test stubs for:
  – Stored Procedures, Functions, Triggers
• Test Validation (assertions)
  – T-SQL (server based) Assertions
     • RAISERROR command
  – Client Side Assertions
     • None Empty ResultSet
     • Row Count
     • Execution Time
• Pre and Post Test Scripts
Database Unit Testing
• Automatic Deployment Integration
  – Automatically deploy database project prior to
    running tests
• Data Generation Integration
  – Automatically generate data based on
    generation plan prior to running tests
demo
 Database Unit Tests
Agenda
Overview
The Database Project
(Test) Data Generation
Unit Tests
Managing Change
Q&A
What kind of change can we
            manage?
• Refactoring
• Compare schemas
• Compare data
demo
 Managing Change

More Related Content

What's hot

Avoiding cursors with sql server 2005 tech republic
Avoiding cursors with sql server 2005   tech republicAvoiding cursors with sql server 2005   tech republic
Avoiding cursors with sql server 2005 tech republic
Kaing Menglieng
 
C# Starter L02-Classes and Objects
C# Starter L02-Classes and ObjectsC# Starter L02-Classes and Objects
C# Starter L02-Classes and Objects
Mohammad Shaker
 

What's hot (13)

Avoiding cursors with sql server 2005 tech republic
Avoiding cursors with sql server 2005   tech republicAvoiding cursors with sql server 2005   tech republic
Avoiding cursors with sql server 2005 tech republic
 
Green dao
Green daoGreen dao
Green dao
 
Green dao
Green daoGreen dao
Green dao
 
qooxdoo Form Management
qooxdoo Form Managementqooxdoo Form Management
qooxdoo Form Management
 
C# Starter L02-Classes and Objects
C# Starter L02-Classes and ObjectsC# Starter L02-Classes and Objects
C# Starter L02-Classes and Objects
 
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
 
R Programming: Comparing Objects In R
R Programming: Comparing Objects In RR Programming: Comparing Objects In R
R Programming: Comparing Objects In R
 
Diving into MySQL 5.7: advanced features
Diving into MySQL 5.7: advanced featuresDiving into MySQL 5.7: advanced features
Diving into MySQL 5.7: advanced features
 
Uma Abordagem Orientada a Modelos para Modelagem Conceitual de Banco de Dados
Uma Abordagem Orientada a Modelos para Modelagem Conceitual de Banco de DadosUma Abordagem Orientada a Modelos para Modelagem Conceitual de Banco de Dados
Uma Abordagem Orientada a Modelos para Modelagem Conceitual de Banco de Dados
 
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
Jug trojmiasto 2014.04.24  tricky stuff in java grammar and javacJug trojmiasto 2014.04.24  tricky stuff in java grammar and javac
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursors
 
dbms lab manual
dbms lab manualdbms lab manual
dbms lab manual
 
«Objective-C Runtime в примерах» — Алексей Сторожев, e-Legion
«Objective-C Runtime в примерах» — Алексей Сторожев, e-Legion«Objective-C Runtime в примерах» — Алексей Сторожев, e-Legion
«Objective-C Runtime в примерах» — Алексей Сторожев, e-Legion
 

Viewers also liked

Viewers also liked (6)

PHX - Session #2 Test Driven Development: Improving .NET Application Performa...
PHX - Session #2 Test Driven Development: Improving .NET Application Performa...PHX - Session #2 Test Driven Development: Improving .NET Application Performa...
PHX - Session #2 Test Driven Development: Improving .NET Application Performa...
 
PHX Session #5 : Architecture Without Big Design Up Front (Garibay)
PHX Session #5 : Architecture Without Big Design Up Front (Garibay)PHX Session #5 : Architecture Without Big Design Up Front (Garibay)
PHX Session #5 : Architecture Without Big Design Up Front (Garibay)
 
PHX Session #3 - "It Works on My Machine!" Closing the Loop Between Developme...
PHX Session #3 - "It Works on My Machine!" Closing the Loop Between Developme...PHX Session #3 - "It Works on My Machine!" Closing the Loop Between Developme...
PHX Session #3 - "It Works on My Machine!" Closing the Loop Between Developme...
 
Development Practices & The Microsoft Approach
Development Practices & The Microsoft ApproachDevelopment Practices & The Microsoft Approach
Development Practices & The Microsoft Approach
 
PHX Session #1: Development Best Practices And How Microsoft Helps
PHX Session #1: Development  Best  Practices And  How  Microsoft  HelpsPHX Session #1: Development  Best  Practices And  How  Microsoft  Helps
PHX Session #1: Development Best Practices And How Microsoft Helps
 
VSTS Architecture Edition Overview
VSTS Architecture Edition OverviewVSTS Architecture Edition Overview
VSTS Architecture Edition Overview
 

Similar to Session #4: Treating Databases as First-Class Citizens in Development

on SQL Managment studio(For the following exercise, use the Week 5.pdf
on SQL Managment studio(For the following exercise, use the Week 5.pdfon SQL Managment studio(For the following exercise, use the Week 5.pdf
on SQL Managment studio(For the following exercise, use the Week 5.pdf
formaxekochi
 
扩展世界上最大的图片Blog社区
扩展世界上最大的图片Blog社区扩展世界上最大的图片Blog社区
扩展世界上最大的图片Blog社区
yiditushe
 
Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)
Paulo Gandra de Sousa
 

Similar to Session #4: Treating Databases as First-Class Citizens in Development (20)

TechDays Tunisia - Visual Studio & SQL Server, Better Together - Ayman El-Hattab
TechDays Tunisia - Visual Studio & SQL Server, Better Together - Ayman El-HattabTechDays Tunisia - Visual Studio & SQL Server, Better Together - Ayman El-Hattab
TechDays Tunisia - Visual Studio & SQL Server, Better Together - Ayman El-Hattab
 
SQL Server Development Tools & Processes Using Visual Studio 2010
SQL Server Development Tools & Processes Using Visual Studio 2010 SQL Server Development Tools & Processes Using Visual Studio 2010
SQL Server Development Tools & Processes Using Visual Studio 2010
 
Introduction to SQL Antipatterns
Introduction to SQL AntipatternsIntroduction to SQL Antipatterns
Introduction to SQL Antipatterns
 
Access 04
Access 04Access 04
Access 04
 
Storage Methods for Nonstandard Data Patterns
Storage Methods for Nonstandard Data PatternsStorage Methods for Nonstandard Data Patterns
Storage Methods for Nonstandard Data Patterns
 
Triggers and Stored Procedures
Triggers and Stored ProceduresTriggers and Stored Procedures
Triggers and Stored Procedures
 
Data Warehouse and Business Intelligence - Recipe 3
Data Warehouse and Business Intelligence - Recipe 3Data Warehouse and Business Intelligence - Recipe 3
Data Warehouse and Business Intelligence - Recipe 3
 
Old code doesn't stink
Old code doesn't stinkOld code doesn't stink
Old code doesn't stink
 
Boost delivery stream with code discipline engineering
Boost delivery stream with code discipline engineeringBoost delivery stream with code discipline engineering
Boost delivery stream with code discipline engineering
 
Interactive Kafka Streams
Interactive Kafka StreamsInteractive Kafka Streams
Interactive Kafka Streams
 
Linq
LinqLinq
Linq
 
on SQL Managment studio(For the following exercise, use the Week 5.pdf
on SQL Managment studio(For the following exercise, use the Week 5.pdfon SQL Managment studio(For the following exercise, use the Week 5.pdf
on SQL Managment studio(For the following exercise, use the Week 5.pdf
 
扩展世界上最大的图片Blog社区
扩展世界上最大的图片Blog社区扩展世界上最大的图片Blog社区
扩展世界上最大的图片Blog社区
 
Fotolog: Scaling the World's Largest Photo Blogging Community
Fotolog: Scaling the World's Largest Photo Blogging CommunityFotolog: Scaling the World's Largest Photo Blogging Community
Fotolog: Scaling the World's Largest Photo Blogging Community
 
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMSM.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
 
PoEAA by Example
PoEAA by ExamplePoEAA by Example
PoEAA by Example
 
Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)
 
SQL Saturday 28 - .NET Fundamentals
SQL Saturday 28 - .NET FundamentalsSQL Saturday 28 - .NET Fundamentals
SQL Saturday 28 - .NET Fundamentals
 
A la découverte de TypeScript
A la découverte de TypeScriptA la découverte de TypeScript
A la découverte de TypeScript
 
How do you create a programming language for the JVM?
How do you create a programming language for the JVM?How do you create a programming language for the JVM?
How do you create a programming language for the JVM?
 

More from Steve Lange

Microsoft ALM Platform Overview
Microsoft ALM Platform OverviewMicrosoft ALM Platform Overview
Microsoft ALM Platform Overview
Steve Lange
 
PHX Session #6: More Bang for Your Buck: Getting the Most out of Team Foundat...
PHX Session #6: More Bang for Your Buck: Getting the Most out of Team Foundat...PHX Session #6: More Bang for Your Buck: Getting the Most out of Team Foundat...
PHX Session #6: More Bang for Your Buck: Getting the Most out of Team Foundat...
Steve Lange
 
PHX - Session #4 Treating Databases as First-Class Citizens in Development
PHX - Session #4 Treating Databases as First-Class Citizens in DevelopmentPHX - Session #4 Treating Databases as First-Class Citizens in Development
PHX - Session #4 Treating Databases as First-Class Citizens in Development
Steve Lange
 

More from Steve Lange (20)

Visual Studio ALM 2013 - Edition Comparison
Visual Studio ALM 2013 - Edition ComparisonVisual Studio ALM 2013 - Edition Comparison
Visual Studio ALM 2013 - Edition Comparison
 
Team Foundation Server 2012 Reporting
Team Foundation Server 2012 ReportingTeam Foundation Server 2012 Reporting
Team Foundation Server 2012 Reporting
 
A Deeper Look at Team Foundation Server 2012 Version Control
A Deeper Look at Team Foundation Server 2012 Version ControlA Deeper Look at Team Foundation Server 2012 Version Control
A Deeper Look at Team Foundation Server 2012 Version Control
 
Upgrading to TFS 2010
Upgrading to TFS 2010Upgrading to TFS 2010
Upgrading to TFS 2010
 
Microsoft ALM Platform Overview
Microsoft ALM Platform OverviewMicrosoft ALM Platform Overview
Microsoft ALM Platform Overview
 
Team Foundation Server - Tracking & Reporting
Team Foundation Server - Tracking & ReportingTeam Foundation Server - Tracking & Reporting
Team Foundation Server - Tracking & Reporting
 
Visual Studio 2010 Testing for Developers
Visual Studio 2010 Testing for DevelopersVisual Studio 2010 Testing for Developers
Visual Studio 2010 Testing for Developers
 
Visual Studio LightSwitch (Beta 1) Overview
Visual Studio LightSwitch (Beta 1) OverviewVisual Studio LightSwitch (Beta 1) Overview
Visual Studio LightSwitch (Beta 1) Overview
 
Team Foundation Server 2010 - Overview
Team Foundation Server 2010 - OverviewTeam Foundation Server 2010 - Overview
Team Foundation Server 2010 - Overview
 
Visual Studio 2010 Testing Overview
Visual Studio 2010 Testing OverviewVisual Studio 2010 Testing Overview
Visual Studio 2010 Testing Overview
 
TFS 2010: Team Development on Crack
TFS 2010: Team Development on CrackTFS 2010: Team Development on Crack
TFS 2010: Team Development on Crack
 
Team Foundation Server 2010 - Version Control
Team Foundation Server 2010 - Version ControlTeam Foundation Server 2010 - Version Control
Team Foundation Server 2010 - Version Control
 
Whats New In 2010 (Msdn & Visual Studio)
Whats New In 2010 (Msdn & Visual Studio)Whats New In 2010 (Msdn & Visual Studio)
Whats New In 2010 (Msdn & Visual Studio)
 
PHX Session #6: More Bang for Your Buck: Getting the Most out of Team Foundat...
PHX Session #6: More Bang for Your Buck: Getting the Most out of Team Foundat...PHX Session #6: More Bang for Your Buck: Getting the Most out of Team Foundat...
PHX Session #6: More Bang for Your Buck: Getting the Most out of Team Foundat...
 
PHX - Session #4 Treating Databases as First-Class Citizens in Development
PHX - Session #4 Treating Databases as First-Class Citizens in DevelopmentPHX - Session #4 Treating Databases as First-Class Citizens in Development
PHX - Session #4 Treating Databases as First-Class Citizens in Development
 
Big Event Looping Deck
Big Event Looping DeckBig Event Looping Deck
Big Event Looping Deck
 
Session #6: Get More Bang For Your Buck
Session #6: Get More Bang For Your BuckSession #6: Get More Bang For Your Buck
Session #6: Get More Bang For Your Buck
 
Session #1: Development Practices And The Microsoft Approach
Session #1: Development Practices And The Microsoft ApproachSession #1: Development Practices And The Microsoft Approach
Session #1: Development Practices And The Microsoft Approach
 
Session #3: "It Works on My Machine!" Closing the Loop Between Development & ...
Session #3: "It Works on My Machine!" Closing the Loop Between Development & ...Session #3: "It Works on My Machine!" Closing the Loop Between Development & ...
Session #3: "It Works on My Machine!" Closing the Loop Between Development & ...
 
Session #2: Test Driven Development
Session #2: Test Driven DevelopmentSession #2: Test Driven Development
Session #2: Test Driven Development
 

Recently uploaded

Recently uploaded (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

Session #4: Treating Databases as First-Class Citizens in Development

  • 1. Treating Databases as First- Class Citizens in Development Rob Bagby Developer Evangelist Microsoft Corporation www.RobBagby.com Rob.Bagby@Microsoft.com
  • 2. Agenda Overview The Database Project (Test) Data Generation Unit Tests Managing Change Q&A
  • 3. Some Important Questions • Where is the “truth” for my schema? – Production? – What about bug fixes? – What about version next? How do I version my databases? • What do I do for test data? • How do I test my database logic? • What tools do I use to differentiate • schemas, data?
  • 4. Challenge Solution • Multiple Truths, stored • Where’s the truth? offline • The same way I version • How do I version? source code • Where do I get test data? • Test Data Generator • How do I unit test? • The same way I test source code (with a little SQL love thrown in) • What tools do I have to • Schema Compare, Data manage change? Compare, Refactoring
  • 5. The “Truth” of my schema • The production DB is only 1 version of the truth of your schema • There may be bug fixes in Q/A right now, waiting deployment • There may be development going on right now by multiple developers / teams
  • 6. Project Model • The database project represents the “truth” of your schema • The project contains the schema (*.sql files) • Use version control to manage different versions
  • 7. Database Development Lifecycle SQL Server Database Database Project Database Project Template SQL Script
  • 8. Database Development Lifecycle Edit Refactor Compare Deploy Build Database Project Data Gen Test Compare
  • 9. Version Control Challenge class class class AuctionApplication AuctionApplication AuctionApplication App ( ( ( int id; id; int int id; void MethodA(); void MethodA(); string cacheTitle; void MethodB(); void MethodA(); ) ) void MethodB(); ) V3 Revision History V2 V1 CREATE TABLE dbo.Auction ALTER ALTER TABLE dbo.Auction TABLE dbo.Auction Database ( WITH CHECK CHECK ADD CONSTRAINT WITH ADD CONSTRAINT id INT NOT NULL, KEY (id) Au_PK Au_SK UNIQUE (name) PRIMARY name VARCHAR(25) NOT NULL, start DATETIME NULL, len INT NULL )
  • 10. Manual Versioning -- version 1 Add table dbo.Auction IF OBJECT_ID (N'dbo.Auction', N'U') IS NULL BEGIN CREATE TABLE dbo.Auction ( id INT NOT NULL, name VARCHAR(25) NOT NULL, start DATETIME NULL, len INT NULL ) END -- version 2 Add PK Au_PK IF NOT EXISTS (SELECT * FROM sys.key_constraints WHERE name = 'Au_PK' AND type = 'PK') BEGIN ALTER TABLE Auction WITH CHECK ADD CONSTRAINT Au_PK PRIMARY KEY (id) END -- version 3 Add UC Au_SK IF NOT EXISTS (SELECT * FROM sys.key_constraints WHERE name = 'Au_SK' AND type = ‘UQ') BEGIN ALTER TABLE Auction WITH CHECK ADD CONSTRAINT Au_SK UNIQUE (name) END
  • 11. Version Control The “Data dude” approach class class class AuctionApplication AuctionApplication AuctionApplication App ( ( ( int id; id; int int id; void MethodA(); void MethodA(); string cacheTitle; ) void MethodB(); void MethodA(); ) void MethodB(); ) V2 V3 Revision History V1 CREATE TABLE TABLE TABLE dbo.Auction CREATE dbo.Auction CREATE dbo.Auction Logical ( ( ( Database id id INT NOT NULL, NULL PRIMARY KEY, KEY, id INT NOT NOT NULL PRIMARY INT name name name VARCHAR(25) NOT NULL UNIQUE, VARCHAR(25) NOT NULL, NULL, VARCHAR(25) NOT start start start DATETIME NULL, DATETIME NULL, NULL, DATETIME len len NULL NULL NULL INT len INT INT ) ) )
  • 12. Deployment The “Data dude” approach CREATE TABLE TABLE TABLE dbo.Auction CREATE dbo.Auction CREATE dbo.Auction Logical ( ( ( Database id id INT NOT NULL, NULL PRIMARY KEY, KEY, id INT NOT NOT NULL PRIMARY INT name name name VARCHAR(25) NOT NULL UNIQUE, VARCHAR(25) NOT NULL, NULL, VARCHAR(25) NOT start start start DATETIME NULL, DATETIME NULL, NULL, DATETIME len len NULL NULL NULL INT len INT INT ) ) ) V2 V3 Revision History V1 New Incremental CREATE TABLE dbo.Auction ALTER TABLE dbo.Auction Deployment Deployment ( WITH CHECK ADD CONSTRAINT id INT NOT NULL PRIMARY KEY, Au_SK UNIQUE (name) name VARCHAR(25) NOT NULL UNIQUE, start DATETIME NULL, len INT NULL )
  • 14. Agenda Overview The Database Project (Test) Data Generation Unit Tests Managing Change Q&A
  • 15. Test Data Q & A • Why not production data? – Ahhh, it may be illegal – Doesn’t test edge cases – Doesn’t address schema changes • What are our test data requirements? – “Random” – Deterministic – Appropriately distributed –…
  • 16. Test Data Q & A (continued) • What are the differing needs for test data? – Functional Test – Load Test • What are the versioning implications – We need differing plan(s) for differing schemas – We need to version plans along side schemas
  • 17. (Test) Data Generation in VSTS Choose the following • The Tables • Number of rows (RowCount ratios help) • The generator for each column Out of the box: string, RegEx, data bound, etc. – Write your own – Set generator-specific settings – Set the seed – provides determinism – • The distribution
  • 18. demo (Test) Data Generation
  • 19. Agenda Overview The Database Project (Test) Data Generation Unit Tests Managing Change Q&A
  • 20. Database Unit Testing • Automatically generate unit test stubs for: – Stored Procedures, Functions, Triggers • Test Validation (assertions) – T-SQL (server based) Assertions • RAISERROR command – Client Side Assertions • None Empty ResultSet • Row Count • Execution Time • Pre and Post Test Scripts
  • 21. Database Unit Testing • Automatic Deployment Integration – Automatically deploy database project prior to running tests • Data Generation Integration – Automatically generate data based on generation plan prior to running tests
  • 23. Agenda Overview The Database Project (Test) Data Generation Unit Tests Managing Change Q&A
  • 24. What kind of change can we manage? • Refactoring • Compare schemas • Compare data