Download Simple version control

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

IMDb wikipedia , lookup

Relational algebra wikipedia , lookup

DBase wikipedia , lookup

Concurrency control wikipedia , lookup

Functional Database Model wikipedia , lookup

Oracle Database wikipedia , lookup

Team Foundation Server wikipedia , lookup

Microsoft Access wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Ingres (database) wikipedia , lookup

Tandem Computers wikipedia , lookup

Database wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Btrieve wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Null (SQL) wikipedia , lookup

Relational model wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
Simple version control
For SQL Server database
Oleksii Kovalov
Why to use version control?





Safely store database source code
Maintain source history
Team development
Continuous delivery
Complex software development
Ways to keep database source code




Data modeling tools
System metadata
Magic
Scripts
Data modeling tools
 PowerDesigner, ERWin, ER Studio
 Pros
 True way to make data model!
 Nice diagrams
 Cons
 Costs lots of money
 Requires special skills
 Stores data model in binary format – no more version control 
System metadata
Schema definition in some domestic format (eg XML) operated
by hand-made framework
 Pros
 Reliable and stable
 As flexible as you need
 Is under yours full control
 Cons
 Too flexible or insufficiently flexible
 All is under your control – if you need something you are forced to
implement it
Magic
Third-party tools to establish version control over your database
 Red Gate SQL Source Control
 Devart DBForge
 Pros
 Works like a magic!
 Cons




Nobody really knows how it works – it is magic!
So – if you are not a witch – you are unable to control it
Costs a money
Works with specific databases only (SQL Server, eg)
Scripts
Native way of true sql jedi
 Pros






Most natural way to keep your code
Works for all kinds and types of dbms
Most powerful and flexible
If something can not be done via script – it can not be done at all.
Fits well all source control systems
No power in the universe can stop script
 Cons
 You need to be sql jedi
 You need to know how to operate text editor (no more “click and drag table”)
And the winner is…….
Scripts!!!!!
Trunk it. Branch it. Merge it. Tag it
 Trunk – main line of development
 Branch – side line of development
 Tag – specific “release”
Trunk it. Branch it. Merge it. Tag it
Tag
R1.2
Trunk 1.0
v 1.1
Branch
v1.2
V1.3
V1.4
Branch
Branching
 Branch by feature
 Make branch of main
 Develop some feature in branch
 Merge branch back to main
 Make release branch from main
 Branch by developer
 Each developer has its own branch of database, even when
working on the same feature
First step. Create database
MyLibrary
 Create
 CreateDatabase.sql
 DatabaseOptions.sql
 Use.sql
create database [MyLibrary]
go
alter database [MyLibrary]
set recovery simple
Go
Use [MyLibrary]
Go
Data schema
 MyLibrary
 Create
 CreateDatabase.sql
 DatabaseOptions.sql
 Use.sql
 DDL
 Author.sql
 Book.sql
Create table dbo.Authors
(
Id int identitity not null
,Name varchar(128) not null
)
Go
Create unique clustered index [IX:Author(Id)]
on dbo.Author(Id)
Go
Create table dbo.Book
(
Id int identity not null
,AuthorId int null
,Name varchar(128)
)
Go
Create index [IX:Book(Id)] on dbo.Book(Id)
Go
alter table dbo.Book
add constraint [Book(AuthorId)->Author]
foreign key (AuthorId) references dbo.Author(AuthorId)
go
References
MyLibrary
 Create
 CreateDatabase.sql
 DatabaseOptions.sql
 Use.sql
 DDL
 Authors.sql
 Books.sql
 DatabaseVersion.sql
 FKS
 Books.sql
alter table dbo.Book
add constraint [Book(AuthorId)->Author]
foreign key (AuthorId)
references dbo.Author(AuthorId)
go
Code
MyLibrary
 Create



CreateDatabase.sql
DatabaseOptions.sql
Use.sql
 DDL



Authors.sql
Books.sql
DatabaseVersion.sql
 FKS

Books.sql
 Code

Common

SelectAuthors.sql
if object_id('dbo.SelectAuthors') is not null
drop procedure dbo.SelectAuthors
go
create procedure dbo.SelectAuthors
as begin
select AuthorId,Name
from dbo.SelectAuthors;
return 0;
end
go
Reference and initial data
MyLibrary
 Create
 CreateDatabase.sql
 DatabaseOptions.sql
 Use.sql
 DDL
 Authors.sql
 Books.sql
 DatabaseVersion.sql
 FKS
 Books.sql
 Code
 Common
 SelectAuthors.sql
 Data
 InitialData
 Author.sql
