State-based or migration- based database development? A review of the pros and cons.

44
State-based or migration-based database development? A review of the pros and cons

Transcript of State-based or migration- based database development? A review of the pros and cons.

Page 1: State-based or migration- based database development? A review of the pros and cons.

State-based or migration-based

database development?

A review of the pros and cons

Page 3: State-based or migration- based database development? A review of the pros and cons.

SQL Relay Birmingham29th October 2014

@_AlexYates_

workingwithdevs.com

uk.linkedin.com/in/alexanderyates

[email protected]

Page 4: State-based or migration- based database development? A review of the pros and cons.

SQL Relay Newcastle27th October 2014

@_AlexYates_

workingwithdevs.com

uk.linkedin.com/in/alexanderyates

[email protected]

Page 5: State-based or migration- based database development? A review of the pros and cons.

SQL Relay Birmingham29th October 2014

@_AlexYates_

workingwithdevs.com

uk.linkedin.com/in/alexanderyates

[email protected]

Page 6: State-based or migration- based database development? A review of the pros and cons.

#devops

ProductionDBA

ChiefArchitect

.NET Devteam lead

(Partnersin crime)

SQL Devteam lead

Page 7: State-based or migration- based database development? A review of the pros and cons.

Reliability Safe

deployments Fire prevention

over firefighting

Visibility Visualise

pipeline Better

coordination

DevOps DBA + devs

work together Automation Agility

1 2 3

Goals for project

Page 8: State-based or migration- based database development? A review of the pros and cons.
Page 9: State-based or migration- based database development? A review of the pros and cons.
Page 10: State-based or migration- based database development? A review of the pros and cons.

There’s more than one way to skin a cat!

Page 11: State-based or migration- based database development? A review of the pros and cons.

V1 V2

Page 12: State-based or migration- based database development? A review of the pros and cons.

Migrations based solutions

V1 V2

Page 13: State-based or migration- based database development? A review of the pros and cons.

State based solutions

V1 V2

Page 14: State-based or migration- based database development? A review of the pros and cons.
Page 15: State-based or migration- based database development? A review of the pros and cons.

“There's nothing more reliable than keeping track of exactly the

scripts you intend to run, and running them, without trying to

compare state and guess.” Paul Stovell, built Octopus Deploy

Page 16: State-based or migration- based database development? A review of the pros and cons.

“There's nothing more reliable than keeping track of exactly the

scripts you intend to run, and running them, without trying to

compare state and guess.” Paul Stovell, built Octopus Deploy

“As soon as you have multiple changes on a single aspect of an object, ordering and the ability to detect which change

needs to be made gets very complicated.”

Gert Drapers, built DataDude

Page 17: State-based or migration- based database development? A review of the pros and cons.

Demo: the happy path!

SOURCE CONTROL

CONTINUOUSINTEGRATION

AUTOMATEDDEPLOYMENT

Page 18: State-based or migration- based database development? A review of the pros and cons.

11

2 Table rename Table ‘foo’ is to be renamed ‘bar’

Edit stored procedure Myproc to select col2 from table ‘foo’

(currently it selects only col1)

Task

Page 19: State-based or migration- based database development? A review of the pros and cons.
Page 20: State-based or migration- based database development? A review of the pros and cons.

0124_edit_sproc.sql

ALTER PROCEDURE myprocAS BEGIN SELECT col1, col2 FROM table_foo END

Page 21: State-based or migration- based database development? A review of the pros and cons.

0124_edit_sproc.sql 0125_table_rename.sqlEXEC sp_rename 'table_foo', 'table_bar‘

ALTER PROCEDURE myprocAS BEGIN SELECT col1 FROM table_bar END

ALTER PROCEDURE myprocAS BEGIN SELECT col1, col2 FROM table_foo END

Page 22: State-based or migration- based database development? A review of the pros and cons.

0124_edit_sproc.sql 0125_table_rename.sqlEXEC sp_rename 'table_foo', 'table_bar‘

ALTER PROCEDURE myprocAS BEGIN SELECT col1 FROM table_bar END

ALTER PROCEDURE myprocAS BEGIN SELECT col1, col2 FROM table_foo END !

Page 23: State-based or migration- based database development? A review of the pros and cons.

Conflicts easily missed Changes overwritten

Order matters Last script wins

To review changes Much reading, but very

important to get code right

The problem with migrations

Page 24: State-based or migration- based database development? A review of the pros and cons.

V123

myproc.sqlCREATE PROCEDURE myprocAS SELECT col1 FROM table_foo

!

Page 25: State-based or migration- based database development? A review of the pros and cons.

V123

myproc.sqlCREATE PROCEDURE myprocAS SELECT col1 FROM table_foo

V124

myproc.sqlCREATE PROCEDURE myprocAS SELECT col1, col2 FROM table_foo

Page 26: State-based or migration- based database development? A review of the pros and cons.

V123

myproc.sqlCREATE PROCEDURE myprocAS SELECT col1 FROM table_foo

V124

myproc.sqlCREATE PROCEDURE myprocAS SELECT col1, col2 FROM table_foo

V125

myproc.sqlCREATE PROCEDURE myprocAS SELECT col1 FROM table_bar

Page 27: State-based or migration- based database development? A review of the pros and cons.

V123

myproc.sqlCREATE PROCEDURE myprocAS SELECT col1 FROM table_foo

V124

myproc.sqlCREATE PROCEDURE myprocAS SELECT col1, col2 FROM table_foo

