44
Migrationа and source control for your DB

Roman Ugolnikov Migrationа and sourcecontrol for your db

  • Upload
    -

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Roman Ugolnikov Migrationа and sourcecontrol for your db

Migrationа and source control for your DB

Page 2: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol for your db

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

Data base data:

v1.0.0

Page 5: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol for your db

Conclusion

We need version control over our database

migration

Page 11: Roman Ugolnikov Migrationа and sourcecontrol for your db

Tools

Play framework:migration

Rails ActiveRecord migrations

Mogeez for

Page 12: Roman Ugolnikov Migrationа and sourcecontrol for your db

Tools

Play framework:migration

Rails ActiveRecord migrations

Mongeez for

Page 13: Roman Ugolnikov Migrationа and sourcecontrol for your db

Tools

Play framework:migration

Rails ActiveRecord migrations

Mogeez for

Page 14: Roman Ugolnikov Migrationа and sourcecontrol for your db

Tools

Play framework:migration

Rails ActiveRecord migrations

Mogeez for

Page 15: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol for your db

Common flow

Build Test Run

• Java API• Spring Bean

• Plugins• CLI

• Plugins• CLI

Page 19: Roman Ugolnikov Migrationа and sourcecontrol for your db

How it worksDatabaseChangelogLock:

DatabaseChangelog:

Page 20: Roman Ugolnikov Migrationа and sourcecontrol for your db

How it works

Schema_versions:

Page 21: Roman Ugolnikov Migrationа and sourcecontrol for your db

How it works

Sequential change flow1. Init db

2. Add column “type”

3. Add new table

Page 22: Roman Ugolnikov Migrationа and sourcecontrol for your db

How it works

Sequential change flow1. Init db

2. Add column “type”

3. Add new tableX

Page 23: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol for your db

How it works

V1__Initial_Setup.sql

V1_1__First_Changes.sql

V1_2__Added_user_info.sql

V2__ ….

Page 26: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol for your db

Supported formats

DSL for database changes:

• XML• YAML• JSON• sql

Pure SQL

Page 30: Roman Ugolnikov Migrationа and sourcecontrol for your db

Base lineConvention over configuration

Toolbox

Page 31: Roman Ugolnikov Migrationа and sourcecontrol for your db

Toolbox

• SQL output• Rollback• Context• Precondition

Page 32: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol for your db

DEMO

Page 40: Roman Ugolnikov Migrationа and sourcecontrol for your db

let us try it

DDLv1

Page 41: Roman Ugolnikov Migrationа and sourcecontrol for your db

DDL

v2

v1

Init data, new column(source)

let us try it

Page 42: Roman Ugolnikov Migrationа and sourcecontrol 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: Roman Ugolnikov Migrationа and sourcecontrol for your db

let us try it

DDL

v2

v3

v1

Init data, new column(source)

Move values in separate table