• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Session #4: Treating Databases as First-Class Citizens in Development
 

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

on

  • 1,511 views

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

Session #4: Treating Databases as First-Class Citizens in Development
Presented by: Rob Bagby

Statistics

Views

Total Views
1,511
Views on SlideShare
1,506
Embed Views
5

Actions

Likes
1
Downloads
23
Comments
0

2 Embeds 5

http://blogs.msdn.com 4
http://www.slideshare.net 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

    • 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