SQL in Version Control using SQL Server Database Projects

14
Get Your SQL into Version Control USING MICROSOFT SQL SERVER DATABASE PROJECTS Floyd Hilton @fhilton

Transcript of SQL in Version Control using SQL Server Database Projects

Page 1: SQL in Version Control using SQL Server Database Projects

Get Your SQLintoVersion ControlUSING MICROSOFT SQL SERVER DATABASE PROJECTS

Floyd Hilton@fhilton

Page 2: SQL in Version Control using SQL Server Database Projects

Boston Code Camp 23 - Thanks to our Sponsors!

Gold

Bronze

In-Kind Donations

Page 3: SQL in Version Control using SQL Server Database Projects

Background

Page 4: SQL in Version Control using SQL Server Database Projects

Why put SQL into Version Control?Lower Risk, Increased Quality•Keeps history

•Documentation – Tie to issue tracker

•Makes processes repeatable

•Group changes (by feature)

•Continuous Integration and Deployment

•Portability

Page 5: SQL in Version Control using SQL Server Database Projects

Two Version Control Processes for SQL

1. Transformation

2. State

Page 6: SQL in Version Control using SQL Server Database Projects

TransformationIncremental changes to the database are saved and then can be run in order to gain the desired database state

V0.0.1

0001Database Create

Script

0002Add Table

0003Add Procedure

0004Add Lookup Data

V0.0.2

State 1 State 2

Actual state is not saved, only how to get there

Page 7: SQL in Version Control using SQL Server Database Projects

Basic Transformation Process

Custom Tool

V0.1.0

SQL Management Studio

Page 8: SQL in Version Control using SQL Server Database Projects

Transformation Tools•DbUp•https://www.nuget.org/packages/dbup/

•Flyway•http://flywaydb.org/

•Nmigrations•https://github.com/sklose/NMigrations

•roundhouse•https://github.com/chucknorris/roundhouse

Page 9: SQL in Version Control using SQL Server Database Projects

StateThe full state of the database is stored for every version

State is saved, and tools are used to transform a database from one state to another

V0.0.2 Schema

in source control

V0.0.1 Schema

in source control

V0.0.1Database

Tool compares state of current database to state of schema in version control

V0.0.2Database

Transformation Script or other

Method

DevelopersMake

ChangesV0.0.3

Schemain source control

DevelopersMake

Changes

Page 10: SQL in Version Control using SQL Server Database Projects

State Tools/Methods•Manual•Manually script schema objects and store in source control

•Entity Framework Migrations

•Redgate SQL Source Control

•SQL Server Database Project

Page 11: SQL in Version Control using SQL Server Database Projects

Which to Choose?TRANSFORMATIONS

When to use◦ Starting from scratch

◦ Simple model used for a single system

◦ Small team

◦ When a state tool is not available

STATE

When to use◦ Existing, complex data models

◦ Multiple, interconnected databases

◦ Databases that are used by many systems

◦ Large Team

Page 12: SQL in Version Control using SQL Server Database Projects

What is an SQL Database Project?It is a Visual Studio Project specifically for storing database schema and information.

What is a Visual Studio Project?◦ "Projects are used in a solution to logically manage, build, and debug the items that make up

your application. The output of a project is usually an executable program (.exe), a dynamic-link library (.dll) file or a module, among others.“

◦ For a Database Project, the output is a DACPAC (Data-Tier Application Package)

What is a Solution?◦ "Solutions contain items that you need in order to create your application. A solution

includes one or more projects, plus files and metadata that help define the solution as a whole."

Page 13: SQL in Version Control using SQL Server Database Projects

Demonstration – SQL Database Project

Page 14: SQL in Version Control using SQL Server Database Projects

ResourcesEnrico Campidoglio◦ NDC talk on Continuous Delivery for SQL

◦ http://www.ndcvideos.com/#/app/video/2531

◦ Features DbUP (https://www.nuget.org/packages/dbup/)

◦ .NETROCKS discussion on Continuous Delivery for SQL◦ http://www.dotnetrocks.com/default.aspx?showNum=1079

Deborah Kurata◦ Pluralsight video – Visual Studio Data Tools for Developers

◦ http://www.pluralsight.com/courses/visual-studio-data-tools-developers