97
PHP Barcelona Conference - 30th October 2010 Harrie Verveer Database Version Control Without Pain zaterdag 30 oktober 2010

Database version control without pain - the PHP Barcelona version

Embed Size (px)

DESCRIPTION

The slides for the talk "database version control without pain" as presented at PHP Barcelona, October 30th 2010

Citation preview

Page 1: Database version control without pain - the PHP Barcelona version

PHP Barcelona Conference - 30th October 2010Harrie Verveer

Database Version Control Without Pain

zaterdag 30 oktober 2010

Page 2: Database version control without pain - the PHP Barcelona version

Oh hai!

2

Harrie Verveeraka Hairy

DutchyI live in a town called “Goes”

Software engineer at ibuildings since 2004Vlissingen (Flushing) office

zaterdag 30 oktober 2010

Page 3: Database version control without pain - the PHP Barcelona version

http://www.mediawiki.org/wiki/File:Europe_satellite_orthographic.jpghttp://www.mediawiki.org/wiki/File:Europe_satellite_orthographic.jpg

zaterdag 30 oktober 2010

Page 4: Database version control without pain - the PHP Barcelona version

zaterdag 30 oktober 2010

Page 5: Database version control without pain - the PHP Barcelona version

zaterdag 30 oktober 2010

Page 6: Database version control without pain - the PHP Barcelona version

zaterdag 30 oktober 2010

Page 7: Database version control without pain - the PHP Barcelona version

zaterdag 30 oktober 2010

Page 8: Database version control without pain - the PHP Barcelona version

zaterdag 30 oktober 2010

Page 9: Database version control without pain - the PHP Barcelona version

zaterdag 30 oktober 2010

Page 10: Database version control without pain - the PHP Barcelona version

zaterdag 30 oktober 2010

Page 11: Database version control without pain - the PHP Barcelona version

Database Version ControlWhat’s the problem?

8zaterdag 30 oktober 2010

Page 12: Database version control without pain - the PHP Barcelona version

http://www.sxc.hu/photo/1207911

zaterdag 30 oktober 2010

Page 13: Database version control without pain - the PHP Barcelona version

http

://en

.wik

iped

ia.o

rg/w

iki/F

ile:S

TS1

20La

unch

HiR

es.jp

g

zaterdag 30 oktober 2010

Page 14: Database version control without pain - the PHP Barcelona version

http://www.flickr.com/photos/kt/1217157/

zaterdag 30 oktober 2010

Page 15: Database version control without pain - the PHP Barcelona version

http://www.sxc.hu/photo/1255121

zaterdag 30 oktober 2010

Page 16: Database version control without pain - the PHP Barcelona version

Why Database Version Control

13zaterdag 30 oktober 2010

Page 17: Database version control without pain - the PHP Barcelona version

Simple Patching Strategy

14

The basic idea

zaterdag 30 oktober 2010

Page 18: Database version control without pain - the PHP Barcelona version

Simple patching strategy

15zaterdag 30 oktober 2010

Page 19: Database version control without pain - the PHP Barcelona version

Example patch file

16

patch-001.sql

zaterdag 30 oktober 2010

Page 20: Database version control without pain - the PHP Barcelona version

http://www.sxc.hu/photo/314594

zaterdag 30 oktober 2010

Page 21: Database version control without pain - the PHP Barcelona version

Simple patching strategy

18

Development

Write codeWrite DB patches

Apply patches to own DB

Update

Apply patches Commit

zaterdag 30 oktober 2010

Page 22: Database version control without pain - the PHP Barcelona version

Fixing patches

19

patch-002.sql

zaterdag 30 oktober 2010

Page 23: Database version control without pain - the PHP Barcelona version

Fixing patches

19

patch-002.sql

zaterdag 30 oktober 2010

Page 24: Database version control without pain - the PHP Barcelona version

Fixing patches

19

patch-002.sql

zaterdag 30 oktober 2010

Page 25: Database version control without pain - the PHP Barcelona version

Fixing patches

20

patch-003.sql

zaterdag 30 oktober 2010

Page 26: Database version control without pain - the PHP Barcelona version

