The document discusses treating databases as first-class citizens in development by managing schemas and data through database projects and tools. It addresses questions around where the truth of a schema resides, how to version databases, generate test data, perform unit testing, and manage changes. The key points are using database projects to represent the truth of the schema, version control to manage versions, test data generators for testing, and tools for schema/data compares and refactoring to facilitate change management.
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
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
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
)
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
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