Top Banner
Database migrations - the missing piece in Java EE Rikard Thulin
126

javazone 2014

Apr 12, 2017

Download

Documents

Rikard Thulin
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: javazone 2014

Database migrations - the missing piece in Java EE

Rikard Thulin

Page 2: javazone 2014

3 billion Java developers lives was ruined on

August 29, 1997

The survivors of the deployment called the war

Judgment Day.

Page 3: javazone 2014

They lived only to face a new nightmare, the war against the

DBA.

Skynet, the computer which controlled releases, sent

two engineers back through time

Page 4: javazone 2014

Their mission: to solve the problems of

schema migrations…

Opening “quote” from “Terminator 2: Judgment Day”

Page 5: javazone 2014

You will learn how to

be happier as you can spend

more time writing code!

Page 6: javazone 2014

And we will have a look into

the future and the

possibilities at hand

Page 7: javazone 2014

Roadmap

• Introduction to the topic and why it is so important

• The current situation, the tools of the trade

• Future possibilities at hand to solve this problem

Page 8: javazone 2014

Part 1

Introduction to the topic and why it is so important

Page 9: javazone 2014

Finally, we where ready to go

IT WAS RELEASE DAY

Page 10: javazone 2014

Just get the luggage

Page 11: javazone 2014

There might be a small burden

Page 12: javazone 2014

Let the team lead or the smart guy do it!Or if you are a manager, it translates to the expensive guy

Page 13: javazone 2014

The team goes…

Page 14: javazone 2014

This is your complete team

Page 15: javazone 2014

change recall refactoring applied some marked specified better run units the tagged along more SQL precondition pain state both statements contains production Sets applying database nested things executes identified context leave test one management Generally apply HOPE change stable individual ran database just IT generate fails different changes uniquely per machines change written productivity log attribute Set change sets development author files WORKS Contexts describes by descript ive someone regardless Major else environments column queries local Each yet either WORK change ANYMORE set Preconditions DATABASECHANGELOG unexpected control file !? Concepts stop store execution arbitrarily location auto commit executed Developers

Page 16: javazone 2014

change recall refactoring applied some marked specified better run units the tagged along more SQL precondition pain state both statements contains production Sets applying database nested things executes identified context leave test one management Generally apply HOPE change stable individual ran database just IT generate fails different changes uniquely per machines change written productivity log attribute Set change sets development author files WORKS Contexts describes by descript ive someone regardless Major else environments column queries local Each yet either WORK change ANYMORE set Preconditions DATABASECHANGELOG unexpected control file !? Concepts stop store execution arbitrarily location auto commit executed Developers

Page 17: javazone 2014

change recall refactoring applied some marked specified better run units the tagged along more SQL precondition pain state both statements contains production Sets applying database nested things executes identified context leave test one management Generally apply HOPE change stable individual ran database just IT generate fails different changes uniquely per machines change written productivity log attribute Set change sets development author files WORKS Contexts describes by descript ive someone regardless Major else environments column queries local Each yet either WORK change ANYMORE set Preconditions DATABASECHANGELOG unexpected control file !? Concepts stop store execution arbitrarily location auto commit executed Developers

Page 18: javazone 2014

Finally we are set to go…

…but…

Page 19: javazone 2014

DBA

Page 20: javazone 2014

Fill in the forms…

Page 21: javazone 2014

You are trapped in the Jar(aka jar-hell)

Page 22: javazone 2014
Page 23: javazone 2014

It breaks andYou look like this

Page 24: javazone 2014

You gently communicate with your fellow co-worker

Page 25: javazone 2014

This was embarrassing and unprofessional

Page 26: javazone 2014
Page 27: javazone 2014

Time to be a hero!

Page 28: javazone 2014

Engineers likes to be heroes

This has to be a good thing, right?

Page 29: javazone 2014

You (the smart guy) spend the weekend

Page 30: javazone 2014
Page 31: javazone 2014

Your boss turns to thisAre you nuts? OVERTIME?!

Page 32: javazone 2014

The obvious solution to professionally solve the problem

Page 33: javazone 2014

We add pre-release meeting!

Page 34: javazone 2014

change remember refactoring applied some marked specified better run units the tagged along more SQL precondition and state both statements contains production Sets last database nested things executes identified context leave test one management Generally apply suddenly change stable individual ran time just a generate fails different STUFF uniquely per machines change co-worker productivity log attribute Set change sets development author files DOES Contexts describes added descriptive NOT regardless Major a environments column queries local Each yet either WORK change ANYMORE set Preconditions DATABASECHANGELOG unexpected control file !? Concepts stop store execution arbitrarily location auto commit executed Developers

Page 35: javazone 2014

change remember refactoring applied some marked specified better run units the tagged along more SQL precondition and state both statements contains production Sets last database nested things executes identified context leave test one management Generally apply suddenly change stable individual ran time just a generate fails different STUFF uniquely per machines change co-worker productivity log attribute Set change sets development author files DOES Contexts describes added descriptive NOT regardless Major a environments column queries local Each yet either WORK change ANYMORE set Preconditions DATABASECHANGELOG unexpected control file !? Concepts stop store execution arbitrarily location auto commit executed Developers