Fixing patches

20

patch-003.sql

Never modify a patch file onceit’s under version control

zaterdag 30 oktober 2010

Page 27: Database version control without pain - the PHP Barcelona version

Undo patch files

21

undo-001.sql

zaterdag 30 oktober 2010

Page 28: Database version control without pain - the PHP Barcelona version

http://www.freefoto.com/preview/41-07-7zaterdag 30 oktober 2010

Page 29: Database version control without pain - the PHP Barcelona version

Bonus points

Install.sql

23zaterdag 30 oktober 2010

Page 30: Database version control without pain - the PHP Barcelona version

Bonus points

Initial dummy content

24zaterdag 30 oktober 2010

Page 31: Database version control without pain - the PHP Barcelona version

http://www.sxc.hu/photo/643214

zaterdag 30 oktober 2010

Page 32: Database version control without pain - the PHP Barcelona version

http://www.sxc.hu/photo/946855

zaterdag 30 oktober 2010

Page 33: Database version control without pain - the PHP Barcelona version

Automating the processFaster & Safer

27zaterdag 30 oktober 2010

Page 34: Database version control without pain - the PHP Barcelona version

Typical patch script

28zaterdag 30 oktober 2010

Page 35: Database version control without pain - the PHP Barcelona version

Typical patch script

29

update.sh

zaterdag 30 oktober 2010

Page 36: Database version control without pain - the PHP Barcelona version

BranchesMerge problems

30zaterdag 30 oktober 2010

Page 37: Database version control without pain - the PHP Barcelona version

Branches

31

Trunk

patch-001.sql

patch-002.sql

patch-003.sql

zaterdag 30 oktober 2010

Page 38: Database version control without pain - the PHP Barcelona version

Trunk

patch-001.sql

patch-002.sql

patch-003.sql

create branch

patch-004.sql

patch-005.sqlpatch-004.sql

patch-005.sql

Branch A

Merge

Branches

32zaterdag 30 oktober 2010

Page 39: Database version control without pain - the PHP Barcelona version

Trunk

patch-001.sql

patch-002.sql

patch-003.sql

create branch

patch-004.sql

patch-005.sqlpatch-004.sql

patch-005.sql

Branch A

Merge

Branches

32

patch-006.sql

patch-007.sql

zaterdag 30 oktober 2010

Page 40: Database version control without pain - the PHP Barcelona version

Branches

33

Trunk

patch-001.sql

patch-002.sql

patch-003.sql

create branch

patch-004.sql

patch-005.sql

patch-004.sql

patch-005.sql

Branch B

create branch

patch-002.sql

patch-003.sql

Branch A

Merge

patch-004.sql

Merge

patch-005.sql

Merge

Merge

zaterdag 30 oktober 2010

Page 41: Database version control without pain - the PHP Barcelona version

Separate sequence

db/patches/trunk/

patch-001.sqlpatch-002.sqlpatch-003.sql

34

db/patches/branchA/

patch-001.sqlpatch-002.sql

zaterdag 30 oktober 2010

Page 42: Database version control without pain - the PHP Barcelona version

Separate sequence

db/patches/trunk/

patch-001.sqlpatch-002.sqlpatch-003.sql

34

db/patches/branchA/

patch-001.sqlpatch-002.sql

Trunk version 3BranchA version 2

zaterdag 30 oktober 2010

Page 43: Database version control without pain - the PHP Barcelona version

Patchfile naming

35

patch_20100611_1430.sql

zaterdag 30 oktober 2010

Page 44: Database version control without pain - the PHP Barcelona version

Patch log

36zaterdag 30 oktober 2010

Page 45: Database version control without pain - the PHP Barcelona version

http://www.sxc.hu/photo/692740

zaterdag 30 oktober 2010

Page 46: Database version control without pain - the PHP Barcelona version

There is no silver bulletWhy it can’t exist

38zaterdag 30 oktober 2010

Page 47: Database version control without pain - the PHP Barcelona version

http://www.flickr.com/photos/eschipul/4160817135/

zaterdag 30 oktober 2010

Page 48: Database version control without pain - the PHP Barcelona version

