se2016 java roman ugolnikov "migration and source control for your db"

44
Migrationа and source control for your DB

Upload: inhacking

Post on 14-Apr-2017

53 views

Category:

Software


0 download

TRANSCRIPT

Page 1: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Migrationа and source control for your DB

Page 2: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

what:Database migration: Database structure: - Tables, constrains, indexes;

Database data:

- Initial data like list of post codes,

statuses for order, etc.

Database logic:

- stored procedures, triggers, functions

Page 3: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

why: versionsData Base migration: Data base structure: - Tables, constrains, indexes;

Data base data:

- Initial data like list of post codes,

statuses for order, etc.

Data base logic:

- stored procedures, triggers, functions

v1.0.0

Page 4: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

why: versionsData Base migration: Data base structure: - Tables, constrains, indexes;

Data base data:

v1.0.0

Page 5: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

why: versionsData Base migration: Data base structure: - Tables, constrains, indexes;

Data base data:

- Initial data like list of post co

v1.2.1

v1.0.0

Page 6: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Data Base migration: Data base structure: - Tables, constrains, indexes;

Data base data:

- Initial data like list of post

v1.2.1

v1.6.0

v1.0.0

why: versions

Page 7: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Data Base migration: Data base structure: - Tables, constrains, indexes;

Data base data:

- Initial data like list of post c

v1.2.1

v1.6.0

v2.0.1

v1.0.0

why: versions

Page 8: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Data Base migration: Data base structure: - Tables, constrains, indexes;

Data base data:

- Initial data like list of post codes,

statuses for order, etc.

Data base logic:

- stored procedures, triggers, functions

v1.2.1

v1.6.0

v2.0.1

v1.0.0

why: versions

Page 9: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Data Base migration: Data base structure: - Tables, constrains, indexes;

Data base data:

- Initial data like list of post codes,

statuses for order, etc.

Data base logic:

- stored procedures, triggers, functions

v1.2.1

v1.6.0

v2.0.1

v1.0.0DEV

why: environments

Page 10: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Conclusion

We need version control over our database

migration

Page 11: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Tools

Play framework:migration

Rails ActiveRecord migrations

Mogeez for

Page 12: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Tools

Play framework:migration

Rails ActiveRecord migrations

Mongeez for

Page 13: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Tools

Play framework:migration

Rails ActiveRecord migrations

Mogeez for

Page 14: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Tools

Play framework:migration

Rails ActiveRecord migrations

Mogeez for

Page 15: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Tools

Liquibase :

•Apache license

•Started in 2006 (active)

•http://liquibase.org

Flyway:

•Apache license

•Started in 2010

•http://www.flywaydb.or

g

Page 16: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Common flow

Build Test Run

• Dev• Test• Acceptance• Production

• GUI tests• Integration

tests• API tests• Unit tests

• Ant• Maven• Gradle

Page 17: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Common flow

Build Test Run

• Dev• Test• Acceptance• Production

• GUI tests• Integration

tests• API tests• Unit tests

• Ant• Maven• Gradle

Page 18: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Common flow

Build Test Run

• Java API• Spring Bean

• Plugins• CLI

• Plugins• CLI

Page 19: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

How it worksDatabaseChangelogLock:

DatabaseChangelog:

Page 20: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

How it works

Schema_versions:

Page 21: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

How it works

Sequential change flow1. Init db

2. Add column “type”

3. Add new table

Page 22: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

How it works

Sequential change flow1. Init db

2. Add column “type”

3. Add new tableX

Page 23: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

How it works

Sequential change flow1. Init db

3. Add new table

4. Remove column “type”

2. Add column “type”

Page 24: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

difference?, comparison.Rollback vs no rollback

SQL outputConditions

baseline

Supported modes:https://flywaydb.org/documentation/gradle/Flyway more and esear.

Page 25: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

How it works

V1__Initial_Setup.sql

V1_1__First_Changes.sql

V1_2__Added_user_info.sql

V2__ ….

Page 26: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

How it works

