Nella creazione di applicazioni, non abbiamo mai avuto a disposizione un ambiente integrato, dedicato allo sviluppatore per la progettazione e il deployment di un Database, per la scrittura di codice SQL. Oggi questa lacuna è stata colmata dall'introduzione degli SQL Server Data Tools: grazie ad un apposito template per Visual Studio 2010 e 2012, è possibile gestire un progetto SQL, come un progetto di una applicazione desktop o di una applicazione web. Vediamo insieme cosa possiamo fare con questi strumenti.
4. Marco Parenzan
.NET Developer, SQL User
1nn0va Speaker (www.innovazionefvg.net)
Trainer and consultant on application
development in companies and in University
(of Trieste)
Head of Development in CGN S.P.A.
(www.cgn.it)
4 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
6. Database Development
Often not integrated into application life cycle
management and processes
What is a database development
experience?
Install SSMS into developer machine….
It‟s an «admin» tool
Outside Visual Studio (it‟s Visual Studio yes)
Third party tools…
What does »deployment» means?
6 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
7. The premise
IMPERATIVE DATABASE
DEVELOPMENT
7 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
8. Database Development Challenges
Databases are inherently stateful
Focus is on ALTER instead of CREATE
Dependencies complicate scripts
Errors are often found only when scripts are
executed
Synchronizing application and database
versions
Targeting different SQL Server and SQL
Azure versions
8 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
9. Simple Schema Versioning Example
-- version 1
CREATE TABLE dbo.Speakers
(
SpeakerId INT NOT NULL,
FirstName NVARCHAR(64) NOT NULL,
LastName NVARCHAR(64) NOT NULL,
)
-- version 2 -- version 3
ALTER TABLE dbo.Speakers ALTER TABLE dbo.Speakers
WITH CHECK ADD CONSTRAINT WITH CHECK
Speaker_PK ADD CONSTRAINT Speaker_PK UNIQUE
(LastName, FirstName)
V1 V2 V3 Revision History
9 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
10. Imperative
Imperative scripts hard codes knowledge about:
The state of the target system:
Presence of objects, dependencies, unbinding, rebinding
Required ordering of operations
Cumulative changes need to be serialized
v1v2v3v4v5 instead of v1v4v5
Validating the end-result against expected end-state
is hard
No easy way to compare expected vs. actual
Batching or separating multiple changes
There are third party tools
10 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
11. Imperative 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 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
12. Missing/Manual
Object Dependencies
Intercept errors before deployment
Don‟t loose data
Sync DB
Managing different versions
13. The foundation
DECLARATIVE DATABASE
DEVELOPMENT
13 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
14. Declarative
Define what you want in the form of a model
Think about a table without thinking about the
specific SQL
Fill the model using a DSL (domain specific
language)
T-SQL
Use the model as the starting point “blueprint”
for all operations
14 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
15. Declarative Versioning
-- version 1
CREATE TABLE dbo.Auction
-- version 2
( CREATE TABLE dbo.Auction
id ( INT NOT NULL,3
-- version
name id CREATE NOT NOTdbo.Auction
TABLE NULL,
VARCHAR(25) NULL PRIMARY KEY,
INT
(
start DATETIME NULL, NOT NULL,
name VARCHAR(25)
len start NULL INT NOT NULL PRIMARY KEY,
INTidDATETIME NULL,
) name VARCHAR(25) NOT NULL UNIQUE,
len INT NULL
start DATETIME NULL,
)
len INT NULL
)
V1 V2 V3 Revision History
15 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
16. Model as the starting point “blueprint”
Deployment/publish, start by comparing the
current state of the target with the required
state (blueprint)
Use the resulting difference knowledge to
programmatically generate an deployment
plan
Use plan to create sequence of (SQL)
statements required to make the target state
become like the blueprint state
16 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
17. Declarative Schema Deployment
17
Source Target
Model Reverse
Schema Schema
Compare Engineer
Model Model
Diff List
Plan
Optimizer
Additional
schema
Deploy Plan
artifacts
Script
Generator
Script Target
Executor Incremental target update DB
17 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
18. Imperative vs. Declarative
Manual vs. generated / programmatic
Point in time vs. always current
Sequential vs. differential
18 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
19. Model Based
.SQL SqlScript Schema
Parse Dom Interpret Validate
Source Model
All schema objects are represented inside a
model
What is in the model is defined by the provider
To load/hydrate a model instance
Parse SqlCodeDom, based on parsers Abstract
Syntax Tree (AST)
Interpretation Schema Model
Symbol list
Object References (hard and soft dependencies)
Validation
19 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
20. Script Fundamentals
Requirement to be
able to round trip DSL A
artifacts S T
Script Model
SQL script Parse Script
Parser
Script fragment T-SQL
Fragment
Script fragment Script
Gen SQL script
Script fragment is AST
Script
Script
Gen T-SQL
Fragment
(Abstract Syntax Tree)
20 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
22. Basic ScriptDom loop
static void Main(string[] args)
{
bool initialQuotedIdentifiers = false;
TSqlParser parser = new TSql110Parser(initialQuotedIdentifiers);
StreamReader sr = new StreamReader(@".test.sql");
IList<ParseError> errors;
TSqlFragment fragment = parser.Parse(sr, out errors);
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
Sql110ScriptGenerator scriptGen = new Sql110ScriptGenerator();
scriptGen.GenerateScript(fragment, sw);
}
22 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
23. 23
SQL SERVER DATA TOOLS
OVERVIEW
23 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
24. History of the “DataDude” Project
Project funded April 2005
Project started July 1st 2005
Visual Studio 2005 Team System for Database
Professionals
Visual Studio 2008 Team System for Database
Professionals
Re-architected to be declarative model based system
Visual Studio 2008 Team System for Database
Professionals GDR R2
Visual Studio 2010 Team System for Database
Professionals
Transitioned to SQL Server 2009
SQL Server Data Tools
24 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
25. Database Project
Evolution of VS database project
We have more things
We don‟t have
Data generation
Unit testing
Data compare
Use a DB Project for this
http://social.msdn.microsoft.com/Forums/en-US/ssdt/thread/33664902-
a4e2-415a-ad4e-a480fbc56158/
Not available in Professional or Express Edition
Migration
http://geekswithblogs.net/80n/archive/2012/09/11/vsdb-to-ssdt-
series--introduction.aspx
Comparison
28. Supported OS Platforms
Windows Vista SP2+
Windows 7 SP1+
Windows Server 2008 SP+
Windows Server 2008 R2 SP1+
Note: this is different then the Visual Studio
2010 platforms
28 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
29. Availability
Visual Studio 2010 Integrated Shell
SQL-CLR requires Visual Studio 2010 Standard
Visual Studio 2010 SP1
Visual Studio 2012
SQL Server 2012
29 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
30. Features of SQL Server Data Tools
30 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
03/29/2012
32. Connected Development
SQL Server Object Explorer (SSOX)
T-SQL Editor/Debugger/IntelliSense (New Query)
Power-Buffer (New Object/View Code)
Table Designer (View Designer)
Data Editor (View Data)
Execute Procedure/Function
Schema Compare
32 | 03/29/2012
17/11/2012 | SQL Saturday #176 – Pordenone, Italy
33. SQL Server Object Explorer (SSOX)
The SQL Server navigation tool window
Modeled after the SSMS Object Explorer
Server scoped vs. Server Explorer which is database scoped
Launch point for T-SQL Editor, Debugger, Object
Execution, Designers, …
Supports connecting directly to SQL Server
SQL Server {2005, 2008, 2008R2, 2012} and SQL Azure
Supports connecting directly to a (contained) database
Constructs a partial model in the background and under the
covers
Supports:
Cascading Drops and Renames
Drift detection
Automatically updates the tree/source as changes occur on the server
33 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
34. T-SQL Editor/Debugger/IntelliSense
T-SQL IntelliSense
SQL Server 2005+ incl. SQL Azure
Result to Grid | Text | File
Execution modes
Execute
Execute with debugger
Reusable debug script generation
34 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
35. Power-Buffer/Table Designer
Declarative deferred online schema management
Brings “project-oriented” features to online / connected
development
Live errors and warnings as changes are made against code
“Project-oriented” validation during editing
Table designer
Code-behind designer
Works on top of Power-Buffer (partial model) or project model
Strives to be the same experience online as experienced in the
project system
Coordinated execution of buffers against the same target
Diff scripts by default are transactional
(ToolsOptionsSQL Server ToolsOnline EditingInclude
transactional scripts)
35 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
36. Data Editor
View and Edit Data via grid editor
Copy-Paste support
Generate as INSERT to file or query window
36 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
37. Execute Procedure/Function
Dialog based input for parameter collection
Generates SQL script for execution/debugging
37 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
38. 38
PROJECT BASED
DEVELOPMENT
38 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
39. Creating and Importing
Single project template
VSDB note:
No distinction between server or user projects
Server projects are projects with target database set to
master
Import from:
Database
DACPAC
SQL Script (multi-file import enabled)
Selective import by using Schema Compare
“Create New Project” from SSOX
Inherits version from source database
39 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
40. Folder Structure
Options on import:
None
Schema
Object type
SchemaObject type
40 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
41. Project Directory Guidelines…
Do not host more then one table inside script
file
Table designer will not see additional tables
inside script
41 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
42. Additional Language Services
Projects enable additional language services
Go To Definition
Find All References
Refactoring
Rename (sp_rename)
Move to Schema (ALTER SCHEMA schema_name
TRANSFER)
Wildcard Expansion
Fully Quality Name
Invoked from Editor, Table Designer and SQL
Server Object Explorer for project based schema
objects
42 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
44. Multi-Targeting
Target version aware:
SQL Server 2005
SQL Server 2008 & SQL Server 2008 R2
SQL Server 2012
SQL Azure
44 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
45. Isolated Sandbox
F5 – Run by default configure to use the
LocalDB instance associated with the project
Data Source=(localdb)<project name>;Initial
Catalog=<project name>;Integrated
Security=True;Pooling=False;Connect
Timeout=30
To retarget to a different SQL Server version
and/or shared SQL Server change:
Project propertiesDebugTarget Connection
String
46. SQL Server 2012 Express LocalDB
Simplify embedded usage of SQL Server
Simple installation
Zero-admin database
Same programming surface as User Instances of SQL Express
The same sqlservr.exe as in service-based SQL Server with the same language and features
Connection through the same client-side APIs, using a new connection string option
SQL Server programming symmetry
Installation similar to .NET Framework model
One set of installed binaries (per major version), no global configuration, no constantly-
running service
Activation model similar to RANU (Access, SQL Compact, …)
The application connects to a database file, SQL activates automatically
No service: SQL process starts in the context of current user
Default connection string
(localdb)v11.0
http://msdn.microsoft.com/en-us/library/hh510202(v=SQL.110).aspx
46 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
47. Build Time Validation
Build validates the complete model
Platform specific validation
Example – Enforces clustered index requirement on
tables in SQL Azure
Integration with Compiler Services
Engine-level build-time validation without schema
deployment
Pre & Post Deployment scripts
Build validates syntactical correctness of pre- and
post- deployment scripts using the SQLCMD
variables
47 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
48. Build Actions
Build - This script will be interpreted as T-SQL (or an XML
Schema in the case of XML Schema Collections) and reflected in
the T-SQL Model
Compile - C# or VB (depending on your project properties) to be
deployed as a SQLCLR assembly
None - Use this for any loose artifacts (like text files)
PreDeploy - The pre deployment master script (there can only be
one)
PostDeploy - The post deployment master script (there can only
be one)
RefactorLog - This is a refactor log which notifies the DaxFx
deployment engine that particular changes should be interpreted
as refactoring operations and not drop/adds
Build Extension Configuration - Reserved for future use, for this
is interpreted as None
48 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
49. Pre & Post Deployment Scripts
Can be used for any “arbitrary” SQL actions that you
need to perform “before” and “after” deployment of
the schema objects
The pre deployment block comes after the generic
database context validation block
The post deployment block comes after the schema
objects block
Pre and post deployment scripts must be
idempotent
They are “executed” each deployment!
http://blogs.msdn.com/gertd/archive/2007/01/25/idempoten
t-scripts-required.aspx
49 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
50. Database References
3-part name usage:
SELECT * FROM
[LiteralDBRef].[Schema1].[Table1]
SELECT *
FROM [$(DBRef)].[Schema1].[Table1]
4-part name usage:
SELECT *
FROM [$(ServerRef)].[$(DBRef)].[Schema1].[Table1]
SELECT *
FROM
[$(ServerRef)].[LiteralDBRef].[Schema1].[Table1]
50 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
51. Project Deployment
Build serializes the model in to model.xml
stream inside .dacpac file
51 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
56. Database Schema Snapshots
Point-in-time representation of schema model
Model must be “error-free” state
Produces same file format (*.dacpac) as build or
extract
To be used for:
Versioning
Comparison
Sharing schema definitions or changes
56 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
58. Schema Definition
Script
Model
Builder
Fragment
Source
DB Scaffolding Schema
Model
Schema Model
Deserializer
Package
58 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
59. Deployment & Schema Compare
Script Script
Model Model
Builder Builder
Fragment Fragment
Source Target
DB Scaffolding Schema Schema Scaffolding DB
Model Model
Model
Schema Model Compare Model Schema
Deserializer Deserializer
Package Package
Diff List
Plan Diff
Optimizer Visualizer
Deploy Plan
59 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy Script
Generator
60. Schema Compare Next and Previous move between top-
level objects, no longer expand the tree.
Simpler set of columns are aligned around the central
action column echoes alignment of schemas above
60
Group-by Action is the Same
default column order
is used for all
groupings;
cleaner and
easier to
Schema no longer parse
used as a parent for regardless of
top-level objects; only grouping
shown when changed
Improved script diff’
algorithm for tables
emphasizes columns
as unit of change.
Gives much improved
alignment. Also
improved look
60 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
67. SQL Azure
It‟s Cloud Computing!
SQL Server e SQL Azure sono diversi
Diverse esigenze
Connected development on SQL Azure
Change targetFix & Build
67 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
68. Conclusions
SQL SERVER DATA TOOLS
68 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
69.
Already third party tools
First release
Many step forward
Some step back (compared to DataDude)
69 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
70. Conclusions
Modeling infrastructure
One development environment
More checks over actions
Use targets
A tool for SQL Azure
Debug and schema deployment
Go under TFS
70 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
71. Resources
Articles
MSDN Magazine Sept 2011 The "Juneau"
Database Project
SQL Server Data Tools Online Installation
http://msdn.microsoft.com/data/tools
Team Blog
http://blogs.msdn.com/b/ssdt/
71 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
73. GRAZIE! Commenta la sessione che hai
appena seguito su Twitter
#sqlsat176
73 | 17/11/2012 | SQL Saturday #176 – Pordenone, Italy
Notas del editor
Developer-focused toolset for building SQL Server & SQL Azure databasesExperiences EnabledConnected DevelopmentProject Based DevelopmentApplication Lifecycle & ToolsFundamentalsDeclarative, model based database developmentIntegrated tools with modern language services Connected and offline with local testingTarget SQL Server and SQL AzureDetecting and managing database drift
Schema Compare is an incredibly useful tool, providing a visual head over SSDT’s model differencing and update engine. It can be used to compare any combination of database, project or dacpac, and allows selective update of the target schema (via an update script in the case of a dacpac). We’ve made some significant changes to the tool for the RTW release, improving its look and feel, particularly to make it easier to digest and process comparison results. This post describes many of Schema Compare's key features – some of which surfaced in CTP4 – with a screen shot at the bottom that highlights several of them.First, the visual comparison ‘language’ of the results grid:Differences-Only by Default: By default the grid contains differences only (with empty folders removed) – if there is only one difference you will see just one item. And the grid always contains all the actions resulting from the comparison – while you can hide an action temporarily within a contracted group it is always present in the grid and will apply to the update or script unless you exclude it by unchecking the action.Equal Objects filter:A toolbar button adds equal objects to the grid. Enabling this is useful if you want to review, for example, unchanged columns alongside the changed columns in a table. Unsupported Actions filter: You can also choose to see unsupported actions – these result from differences for which there is no supported action that can be taken on the target. These typically result from differences in server objects or built-in types between schema versions.Action Icons: Actions (Add, Change, and Delete) are visualized using icons, making it easier to absorb a set of changes at a glance. The checkbox alongside an icon indicates if the action will be included in the update or generated script. If there is no icon the item will not be included in an update or script.Grayed Items: Items that do not contribute to the update are grayed – excluded actions, unsupported actions and equal objects are all grayed. Folders are grayed when all their contents are grayed making it easy to see when a group of differences have all been excluded without you needing to drill in.Grouping: By default, items are grouped by action so you can quickly assess what changes will be made on update. You can also group the results by object type or by schema. You can expand or collapse a group to temporarily hide detail, and you can exclude all or include all objects in a group.Refactor Highlighting: Schema Compare processes the refactor log if present when targeting a database. Refactoring is indicated in the grid as a change action with the source name bolded to highlight the new schema and/or name. Refactoring will cause objects to be renamed in the database. Refactoring sometimes also shows up as a second order effect on other objects that SQL Server will modify when applying the rename. These will not be marked as actions in the grid as you cannot exclude them, but you will see the changed script if you select the affected object. Probably the biggest set of changes affects the script difference pane. While the grid provides a great overview, to see all changes to an object in the grid you have to fully expand it, which, can quickly clutter the view if you're reviewing many objects. To address this we’ve focused more attention on the script differencing experience – after all, you are writing and editing object scripts to begin with. Changes include:Expanded Object Scripts: The script difference pane now shows the combined scripts for an object and its hierarchical children. This gives a complete picture of all the changes affecting an object in one easy-to-scan place. To complement this, the Next and Previous buttons step between top-level objects only. Together, these two changes can dramatically simplify scanning through the results of a comparison. Enhanced Script Differencing: The script difference algorithm now treats child objects as discrete entities, more effectively highlighting those that have been added, deleted or changed. The color scheme is now more subtle and better reinforces the direction of changes. And remember that you can expand the script pane or swap it to the top – so you can easily optimize the layout to better focus on reviewing scripts.The screen shot below highlights many of these improvements.