45
Wersjonowanie baz danych podczas developmentu Liquibase Piotr Pelczar [email protected]

Liquibase - database structure versioning

Embed Size (px)

Citation preview

Page 1: Liquibase - database structure versioning

Wersjonowanie baz danych podczas developmentu

Liquibase

Piotr [email protected]

Page 2: Liquibase - database structure versioning

Piotr ’Athlan’ Pelczar

Freelancer: vgroup.pl, athlan.plgoldenline.pl/[email protected]

Page 3: Liquibase - database structure versioning

Agenda

1. Problem wersjonowania struktur baz danych

2. Przedstawienie narzędzia Liquibase3. Instalacja4. Pierwszy zrzut bazy, tworzenie tagów5. Database diff, tag6. Rollback7. Preconditions8. Dobre praktyki9. Integracja z Maven i Eclipse

Page 4: Liquibase - database structure versioning

Problem

• Baza danych musi być integralna podczas developmentu.

• Jak utrzymać jej integralność?• Jak pozbyć się problemu supportowania

developerów?• Problem branchowania i mergeowania

zmian wprowadzanych podczas równoległego rozwoju aplikacji.

Page 5: Liquibase - database structure versioning

Czym jest Liquibase?

• Niezależna od bazy danych biblioteka.• OpenSource.• Dostępna z linii poleceń.• Do śledzenia i zarządzania zmianami w

bazie danych.• Zmiany są zapisywane w plikach XML.

Page 6: Liquibase - database structure versioning

Wspierane bazy danych

• MySQL• PostgreSQL• Oracle• MS-SQL• Sybase Enterprise• Sybase Anywhere• DB2• Apache Derby

• HSQL• H2• Informix• InterSystems

Caché• Firebird• SAPDB• SQLite

http://www.liquibase.org/databasesWarto się zapoznać z issues.

Page 7: Liquibase - database structure versioning

Format zmian

• Jako plik XML• Obsługuje ponad 40 rodzajów zmian• W tym raw SQL, co jest otwartą furktą• Można uruchamiać komendy shell

http://www.liquibase.org/manual/refactoring_commands

Page 8: Liquibase - database structure versioning

Format zmian

Page 9: Liquibase - database structure versioning

Format zmian

• Zmiany są również przechowywane lokalnie w bazie danych w tabeli databasechangelog

Page 10: Liquibase - database structure versioning

Współbieżność pracy

• Współbieżność jest zagwarantowana poprzez wpisy w tabeli databasechangeloglock

Page 11: Liquibase - database structure versioning

Uruchamianie Liquibase

Możliwość uruchomienia Liquibase z poziomu:

• Ant• Maven• Spring Framework (jako bean w kontekście)• Grails• Servlet Listener• Command Line• Integracja z Hibernate

Page 12: Liquibase - database structure versioning

Instalacja

1. http://www.liquibase.org/download2. Rozpakowujemy3. Pobieramy odpowiedni driver JDBC do

bazy danych, np. postgresql-9.1-902.jdbc4.jar

Page 13: Liquibase - database structure versioning

Instalacja

4. W bieżącym katalogu tworzymy pusty liquibase.properties z wpisem:

classpath=D:\\Programs\\Liquibase\\ postgresql-9.1-

902.jdbc4.jar

Nadpisuje on wartości z linii poleceń, można zmienić lokalizację za pomocą flagi --defaultsFile

http://www.liquibase.org/manual/command_line#using_a_liquibaseproperties_file

Page 14: Liquibase - database structure versioning

Wymagania

1. Java 1.5 lub nowsza2. Poprawnie skonfigurowany classpath oraz

JAVA_HOME3. Tak, aby Java była dostępna z polecenia:

java -version

4. Liquibase jest uruchamiane jako archiwum JAR:

java -jar liquibase.jar

Page 15: Liquibase - database structure versioning

Po rozpakowaniu

Page 16: Liquibase - database structure versioning

Pierwszy zrzut bazy danych

liquibase--url=jdbc:postgresql://localhost:5432/ handlowcy--username postgres--password root--changeLogFile="schema.xml"generateChangeLog

http://www.liquibase.org/manual/generating_changelogs

Page 17: Liquibase - database structure versioning
Page 18: Liquibase - database structure versioning

Pierwszy zrzut bazy danych

Ograniczenia, w sposób automatyczny nie są zrzucane:

• Procedury składowane• Funkcje• Triggey

• Jest możliwość wykonywania swoich zapytań SQL, ale nie będą przenośne.

http://www.liquibase.org/manual/generating_changelogs

Page 19: Liquibase - database structure versioning

Załadowanie schematu bazy

liquibase--url=jdbc:postgresql://localhost:5432/ handlowcy_dev--username postgres--password root--changeLogFile="schema.xml"update

Page 20: Liquibase - database structure versioning

Wygenerowanie zapytań SQL

liquibase--url=jdbc:postgresql://localhost:5432/ handlowcy_dev--username postgres--password root--changeLogFile="schema.xml"updateSQL > changes.sql

Page 21: Liquibase - database structure versioning

Nadeszły zmiany, diff

Sprawdzenie zmian, które nastąpiły:

liquibase--url=jdbc:postgresql://…--username postgres--password root--referenceUrl=jdbc:postgresql://…--referenceUsername postgres--referencePassword rootdiff

Page 22: Liquibase - database structure versioning

