Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
SQL Server
Development Best
Practices
Source Control, Testing, and
Continuous Integration
Ike Ellis, MVP
The Monastery
Please silence
cell phones
Explore Everything PASS Has to Offer
Free SQL Server and BI Web Events Free 1-day Training Events Regional Event
Local Use...
Session Evaluations
ways to access
Go to
passsummit.com/evals
Download the GuideBook App
and search: PASS Summit 2014
Foll...
Agenda
• Source Control
• Testing
• SQLCop
• Continuous Integration
• Continuous Deployment
• Tooling
No Developer Left Behind
Time
Main Trunk
New Dev Working
When Integration Time Strikes
Longer Time = More Errors
More errors to solve, means more time to solve errors
Dev continue...
Shorten the Time
Less or no problems to solve
Deployment can always happen
Code on every workstation is in a build
ready c...
• Complicated deployment is always messed up
• Same simple deployment, but hundreds of times (lots of
customers – multiten...
“Great software developers are 127 times more effective
than average, but only require 50% higher salary, making
them the ...
• Change tracking
• Code storage
• Permissions
• History
What is source control?
• Check-in
• Check-out
• Merge
• Branch
• Undo
• Lock
SC: Process
• Management visibility
• Productive developers
• Allows insight into all work done
• Compliance and auditing
• Scalable t...
Source Control: Products
• Version control is easy with files
• Much harder with a managed database file
SC: Problems with the Database
• Everything’s a file (any product)
• Migrations/file versions/update scripts
• TFS
• Notion of offline, disconnected dev
...
Check in Lookup Table/Data
http://blogs.msdn.com/b/ssdt/archive/2012/02/02/includin
g-data-in-an-sql-server-database-proje...
Source Control
Demo
• Write code all day…never write a test
• Something breaks
• Cheapest place to find the bug?
Unit Testing: What happens wi...
Bug Correction Time
0.85
Find A Bug Fix A Bug
• Automated and self-checked procedure used to validate
that a particular module of source code works correctly
• Write te...
Arrange
Act
Assert
Unit Test: Process
• Documents code functionality
• Better than commenting code
• Allows refactoring
• Red Green Refactor Repeat
Unit Testing...
Unit Testing: Main Benefit
Tests get checked in and run regularly
Prevents same bug from happening again
Unit Testing & Source Control
• Unit testing framework for MS SQL Server
• Compatible with SQL Server 2005 and above
• Tests are run inside transaction
...
• Supports sproces, functions, and table triggers
• Needs a test project and a SQL test class added to that
project
• Test...
Tracks SQL Best Practices
Free
Sprocs come with SQLTest
SQLCop
Unit Testing
Demo
• Long names = OK
• Test small chunks
• Covering is overrated
• Tests take no arguments
• Not all sprocs are testable, but...
CI: The Integration/Deployment Process
We do it when we feel like it
We do it daily
We do it on a schedule
We do it at eve...
CI: Benefits
• Avoids the “Works on My PC” syndrome
• All developers can get their work deployed and not be left behind
• ...
CI: Working with Legacy Code
First thing we do is deploy
Can we deploy
Is source control accurate?
CI Fundamentals
Build Steps = Automatically Build Stuff = Scripts
Build Triggers = What makes us build?
Build Agents = Wha...
CI Products
CruiseControl
Hudson
JetBrains = TeamCity
MSBuild/TFS
RedGate CI for databases
CI Process (An Example)
Step 1: Check in from source control
Step 2: Build Trigger begins a build, CI takes over
Step 3: C...
Best Practices – Don’t Break the Build
Check-in several times
a day
Merge changes at
every check-in
If you broke the build...
Continuous Integration
Demo
• Great for velocity
• Used when it’s a single system with many complicated
parts
• Load balancers
• file servers
• databa...
Continuous Deployment
Demo
Code &
Write Tests
Check-in
Run Tests
& Integrate
Create
Build
Deploy to
UAT
Deploy to
Production
Generic Tools
Database T...
• Write tests to avoid errors with missing objects, schema changes,
bad logic, and bad migrations
• Use source control and...
Ike Ellis, MVP
• http://www.ikeellis.com
• The Monastery
• SQL Pass Book Readers
• San Diego Tech Immersion Group
• @ike_e...
ALM Best Practices with a SQL Server Database
ALM Best Practices with a SQL Server Database
Upcoming SlideShare
Loading in …5
×

ALM Best Practices with a SQL Server Database

1,996 views

Published on

This deck describes how to unit test, source control, and put a SQL Server database into a continuous integration/ continuous deployment environment.

Published in: Technology
  • Be the first to comment

ALM Best Practices with a SQL Server Database

  1. 1. SQL Server Development Best Practices Source Control, Testing, and Continuous Integration Ike Ellis, MVP The Monastery
  2. 2. Please silence cell phones
  3. 3. Explore Everything PASS Has to Offer Free SQL Server and BI Web Events Free 1-day Training Events Regional Event Local User Groups Around the World Free Online Technical Training This is Community Business Analytics Training Session Recordings PASS Newsletter
  4. 4. Session Evaluations ways to access Go to passsummit.com/evals Download the GuideBook App and search: PASS Summit 2014 Follow the QR code link displayed on session signage throughout the conference venue and in the program guide Submit by 11:59 PM EST Friday Nov. 7 to WIN prizes Your feedback is important and valuable. Evaluation Deadline: 11:59 PM EST, Sunday Nov. 16
  5. 5. Agenda • Source Control • Testing • SQLCop • Continuous Integration • Continuous Deployment • Tooling
  6. 6. No Developer Left Behind Time Main Trunk New Dev Working
  7. 7. When Integration Time Strikes Longer Time = More Errors More errors to solve, means more time to solve errors Dev continues, prolonging error correcting time Integration might never happen Time Main Trunk New Dev Working
  8. 8. Shorten the Time Less or no problems to solve Deployment can always happen Code on every workstation is in a build ready condition Main Trunk New Dev Working
  9. 9. • Complicated deployment is always messed up • Same simple deployment, but hundreds of times (lots of customers – multitenancy) Other problems
  10. 10. “Great software developers are 127 times more effective than average, but only require 50% higher salary, making them the best bargain in business.” Martin Fowler
  11. 11. • Change tracking • Code storage • Permissions • History What is source control?
  12. 12. • Check-in • Check-out • Merge • Branch • Undo • Lock SC: Process
  13. 13. • Management visibility • Productive developers • Allows insight into all work done • Compliance and auditing • Scalable team • Code delivery • History tracking • Rollback capable • Safety net • Allows for danger/manages risk Source Control: Benefits
  14. 14. Source Control: Products
  15. 15. • Version control is easy with files • Much harder with a managed database file SC: Problems with the Database
  16. 16. • Everything’s a file (any product) • Migrations/file versions/update scripts • TFS • Notion of offline, disconnected dev • RedGate • ApexSQL SC: Options
  17. 17. Check in Lookup Table/Data http://blogs.msdn.com/b/ssdt/archive/2012/02/02/includin g-data-in-an-sql-server-database-project.aspx Building CI with TFS http://blogs.msdn.com/b/ssdt/archive/2014/07/24/sql- server-database-projects-and-team-foundation-build.aspx References
  18. 18. Source Control Demo
  19. 19. • Write code all day…never write a test • Something breaks • Cheapest place to find the bug? Unit Testing: What happens without it Write a bug QA Testing Deliver
  20. 20. Bug Correction Time 0.85 Find A Bug Fix A Bug
  21. 21. • Automated and self-checked procedure used to validate that a particular module of source code works correctly • Write tests for Triggers, Stored Procedures, Functions Unit Test: What is it?
  22. 22. Arrange Act Assert Unit Test: Process
  23. 23. • Documents code functionality • Better than commenting code • Allows refactoring • Red Green Refactor Repeat Unit Testing: Benefits
  24. 24. Unit Testing: Main Benefit
  25. 25. Tests get checked in and run regularly Prevents same bug from happening again Unit Testing & Source Control
  26. 26. • Unit testing framework for MS SQL Server • Compatible with SQL Server 2005 and above • Tests are run inside transaction • Grouped together in a schema • Output to text or XML • Fake tables and views • Spy on stored procedures tSQLt
  27. 27. • Supports sproces, functions, and table triggers • Needs a test project and a SQL test class added to that project • Test Explorer TFS Unit Testing
  28. 28. Tracks SQL Best Practices Free Sprocs come with SQLTest SQLCop
  29. 29. Unit Testing Demo
  30. 30. • Long names = OK • Test small chunks • Covering is overrated • Tests take no arguments • Not all sprocs are testable, but if testing is what you do, you’ll write better sprocs • Test against a different database than development • Insolate yourself from development database changes that rapidly change Testing Best Practices
  31. 31. CI: The Integration/Deployment Process We do it when we feel like it We do it daily We do it on a schedule We do it at every check-in to source control
  32. 32. CI: Benefits • Avoids the “Works on My PC” syndrome • All developers can get their work deployed and not be left behind • Tests can be run constantly, and breaking tests can generate emails, thus inspiring code confidence • Higher quality in code • Automatically build documentation, remove unneeded files, include dependencies • Increased visibility of project • Deployment can be separate from developers • Easier to deploy dev environment to new developers
  33. 33. CI: Working with Legacy Code First thing we do is deploy Can we deploy Is source control accurate?
  34. 34. CI Fundamentals Build Steps = Automatically Build Stuff = Scripts Build Triggers = What makes us build? Build Agents = What can we do in the CI process? Build Notifications = Who gets told what and when? Build Correction = What went wrong and who will solve it?
  35. 35. CI Products CruiseControl Hudson JetBrains = TeamCity MSBuild/TFS RedGate CI for databases
  36. 36. CI Process (An Example) Step 1: Check in from source control Step 2: Build Trigger begins a build, CI takes over Step 3: CI builds the solution Step 4: CI runs all the tests Step 5: CI deploys package to a UAT server Step 6: CI notifies everyone a new build is ready to test
  37. 37. Best Practices – Don’t Break the Build Check-in several times a day Merge changes at every check-in If you broke the build, tell everyone, so they can stop getting latest from source control Don’t check-in until the build is fixed Notify everyone once the build is fixed
  38. 38. Continuous Integration Demo
  39. 39. • Great for velocity • Used when it’s a single system with many complicated parts • Load balancers • file servers • databases • web services • Or a simple system, but replicated many times, like multi- tenancy • Or both Continuous Deployment
  40. 40. Continuous Deployment Demo
  41. 41. Code & Write Tests Check-in Run Tests & Integrate Create Build Deploy to UAT Deploy to Production Generic Tools Database Tools Visual Studio Subversion GIT TFS TFS(Test & Buid) TeamCity Jenkins CruiseControl Octopus Deploy SSDT SSMS RedGate ApexSQL SSDT tSQLt RedGate Test SSDT SSDT RedGate RedGate
  42. 42. • Write tests to avoid errors with missing objects, schema changes, bad logic, and bad migrations • Use source control and versioning to create a good migration • Use CI to create a build that is repeatable and roll-backable • Every developer should have their own database • Use CD to control complicated build systems with moving parts, or repeatable simple build systems, like multi-tenancy Do what you know you’re supposed to be doing Conclusion and Best Practices
  43. 43. Ike Ellis, MVP • http://www.ikeellis.com • The Monastery • SQL Pass Book Readers • San Diego Tech Immersion Group • @ike_ellis • 619.922.9801 • Email address is just my first name @the-monastery.com

×