Page 36: javazone 2014

change remember refactoring applied some marked specified better run units the tagged along more SQL precondition and state both statements contains production Sets last database nested things executes identified context leave test one management Generally apply suddenly change stable individual ran time just a generate fails different STUFF uniquely per machines change co-worker productivity log attribute Set change sets development author files DOES Contexts describes added descriptive NOT regardless Major a environments column queries local Each yet either WORK change ANYMORE set Preconditions DATABASECHANGELOG unexpected control file !? Concepts stop store execution arbitrarily location auto commit executed Developers

$ git pull

Page 37: javazone 2014

change remember refactoring applied some marked specified better run units the tagged along more SQL precondition and state both statements contains production Sets last database nested things executes identified context leave test one management Generally apply suddenly change stable individual ran time just a generate fails different STUFF uniquely per machines change co-worker productivity log attribute Set change sets development author files DOES Contexts describes added descriptive NOT regardless Major a environments column queries local Each yet either WORK change ANYMORE set Preconditions DATABASECHANGELOG unexpected control file !? Concepts stop store execution arbitrarily location auto commit executed Developers

Page 38: javazone 2014

change remember refactoring applied some marked specified better run units the tagged along more SQL precondition and state both statements contains production Sets last database nested things executes identified context leave test one management Generally apply suddenly change stable individual ran time just a generate fails different STUFF uniquely per machines change co-worker productivity log attribute Set change sets development author files DOES Contexts describes added descriptive NOT regardless Major a environments column queries local Each yet either WORK change ANYMORE set Preconditions DATABASECHANGELOG unexpected control file !? Concepts stop store execution arbitrarily location auto commit executed Developers

Page 39: javazone 2014

Instead

Page 40: javazone 2014
Page 41: javazone 2014

The aha moment

Page 42: javazone 2014

From

Page 43: javazone 2014

To

Page 44: javazone 2014

The smiling team

Page 45: javazone 2014

Productivity increased

Page 46: javazone 2014

You will learn how tobe happier as you can spend

more time writing code!

Page 47: javazone 2014

Sweden

Page 48: javazone 2014

Friendly

Page 49: javazone 2014

neighbor

Page 50: javazone 2014

to

Page 51: javazone 2014

Norway

Page 52: javazone 2014

unlike

Page 53: javazone 2014

Norway

Page 54: javazone 2014

we

Page 55: javazone 2014

do

Page 56: javazone 2014

not

Page 57: javazone 2014

have

Page 58: javazone 2014

a

Page 59: javazone 2014

storage

Page 60: javazone 2014

problem

Page 61: javazone 2014

Proven national gas reserves (cubic meters)

0 2,007,000,000,000

Proven national oil reserves (barrels) 0 5,320,000,000

NO STORAGE PROBLEM

Page 62: javazone 2014
Page 63: javazone 2014

Development

One developer, life is easy!

Page 64: javazone 2014

Development

Obvious we need production

Production

Page 65: javazone 2014

Anotherjoins

Page 66: javazone 2014

Now each developer can work

Development Production

Page 67: javazone 2014

Time to bring out the engineer

from the closet

Page 68: javazone 2014

Development ProdCI

for the engineers to cooperate

we need Continuos Integration

Page 69: javazone 2014

Time to really

bring out the engineerfrom the closet

Page 70: javazone 2014

ProdDev CI

someone told the boss that things should not only be tested

on end users

QA

Page 71: javazone 2014

Time to really, really

bring out the engineerfrom the closet

Page 72: javazone 2014

StagingDev CI

Now when we know about quality assurance we should stage

our production environment

QA

Prod

Page 73: javazone 2014

Morejoins

As we do everything by the book

things ($) goes well

Page 74: javazone 2014

Staging clusterCI

How do we keep everything CONSISTENT?

QA

Staging cluster

Prod clusterProd cluster

Dev

Page 75: javazone 2014

{code}VCS Build server

How do we keep { code }

CONSISTENT?

artifactsRepository

push pull

publish

release

Dev

QA

promote

DevStagingProd

Page 76: javazone 2014

How do we keep { code + database }

CONSISTENT?

Page 77: javazone 2014

Two entities

Page 78: javazone 2014
Page 79: javazone 2014
Page 80: javazone 2014

Really?Is the database EVIL?

Page 81: javazone 2014

artifact manual, script, …

Source Database

Page 82: javazone 2014

the problem is to keep

them consistent

Page 83: javazone 2014

HOW

Page 84: javazone 2014

artifact manual, script, …

Source Database

db deltas, automatic

Page 85: javazone 2014

artifact

Source

Page 86: javazone 2014

{code +

migrations}

VCS Build server

How do we keep { code + database }

CONSISTENT?