Nadeszły zmiany, diff

Page 23: Liquibase - database structure versioning

Generujemy zmiany, diffChangeLog

liquibase--url=jdbc:postgresql://…--username postgres--password root--referenceUrl=jdbc:postgresql://…--referenceUsername postgres--referencePassword root--changeLogFile=changelog-DATE-ath.xmldiffChangeLog

Page 24: Liquibase - database structure versioning

Diff danych

liquibase--url=jdbc:postgresql://…--username postgres--password root--referenceUrl=jdbc:postgresql://…--referenceUsername postgres--referencePassword root--changeLogFile=changelog-DATE-ath.xml--diffTypes=datadiffChangeLog

Page 25: Liquibase - database structure versioning

Diff uwagi

Diff obsługuje:

• Missing/unexpected tables, views, columns• Missing/unexpected primary keys, unique

constraints• Missing/unexpected foreign Keys• Missing/unexpected sequences, indexes• Column definition differences (data type, auto-

increment, etc.)• View definition differences• Data differences (limited), not checked by

default

Page 26: Liquibase - database structure versioning

Nadeszły zmiany, diff

Diff nie obsługuje:

• Non-foreign key constraints (check, etc)• Stored Procedures• Data type length*

* Testowano: Zmiany długości varchar w PostgreSQL są wykrywane.

Page 27: Liquibase - database structure versioning

Nadeszły zmiany, diff

• Ważnym jest, żeby dobrze interpretować „reference”.

Z naszej perspektywy baza produkcyjna jest główną, a rererence jest developerska.

Jeżeli odwrócimy, naturalną konsekwencją jest wygenerowanie DROP’ów zamiast CREATE’ów.

• Umieszczanie daty i nazwy dewelopera w nazwach plików changelogów to dobry nawyk:2012-11-24-athlan.xml.

• Przy zbiorczych change’ach pliki te jednoznacznie są identyfikowane nie tylko w Liquibase, ale na repozytorium git czy SVN, można je łatwiej mergeować.

Page 28: Liquibase - database structure versioning

Nadeszły zmiany, diff

• Raz wykonany changeSet już nigdy nie zostanie powtórzony (identyfikowany po author oraz id).

• Nie ma poprawiania XML na repo.

Page 29: Liquibase - database structure versioning

Tagowanie bazy danych

1. W pliku XML

<tagDatabase tag="NAZWA"/>

2. Z poziomu linii poleceń

liquibase--url=jdbc:postgresql://…--username postgres--password roottag NAZWA

Page 30: Liquibase - database structure versioning

Rollback

Tryb Komenda Tryb SQL

Do taga rollback<tag>

rollbackSQL<tag>

Data rollbackToDate <date>

rollbackToDateSQL <date>

Ilość changeset’ów

rollbackCount <number>

rollbackCountSQL <number>

Page 31: Liquibase - database structure versioning

Rollback

liquibase--url=jdbc:postgresql://…--username postgres--password rootrollback tag

rollbackSQL tag > rollback.sql

Page 32: Liquibase - database structure versioning

Rollback

• Większość operacji ma komplementarne sobie operacje odwrotne.

• Jeżeli nie mają, lub chcemy podjąć inne akcje, do changeset’a dodajemy tag <rollback>

• Są zapisywane w bazie danych.

Page 33: Liquibase - database structure versioning

Rollback

Page 34: Liquibase - database structure versioning

Rollback

• Przykładowy procedure

Page 35: Liquibase - database structure versioning

Preconditions

Warunki, które muszą być spełnione, aby nastąpił cały zestaw changeset’ów lub pojedynczy z nich.

Przydają się, gdy:• Osoba przygotowująca changelog ma jakieś

założenia.• Zapewnia ich przestrzealność.• Umożliwia sprawdzenie danch, bądź

parametrów.• Decyduje, które chengesety są uruchamiane,

a które nie.

Page 36: Liquibase - database structure versioning

Preconditions

Page 37: Liquibase - database structure versioning

Preconditions

Page 38: Liquibase - database structure versioning

Preconditions

Możliwe preconditions:

• columnExists• tableExists• viewExists• indexExists• sequenceExists• primaryKeyExistsdbms• runningAs

• changeSetExecuted• sqlCheck• changeLogPropertyDefin

ed• customPrecondition

(classNameimplementuje interfejs liquibase.precondition.CustomPrecondition)

Page 39: Liquibase - database structure versioning

Dobre praktyki

1. Trzymaj changelog’i spięte <include> zawarte w master.xml i wykonuj master’a

Page 40: Liquibase - database structure versioning

Integracja z Maven: pom.xml• Tworzymy główny pom.xml• Przykład dotyczy modułu common-

model

Page 41: Liquibase - database structure versioning

Integracja z Maven: pom.xml

Page 42: Liquibase - database structure versioning

Integracja z Maven: pom.xml

Page 43: Liquibase - database structure versioning

Integracja z Maven: pom.xmlKompilacja Maven z changesetami:

mvn install:liquibase

Page 44: Liquibase - database structure versioning

Piotr [email protected]

Pytania?

1. Database diff, tag2. Rollback3. Preconditions4. Dobre praktyki5. Integracja z Maven i Eclipse6. Inne…

Page 45: Liquibase - database structure versioning

Wersjonowanie baz danych podczas developmentu

Liquibase

Piotr [email protected]