insert into dbo.Author(Name)
values('Twain, Mark')
go
insert into dbo.Author(Name)
values('Saberhagen, Fred')
go
insert into dbo.Author(Name)
values('Polo, Mark')
go
Reference data - problem
Create table dbo.Author
(
Id int identitity not null
,Name varchar(128) not null
)
Go
insert into dbo.Author(Name)
values('Twain, Mark')
go
Create table dbo.Author
(
Id int identitity not null
,Name varchar(128) not null
,Country varchar(128) not null
)
Go
insert into dbo.Author(Name,Country)
values('Twain, Mark‘,’USA’)
go
Reference data – solution: XML
Select
Name as [@name]
,Country as [@Country]
From dbo.Author
for xml path('r'), root('root')
go
<root>
<r Name="Twain, Mark"
Country="NA" />
<r Name="Saberhagen,
Fred" Country="NA" />
<r Name="Polo, Mark"
Country="NA" />
</root>
Reference data – solution: XML
declare @xml xml
set @xml = '<root>
<r Name="Twain, Mark" Country="NA" />
<r Name="Saberhagen, Fred“ Country="NA" />
<r Name="Polo, Mark" Country="NA" />
</root>'
insert into dbo.Author(Name,Country)
Select b.value('@Name','varchar(128)')
,b.value('@Country','varchar(128)')
[email protected]('/root/r') a(b)
go
Building database

MyLibrary

Create
 CreateDatabase.sql
 DatabaseOptions.sql
 Use.sql

DDL
 Authors.sql
 Books.sql

FKS
 Books.sql

Code
 Common
 SelectAuthors.sql

Data
 InitialData

Author.sql
000-database.sql
100-ddl.sql
200-fks.sql
300-code.sql
400-data.sql
!Final.sql
Merge.bat
del *.sql
copy ..\Create\CreateDatabase.sql +
..\Create\DatabaseOptions.sql + ..\Create\use.sql 000create.sql
copy ..\ddl\*.sql 100-ddl.sql
copy ..\fks\*.sql 200-fks.sql
copy ..\code\common\*.sql 300-code.sql
copy ..\data\initialdata\*.sql 400-data.sql
copy *.sql !final.sql
SqlCmd variable substitution
 Why to use
 Dynamically change database properties
 Database name
 Server name
 Etc
 How to use
 create database [$(dbname)];
 Sqlcmd -i !Final.sql -v dbname=MyLib.trunk
 set dbname=MyLib.trunk
 Sqlcmd -i !Final.sql
Multibase applications
 Hardcoded three-part-name
 Archive.dbo.Author
 ->synonym
 Redirect synonym
 Many database from different branches on the same
server
 Useful to compare changes with Database Compare Tools
(RedGate compare, DevArt dbForge)
 Use sqlcmd substitution to change database name
Branching
 Make new developer branch dev01 from trunk
 To develop some new killer feature
 Develop new feature in branch
Merging
 Diff and merge tools





MS SDK – windiff.exe
WinMerge
DevArt Code Compare
Visual Studio vsDiffMerge.exe
TortoiseSVN diff tool
Database upgrades
 Code update
 Stored procedures. No problem. Simply replace it. In
alphabetical order.
 Views. Table-valued functions. Not so easy. Must preserve
specific order of creation. Simply name script file with number
prefixes.
 Schema update
 Need migration scripts.
 Keep all your database as set of migration scripts
Migration scripts. Database versions
 Need to know
 Apply diff scripts
 Add or drop specific fields, tables, constraints, indexes
 Almost all data modeling tools produces diff scripts
 It is great to have diff scripts that can be run many times – without
errors
 Don’t need to know
 Simply add tables, columns, indexes that doesn’t exists in target
database
 Drop unneeded columns, tables, indexes, etc.
 Redgate automation pack
Table – add, drop, alter columns. Tips
 Always add all new columns, indexes, FKs to bottom of
table definition or at the end of script
Table–add,drop,alter,rename column. Column default
 Alter table add [column name] [data type] [not] [null]
 All newly added not-null columns should have default to
prevent errors when adding to table with data.
 If no default value is possible – add column as nullable,
fill it with appropriate data and then alter to not null.
Transactions in update scripts
•Pros
• All changes are atomic and can be rolled back in case
of error
•Cons
• Can consume a lot of disk space for log file when
applied to large database
• Atomic – script cannot be partially re-runned in case of
error
30 |
3/17/2015
Footer Goes Here
|
Few words on testing upgrades
•Test upgrades from all released version
•Test upgrades on different customer data pattern
31 |
3/17/2015
Footer Goes Here
|
Useful utility procedures
 Drop all statistics
 Drop column
 If exists
 Drop statistics that belong on columns
 Drop (if any)
 Statistics
 Foreign key
 Index
 Drop (if exists)
 Index
 Foreign key constraint
Спасибо за
участие!