Comparing tables

40

Table in DB 1Table in DB 1

column 1 int

column 2 varchar(255)

column 3 varchar(255)

Table in DB 2Table in DB 2

column 1 int

column 2 varchar(255)

column 4 varchar(255)

zaterdag 30 oktober 2010

Page 49: Database version control without pain - the PHP Barcelona version

Comparing tables

41

Table in DB 1Table in DB 1

id int

name varchar(255)

city varchar(255)

Table in DB 2Table in DB 2

id int

name varchar(255)

website varchar(255)

zaterdag 30 oktober 2010

Page 50: Database version control without pain - the PHP Barcelona version

Comparing tables

42

Table in DB 1Table in DB 1

id int

firstname varchar(255)

lname varchar(255)

Table in DB 2Table in DB 2

id int

firstname varchar(255)

lastname varchar(255)

zaterdag 30 oktober 2010

Page 51: Database version control without pain - the PHP Barcelona version

43

To synchronize two database structures without unexpected

dataloss, communicating all steps needed to get from A to B is

inevitable.

zaterdag 30 oktober 2010

Page 52: Database version control without pain - the PHP Barcelona version

Phing & DB Deploy

44zaterdag 30 oktober 2010

Page 53: Database version control without pain - the PHP Barcelona version

Phing & DB Deploy

45Cartoon by Oliver Widder - http://geekandpoke.typepad.com/zaterdag 30 oktober 2010

Page 54: Database version control without pain - the PHP Barcelona version

46

http://phing.info/

zaterdag 30 oktober 2010

Page 55: Database version control without pain - the PHP Barcelona version

Phing

47zaterdag 30 oktober 2010

Page 56: Database version control without pain - the PHP Barcelona version

48zaterdag 30 oktober 2010

Page 57: Database version control without pain - the PHP Barcelona version

DB Deploy

49zaterdag 30 oktober 2010

Page 58: Database version control without pain - the PHP Barcelona version

Phing & DB Deploy

50zaterdag 30 oktober 2010

Page 59: Database version control without pain - the PHP Barcelona version

DB Deploy

51

db/patches/1-create_user_table.sql

zaterdag 30 oktober 2010

Page 60: Database version control without pain - the PHP Barcelona version

DB Deploy

52

changelog table:

zaterdag 30 oktober 2010

Page 61: Database version control without pain - the PHP Barcelona version

DB Deploy

52

changelog table:

zaterdag 30 oktober 2010

Page 62: Database version control without pain - the PHP Barcelona version

DB Deploy

53

PHP

zaterdag 30 oktober 2010

Page 63: Database version control without pain - the PHP Barcelona version

DB Deploy

54

SQL

zaterdag 30 oktober 2010

Page 64: Database version control without pain - the PHP Barcelona version

DB Deploy

55

Features

zaterdag 30 oktober 2010

Page 65: Database version control without pain - the PHP Barcelona version

LiquibaseThe most complete solution I know

56zaterdag 30 oktober 2010

Page 66: Database version control without pain - the PHP Barcelona version

Liquibase

57

www.liquibase.org

zaterdag 30 oktober 2010

Page 67: Database version control without pain - the PHP Barcelona version

Liquibase

58zaterdag 30 oktober 2010

Page 68: Database version control without pain - the PHP Barcelona version

Liquibase

59zaterdag 30 oktober 2010

Page 69: Database version control without pain - the PHP Barcelona version

Liquibase

60zaterdag 30 oktober 2010

Page 70: Database version control without pain - the PHP Barcelona version

Liquibase

Updating

Reverting

Tagging

Generate XML

Diff

61zaterdag 30 oktober 2010

Page 71: Database version control without pain - the PHP Barcelona version

http://www.sxc.hu/photo/1241520

zaterdag 30 oktober 2010

Page 72: Database version control without pain - the PHP Barcelona version

Liquibase

Branches

63zaterdag 30 oktober 2010

Page 73: Database version control without pain - the PHP Barcelona version

Liquibase

DBMS Support

64zaterdag 30 oktober 2010

Page 74: Database version control without pain - the PHP Barcelona version