artifacts

{code + migrations}

Repository

push pull

publish

release

Dev

QA

promote

DevStagingProd

Page 87: javazone 2014

Shiny Happy PeopleYou will get

Page 88: javazone 2014

Schema MigrationDEMO DEMO DEMO DEMO

Page 89: javazone 2014

Schema MigrationDEMO DEMO DEMO DEMO

Migrations are applied automatically during deployment

Page 90: javazone 2014

Schema MigrationDEMO DEMO DEMO DEMO

Spring based application

Page 91: javazone 2014

Schema MigrationDEMO DEMO DEMO DEMO

What you will see

Page 92: javazone 2014

Shiny Happy PeopleDEMONSTRATION OF

Page 93: javazone 2014

LOCAL DEV PROD

Version 0 Version 0 Version 0

Page 94: javazone 2014

LOCAL DEV PROD

Version 1 Version 0 Version 0

Page 95: javazone 2014

LOCAL DEV PROD

Version 1 Version 1 Version 0

Page 96: javazone 2014

LOCAL DEV PROD

Version 2 Version 1 Version 0

Page 97: javazone 2014

LOCAL DEV PROD

Version 2 Version 3 Version 0

Page 98: javazone 2014

LOCAL DEV PROD

Version 2 Version 3 Version 3

Page 99: javazone 2014

What can we do?create/drop TABLEcreate/drop INDEX

add/remove CONSTRAINT alter SQUENCE

STOREDPROCEDUREScreate/drop VIEWS

insert DATA

and more!

Page 100: javazone 2014

What

is the trick?

Page 101: javazone 2014

When

is the trick?

Page 102: javazone 2014

Database Change ManagementReal Life Tricks

• Possible to use on an existing database • Quick and simple to implement • Database copies mix and match • Wash copies of production database • DBA approval / review process • One click release possible

Page 103: javazone 2014

Part 2

The current situation tools of the trade

Page 104: javazone 2014
Page 105: javazone 2014

Tools of the Trade

ActiveRecord Ruby (Ruby)

Entity Framework Migrations (.net)

COMPETITION HAS IT

Page 106: javazone 2014

Tools of the Trade

Hibernate & JPA Schema Generation

Page 107: javazone 2014

Tools of the Trade

FlyWay & LiquibaseStronger execution order Supports different DB / environment Stereotype of being “heavyweight" Automatic rollback

Convention over configuration Gaining popularity

Page 108: javazone 2014

Tools of the TradeFlyWay

Convention over configuration Get up and running in 5 minutes Gaining popularity

Page 109: javazone 2014

Tools of the TradeLiquibase

Stronger execution order Supports different DB for different environment Stereotype of being “heavyweight" Automatic rollback

Page 110: javazone 2014

Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one

of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the

FlyWay & Liquibase Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one

of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one of the tools Use one

Page 111: javazone 2014

Tools of the Trade summary

Schema migration is •Not solved by Java EE today •Solved by 3PP •Already in competitors standards

Page 112: javazone 2014

Part 3

Future possibilities to solve this problem

Page 113: javazone 2014

Reasons to add Migrations to Java EE

We are behind the competition Schema migrations is not well know to the broad developer community Increased Quality and Productivity

Adding complexity to the platform

Page 114: javazone 2014

Advanced tooling

Page 115: javazone 2014

the future?

class User { }

Page 116: javazone 2014

@Migration(id="1", author="duke") class CreateUserTable {

@CreateTable(User.class) @AddColumn(User.class) long id;

@AddColumn(entity = User.class, size = "255") String name; }

the future?

Page 117: javazone 2014

the future?

@Migration(id="1", author="duke") class CreateUserTable { @AddColumn(User.class) long id; … }

@Entity class User { @Id long id; … }

Migration2Code Generator

Page 118: javazone 2014

@Entity class User {

@Id long id;

@Column(size="255") String name; }

the future?

Page 119: javazone 2014

@Migration(id="2", author="duke") class AddColumnsToUser {

@DropColumn(User.class) String name;

@AddColumn(entity = User.class, size="80") String firstName;

@AddColumn(entity = User.class, size="80") String lastName;

@AddColumn(entity = User.class, size="255") String email; }

the future?

Page 120: javazone 2014

@Entity class User {

@Id long id;

@Column(size="80") String firstName;

@Column(size="80") String lastName;

@Column(size="255") String email; }

the future?

Page 121: javazone 2014

The future my proposal

Add schema migration to

Java EE

Page 122: javazone 2014

The future my proposal

Who? WE JCP JSR! What? Lets figure it out!When? Now. Lets start!

Page 123: javazone 2014

Wrap up

Page 124: javazone 2014

Wrap up

1. Start using one of the tools! 2. We need to figure out if

database migrations is a candidate to be included in Java EE

3. Now it the time to take action!

Page 125: javazone 2014

You can be the Hero

Hero wrap up

Page 126: javazone 2014

Q & A

[email protected]