Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Developing, Building and
Releasing databases with
SSDT and
Taavi Koosaar
DevOps Consultant | VS ALM MVP | VS ALM
Ranger
Agenda
• Concepts
• Development
• Build
• Release
Concepts
Development and deployment models
• Manual scripts, combine and run
– Write the scripts manually and run them in correct s...
The SSDT tooling
DACFx
SqlPackageSSMSSSDT
Visual Studio
SQL DBdacpac
SQL Client
SSDT 2016 July and forward
DAC framework
• The component /framework that makes deployment
possible
– Model, deployment and api
– SqlPackage.exe
– Dat...
SSDT in Visual Studio
• SQL Server Object Explorer – connected / offline dev
• SQL Database project structure, references ...
SQL Database project
• Everything you plan to deploy to database and
everything you are referencing should be there
– Exce...
Composite projects
• Reference external databases
• Break up a larger database into logical units
• Separate by schema / f...
Composite project references
SQL
DB
dacpac
My DB Project
Artefacts
Artefacts
Artefacts
Artifacts
My Other
DB Project
data-...
What is a DacPac?
• A zip file
• Result of the build of database project
• Contains the model / schema / refactorings
• Co...
DacPac deployment flow (per DacPac)
Deploy
Schema
Comparison
Script
generation
(delta)
Execute Pre
Deployment
Execute
gene...
Development
Getting started with database project
• Import existing database into database project
– From database
– From dacpac (e.g....
Getting started with database project
• Validation turned on
– Turn validation off for initial import!
– Use the latest ve...
Enhanced DacPac deployment flow
1. Database backup
2. Pre DacPac deployment scripts
3. Dacpac deploy
1. Compare source and...
DB Proj Structure
Refactoring
• Rename, Move to Schema, Expand Wildcards, Fully
qualified names
• Refactor log - Extra metadata in dacapc to...
Everything should be named
• PK
• FK
• Constraints
• …
[ModifiedDate] DATETIME
CONSTRAINT [DF_Address_ModifiedDate] DEFAUL...
Static data is static (and part of schema)
• All data in the static scripts must be static incl ID
columns
• Use merge scr...
Pre-deployment, post-deployment,
predacpac
• Reference all the necessary databases and projects to
build (dacpacs, master,...
Handling changes
• Transitional
– R1: Do non-breaking change (e.g. add NULL column)
– R2: Turn column into NOT NULL
• All ...
Build
Building a DacPac
Commit changes to source
control
Deploy
Continuous
Integration
Pull changes from source control
Build
solution
Package the...
Release
Deploy a DacPac
Use variables in release pipeline
Version and Environment in the DB / scripts
• Make sure to send in Environment and Version as
variables to tasks in releas...
Custom properties for dacpac deploy
• E.g.
/p:DropObjectsNotInSource=True
/p:DoNotDropObjectTypes=Users;RoleMembership;Per...
Validate database schema changes
• As often as possible by deploying dacpac to specific
environment
• The bigger the envir...
DacPac compare report
• DacPac has support to report the changes that will be
done
– Extra arguments to sql package
– /dep...
Links
• Download SSDT
– https://msdn.microsoft.com/library/mt204009.aspx
• Download SQL CMD
– https://www.microsoft.com/en...
Thank You!
Taavi Koosaar
taavik@solidify.se
@melborp | +46 732 013 296
SSDT can help manage and maintain
your SQL database!
www.solidify.se
Nächste SlideShare
Wird geladen in …5
×

Meetup developing building and_deploying databases with SSDT

394 Aufrufe

Veröffentlicht am

Från Swedish Microsoft ALM DevOps Meetup 3, https://www.meetup.com/swedish-ms-alm-devops/events/236531424/
På denna träff kommer vi gräva oss ned i build & deployment och inleder med en presentation om hur man kan hantera databasuppdateringar som en del av deploymentflödet.

Veröffentlicht in: Software
  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

Meetup developing building and_deploying databases with SSDT

  1. 1. Developing, Building and Releasing databases with SSDT and Taavi Koosaar DevOps Consultant | VS ALM MVP | VS ALM Ranger
  2. 2. Agenda • Concepts • Development • Build • Release
  3. 3. Concepts
  4. 4. Development and deployment models • Manual scripts, combine and run – Write the scripts manually and run them in correct sequence • Model based deployment – Compare two schemas and apply difference • Migration based deployment – Apply all changes since the last migration – E.g. EF migration
  5. 5. The SSDT tooling DACFx SqlPackageSSMSSSDT Visual Studio SQL DBdacpac SQL Client
  6. 6. SSDT 2016 July and forward
  7. 7. DAC framework • The component /framework that makes deployment possible – Model, deployment and api – SqlPackage.exe – Data Tier Applications • Comes with SSDT and SQL Server (can also be separately installed) • Compatible downwards • Not everything of SQL is supported – https://msdn.microsoft.com/en-us/library/ee210549.aspx
  8. 8. SSDT in Visual Studio • SQL Server Object Explorer – connected / offline dev • SQL Database project structure, references and daily work – Incl code analytics, intellisense, refactoring • SQL Schema Compare | Data Compare • Pre-Build, Post-Build and Pre-DacPac, Post-DacPac – Reference data scripts • DACPAC, BACPAC and Publish/Deployment – Settings, sqlcmd variables • Refactor log
  9. 9. SQL Database project • Everything you plan to deploy to database and everything you are referencing should be there – Except the few things you may not want to e.g. logins, roles • Project & Database references – Composite projects • (SQL CMD) Variables • Code Analysis rules • Snapshots
  10. 10. Composite projects • Reference external databases • Break up a larger database into logical units • Separate by schema / functional area • Decrease build time – build smaller pieces
  11. 11. Composite project references SQL DB dacpac My DB Project Artefacts Artefacts Artefacts Artifacts My Other DB Project data-tier application system database project Same Database Same Server, Different Database Different Server, Different Database master msdb
  12. 12. What is a DacPac? • A zip file • Result of the build of database project • Contains the model / schema / refactorings • Contains the pre and post deployment combined scripts • Versioned
  13. 13. DacPac deployment flow (per DacPac) Deploy Schema Comparison Script generation (delta) Execute Pre Deployment Execute generated script Execute Post Deployment
  14. 14. Development
  15. 15. Getting started with database project • Import existing database into database project – From database – From dacpac (e.g. that came from production) • First time, you will hit and find a lot of … – Invalid objects - Old objects referencing non-existent columns / encrypted objects / invalid synonyms / cross database references / circular references / unsupported objects for target platform • Engage a Subject Matter Expert: Talk to your DBA to help clean up these issues
  16. 16. Getting started with database project • Validation turned on – Turn validation off for initial import! – Use the latest version of the tools! – Import everything and fix issues within Visual Studio • SSDT imports the first 1000 stored procedures into Procs1, the next into Procs2 etc – You may need to add some folder structure to your project to keep developers sane. – Having many 1000’s of objects affects SSDT performance
  17. 17. Enhanced DacPac deployment flow 1. Database backup 2. Pre DacPac deployment scripts 3. Dacpac deploy 1. Compare source and target schemas 2. Generate diff script 3. Pre Deployment script 4. Run diff script 5. Post Deployment scripts 4. Post DacPac deployment scripts (optional) 5. Restore on failure (optional)
  18. 18. DB Proj Structure
  19. 19. Refactoring • Rename, Move to Schema, Expand Wildcards, Fully qualified names • Refactor log - Extra metadata in dacapc to understand change
  20. 20. Everything should be named • PK • FK • Constraints • … [ModifiedDate] DATETIME CONSTRAINT [DF_Address_ModifiedDate] DEFAULT (getdate()) NOT NULL CONSTRAINT [PK_Address_AddressID] PRIMARY KEY CLUSTERED ([AddressID] ASC) CONSTRAINT [FK_Address_StateProvince_StateProvinceID] FOREIGN KEY ([StateProvinceID]) REFERENCES [Person].[StateProvince] ([StateProvinceID])
  21. 21. Static data is static (and part of schema) • All data in the static scripts must be static incl ID columns • Use merge script approach for static data when possible – Can use sp_generate_merge to create merge scripts from real tables – https://github.com/readyroll/generate-sql-merge • When adding/removing ReferenceData files or Pre/Post deployment scripts – Pre / Post deployment.sql file needs be updated!
  22. 22. Pre-deployment, post-deployment, predacpac • Reference all the necessary databases and projects to build (dacpacs, master, database projects) – Use extraction of dacpacs from real databases • PreDacPac, PreDeploy, PostDeploy scripts (incl static data) are marked Build Action = None • All pre, post and predacpac scripts must be repeatable and fail-safe • Use SQL CMD variables such as version, environment in conditions in scripts, and Installation table
  23. 23. Handling changes • Transitional – R1: Do non-breaking change (e.g. add NULL column) – R2: Turn column into NOT NULL • All in one – Use predacpac to do some of the changes before dacpac deployment hits
  24. 24. Build
  25. 25. Building a DacPac
  26. 26. Commit changes to source control Deploy Continuous Integration Pull changes from source control Build solution Package the output Test
  27. 27. Release
  28. 28. Deploy a DacPac
  29. 29. Use variables in release pipeline
  30. 30. Version and Environment in the DB / scripts • Make sure to send in Environment and Version as variables to tasks in release – To PreDacPac SQL script – To dacpac deployment • Using Sql Dacpac – /v:Environment="$(Release.EnvironmentName)" /v:Version="$(Build.BuildNumber)" • Using Sql Script – -Variable "Environment=`"$(Release.EnvironmentName)`"", "Version=`"$(Build.BuildNumber)`""
  31. 31. Custom properties for dacpac deploy • E.g. /p:DropObjectsNotInSource=True /p:DoNotDropObjectTypes=Users;RoleMembership;Permi ssions • /p:BlockOnPossibleDataLoss=False • All SqlPackage options are documented in msdn – https://goo.gl/Uh16By
  32. 32. Validate database schema changes • As often as possible by deploying dacpac to specific environment • The bigger the environments schema difference (dev -> test -> prod), the bigger the upgrade script (and more fragile) – To mitigate write all pre/post/.. scripts fail safe and repeatable – To mitigate restore stage/acceptance database and run once-off throw away upgrades as safety CI on schedule – Note! This is necessary more often when releases are less frequent, otherwise CD taking package from Dev -> Test -> Stage -> Prod should handle it
  33. 33. DacPac compare report • DacPac has support to report the changes that will be done – Extra arguments to sql package – /deployscriptpath:”MyDeployScript.sql” /deployreportpath:”MyDeployReport.xml” • Can compare with previous release – Custom extension, currently not tested – https://marketplace.visualstudio.com/items?itemName=colinsal mcorner.colinsalmcorner-buildtasks • https://blogs.msdn.microsoft.com/ssdt/2016/10/20/sql- server-data-tools-16-5-release/
  34. 34. Links • Download SSDT – https://msdn.microsoft.com/library/mt204009.aspx • Download SQL CMD – https://www.microsoft.com/en- us/download/details.aspx?id=53591 • SSDT Nuget Package – https://www.nuget.org/packages/Microsoft.Data.Tools.Msbuild/ • Generate SP Merge – https://github.com/readyroll/generate-sql-merge
  35. 35. Thank You! Taavi Koosaar taavik@solidify.se @melborp | +46 732 013 296 SSDT can help manage and maintain your SQL database!
  36. 36. www.solidify.se

×