Changes are grouped into changesets: • Change(s) that should be applied atomically

Changesets are grouped into changelogs:•Files managed in version control

Page 27: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

How it works

v1_changeLog.xml

v1.1_changeLog.xml

v2_changeLog.xml

v1.2_changeLog.xml

v1.3_changeLog.xml

v2.1_changeLog.xml

Page 28: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Supported db listMySQLPostgreSQLOracleMSSQL ServerSybase_EnterpriseSybase_AnywhereDB2Apache_DerbyHSQLH2InformixFirebirdSQLite

MySQLPostgreSQLOracle MSSQL ServerSQL AzureMariaDBPhoenixVerticaAWS RedshiftDB2, DB2 z/OSDerbyH2, Hsql, SQLite, SAP HANA, solidDB, Sybase ASE

Page 29: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Supported formats

DSL for database changes:

• XML• YAML• JSON• sql

Pure SQL

Page 30: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Base lineConvention over configuration

Toolbox

Page 31: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Toolbox

• SQL output• Rollback• Context• Precondition

Page 32: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Toolbox: precondition

<preConditions onFail="WARN" onError="MARK_RAN"><dbms type="oracle"/><runningAs username="SYSTEM"/><tableExists tableName=“newTable"/><customPrecondition className=“com.example.CheckOne"/>

<sqlCheck expectedResult="0">select count(*) from oldtable

</sqlCheck></preConditions>

Page 33: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Toolbox: precondition

<preConditions onFail="WARN" onError="MARK_RAN"><dbms type="oracle"/><runningAs username="SYSTEM"/><tableExists tableName=“newTable"/><customPrecondition className=“com.example.CheckOne"/>

<sqlCheck expectedResult="0">select count(*) from oldtable

</sqlCheck></preConditions>

Page 34: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Toolbox: precondition

<preConditions onFail="WARN" onError="MARK_RAN"><dbms type="oracle"/><runningAs username="SYSTEM"/><tableExists tableName=“newTable"/><customPrecondition className=“com.example.CheckOne"/>

<sqlCheck expectedResult="0">select count(*) from oldtable

</sqlCheck></preConditions>

Page 35: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Toolbox: precondition

<preConditions onFail="WARN" onError="MARK_RAN"><dbms type="oracle"/><runningAs username="SYSTEM"/><tableExists tableName=“newTable"/><customPrecondition className=“com.example.CheckOne"/>

<sqlCheck expectedResult="0">select count(*) from oldtable

</sqlCheck></preConditions>

Page 36: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Toolbox: precondition

<preConditions onFail="WARN" onError="MARK_RAN"><dbms type="oracle"/><runningAs username="SYSTEM"/><tableExists tableName=“newTable"/><customPrecondition className=“com.example.CheckOne"/>

<sqlCheck expectedResult="0">select count(*) from oldtable

</sqlCheck></preConditions>

Page 37: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Toolbox: precondition

<preConditions onFail="WARN" onError="MARK_RAN"><dbms type="oracle"/><runningAs username="SYSTEM"/><tableExists tableName=“newTable"/><customPrecondition className=“com.example.CheckOne"/>

<sqlCheck expectedResult="0">select count(*) from oldtable

</sqlCheck></preConditions>

Page 38: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

Toolbox: precondition

<preConditions onFail="WARN" onError="MARK_RAN"><dbms type="oracle"/><runningAs username="SYSTEM"/><tableExists tableName=“newTable"/><customPrecondition className=“com.example.CheckOne"/>

<sqlCheck expectedResult="0">select count(*) from oldtable

</sqlCheck></preConditions>

Page 39: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

DEMO

Page 40: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

let us try it

DDLv1

Page 41: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

DDL

v2

v1

Init data, new column(source)

let us try it

Page 42: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

how it works

DDL

v2

v3

v1

Init data, new column(source)

Move values in separate table

let us try it

Page 43: SE2016 Java Roman Ugolnikov "Migration and source control for your DB"

let us try it

DDL

v2

v3

v1

Init data, new column(source)

Move values in separate table