V125

myproc.sqlCREATE PROCEDURE myprocAS SELECT col1 FROM table_bar

!

Page 28: State-based or migration- based database development? A review of the pros and cons.
Page 29: State-based or migration- based database development? A review of the pros and cons.

REVISION 123 REVISION 124

table_foo.sqlCREATE TABLE table_foo(col1 NVARCHAR(max), col2 NVARCHAR(max)) myproc.sqlCREATE PROCEDURE myprocAS SELECT col1 FROM table_foo

table_bar.sqlCREATE TABLE table_bar(col1 NVARCHAR(max), col2 NVARCHAR(max)) myproc.sqlCREATE PROCEDURE myprocAS SELECT col1, col2 FROM table_bar

Page 30: State-based or migration- based database development? A review of the pros and cons.

Diff script:

DROP TABLE table_foo

CREATE TABLE table_bar(col1 NVARCHAR(max), col2 NVARCHAR(max))

ALTER PROCEDURE myprocAS  SELECT col1, col2 FROM table_bar

The problem with state

Page 31: State-based or migration- based database development? A review of the pros and cons.

Diff script

DROP TABLE table_foo

CREATE TABLE table_bar(col1 NVARCHAR(max), col2 NVARCHAR(max))

ALTER PROCEDURE myprocAS  SELECT col1, col2 FROM table_bar

!

The problem with state

Page 32: State-based or migration- based database development? A review of the pros and cons.

Script

DROP TABLE table_foo

CREATE TABLE table_bar(col1 NVARCHAR(max), col2 NVARCHAR(max))

ALTER PROCEDURE myprocAS SELECT col1, col2 FROM table_bar

Need to understand your tool It should be obvious to you that

your tool won’t work

What is the Plan B / override? Because one day you’ll need it

Test for data loss Automatically (naturally)

The problem with state

Page 33: State-based or migration- based database development? A review of the pros and cons.

One more thing…

Page 34: State-based or migration- based database development? A review of the pros and cons.
Page 35: State-based or migration- based database development? A review of the pros and cons.

State

Easier (less control)

Better for sprocs/functions

Better for large/distributed teams

Better for frequent changes

Better for dependency nightmares

Drift: rolled back

Migrations

More control (harder/needs discipline)

Better for data migrations

Better for small teams

Better for infrequent changes

Better for simple data stores

Drift: ignored

So what is better?

Page 36: State-based or migration- based database development? A review of the pros and cons.

State (+migrations)

SSDT pre/post deploy scripts

Redgate migrations

Migrations (+state)

ReadyRoll ‘deploy changes’

Best of both worlds?

Page 37: State-based or migration- based database development? A review of the pros and cons.
Page 38: State-based or migration- based database development? A review of the pros and cons.

Reliability Safe

deployments Fire prevention

over firefighting

1

Goals for project

Page 39: State-based or migration- based database development? A review of the pros and cons.
Page 40: State-based or migration- based database development? A review of the pros and cons.

Reliability Safe

deployments Fire prevention

over firefighting

Visibility Visualise

pipeline Better

coordination

1 2

Goals for project

Page 41: State-based or migration- based database development? A review of the pros and cons.

Reliability Safe

deployments Fire prevention

over firefighting

Visibility Visualise

pipeline Better

coordination

DevOps DBA + devs

work together Automation Agility

1 2 3

Goals for project

Page 42: State-based or migration- based database development? A review of the pros and cons.
Page 43: State-based or migration- based database development? A review of the pros and cons.

@_AlexYates_

workingwithdevs.com/delivering-databases-migrations-vs-state

uk.linkedin.com/in/alexanderyates

[email protected]

Page 44: State-based or migration- based database development? A review of the pros and cons.

Image sourcesAuthor Source Information

Chiltepinster Wikimedia Commons Mocking Bird Argument.jpg – Wikimedia Commons. This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license. Source on Wikimedia Commons: “Own work”

Tableatny Wikimedia Commons Athlete at Starting block.jpg – Wikimedia Commons. This file is licensed under the Creative Commons Attribution 2.0 Generic license. Source on Wikimedia Commons: “BXP135671”

Henry Mühlpfordt Flickr CERN Atlas Control Room 2010-07-01 – Flickr. This file is licensed under the Creative Commons Attribution-ShareAlike 2.0 Generic license.

Department for Business, Innovation and Skills

Flickr Toyota’s new Auris – Flickr. This file is licensed under the Creative Commons Attribution-NoDerivs 2.0 Generic license.

Stephen Wolfe Flickr Violinists – Flickr. This file is licensed under the Creative Commons Attribution 2.0 Generic license.

Akira Hsu Flickr The Micro Four Thirds Logo by Empire Elite Stormtroopers - for the great “Micro Four Thirds Day” – Flickr. This file is licensed under the Creative Commons Attribution 2.0 Generic license.

Nils Rinaldi Flickr Hippo fight 2/3 – Flickr. This file is licensed under the Creative Commons Attribution 2.0 Generic license.

My own collection All pictures are either of me, taken by friends/colleagues, or taken by me

All pictures on about me slide, including kitten, and team at Farm Credit Services of America (FCSA). FCSA have allowed me/Redgate to reference the visit in presentations/marketing material etc.

Memegenerator.net Memegenerator.net I don’t always edit database. Content designed to be shared and delivered with credit to memegenerator.net.

Ctrl.Alt.Design ctrla.lt Social Media share icons