Liquibase

Documentation

65zaterdag 30 oktober 2010

Page 75: Database version control without pain - the PHP Barcelona version

Liquibase

Java

66zaterdag 30 oktober 2010

Page 76: Database version control without pain - the PHP Barcelona version

Akrabat DB Schema ManagerZend Framework specific solution

67zaterdag 30 oktober 2010

Page 77: Database version control without pain - the PHP Barcelona version

Akrabat DB Schema Manager

Zend Framework

Proposed by Rob Allen in 2006

Recently implemented

http://github.com/akrabat/Akrabat

68zaterdag 30 oktober 2010

Page 78: Database version control without pain - the PHP Barcelona version

Writing the patch

69

scripts/migrations/001-CreateUserTable.php

zaterdag 30 oktober 2010

Page 79: Database version control without pain - the PHP Barcelona version

Writing the patch

70

scripts/migrations/001-CreateUserTable.php

zaterdag 30 oktober 2010

Page 80: Database version control without pain - the PHP Barcelona version

Akrabat DB Schema Manager

PHP Patches

71zaterdag 30 oktober 2010

Page 81: Database version control without pain - the PHP Barcelona version

Akrabat DB Schema Manager

Easy syntax if you know ZF

72zaterdag 30 oktober 2010

Page 82: Database version control without pain - the PHP Barcelona version

Akrabat DB Schema Manager

Easy to install, easy to use

73zaterdag 30 oktober 2010

Page 83: Database version control without pain - the PHP Barcelona version

Akrabat DB Schema Manager

Easy to install, easy to use

73zaterdag 30 oktober 2010

Page 84: Database version control without pain - the PHP Barcelona version

Akrabat DB Schema Manager

Branch merging

74zaterdag 30 oktober 2010

Page 85: Database version control without pain - the PHP Barcelona version

Doctrine MigrationsDoctrine ORM specific solution

75zaterdag 30 oktober 2010

Page 86: Database version control without pain - the PHP Barcelona version

Doctrine Migrations

76

http://www.doctrine-project.org/

zaterdag 30 oktober 2010

Page 87: Database version control without pain - the PHP Barcelona version

Doctrine Migrations Patch

77zaterdag 30 oktober 2010

Page 88: Database version control without pain - the PHP Barcelona version

Doctrine Migrations

78

Update-to-version

./doctrine migrate

zaterdag 30 oktober 2010

Page 89: Database version control without pain - the PHP Barcelona version

YAML

79zaterdag 30 oktober 2010

Page 90: Database version control without pain - the PHP Barcelona version

YAML

79

./doctrine generate-migrations-diff

zaterdag 30 oktober 2010

Page 91: Database version control without pain - the PHP Barcelona version

Doctrine Migrations

80zaterdag 30 oktober 2010

Page 92: Database version control without pain - the PHP Barcelona version

Doctrine Migrations

81

Generate patches

zaterdag 30 oktober 2010

Page 93: Database version control without pain - the PHP Barcelona version

Doctrine Migrations

82

Useful if you’re already using Doctrine ORM

zaterdag 30 oktober 2010

Page 94: Database version control without pain - the PHP Barcelona version

Summary

83zaterdag 30 oktober 2010

Page 95: Database version control without pain - the PHP Barcelona version

Summary

Patchfiles• Automating

• Branch merging

• Why there is no silver bullet

Phing + DB Deployhttp://phing.infohttp://dbdeploy.com

Liquibasehttp://www.liquibase.org

84

Akrabat DB Schema Managerhttp://akrabat.comhttp://github.com/akrabat/Akrabat

Doctrine Migrationshttp://www.doctrine-project.org

zaterdag 30 oktober 2010

Page 96: Database version control without pain - the PHP Barcelona version

Questions ?

85zaterdag 30 oktober 2010

Page 97: Database version control without pain - the PHP Barcelona version

86

Contact me

Harrie VerveerSoftware Engineer at Ibuildings

Blog:www.harrieverveer.com

E-mail:[email protected]

Skype:harrie-ibuildings

Twitter:@harrieverveer

zaterdag 30 oktober 2010