jooq and flyway

41
Getting Started with JOOQ and Flyway Wiring up your Java app with DB JUG.LV 08.05.2014 RIGA DMITRY LEBEDEV & RUSTAM ARSLANOV @ 28STONE

Upload: dmitry-buzdin

Post on 27-Aug-2014

367 views

Category:

Software


19 download

DESCRIPTION

 

TRANSCRIPT

Page 1: JOOQ and Flyway

Getting Started with JOOQ and Flyway

Wiring up your Java app with DB

JUG.LV 08.05.2014 RIGA

DMITRY LEBEDEV & RUSTAM ARSLANOV @ 28STONE

Page 2: JOOQ and Flyway

About

Dmitry - developerRustam - DevOps guyCustomer - financesProject - set of

microservices

http://www.28stone.com

Page 3: JOOQ and Flyway

Motivation

Use simple toolsTry something new

Keep codebase maintainable

Page 4: JOOQ and Flyway

Flyway

SQL scripts for migrationJava beans for complex logic

Simple commands

Page 5: JOOQ and Flyway

JOOQ

Java API corresponds to SQL semanticsActiveRecord & JPA beans generation

Strict typing in Java

Page 6: JOOQ and Flyway

Simple Tool: Flyway

flyway:clearflyway:init

flyway:migrate

Page 7: JOOQ and Flyway

Simple Tool: JOOQList<BookRecord> list = create.select(field("BOOK.TITLE"), field("AUTHOR.

FIRST_NAME"), field("AUTHOR.LAST_NAME"))

.from(table("BOOK"))

.join(table("AUTHOR"))

.on(field("BOOK.AUTHOR_ID").equal(field("AUTHOR.ID")))

.where(field("BOOK.PUBLISHED_IN").equal(1948))

.fetchInto(BookRecord.class);

Page 8: JOOQ and Flyway

Flyway

migrate

V1_1__create_book_table.sqlV1_2__create_author_table.sqlV1_3__alter_book_table.sql DB

Page 9: JOOQ and Flyway

Database

DB

book

author

schema_version

Page 10: JOOQ and Flyway

Database

DB

schema_version

Page 11: JOOQ and Flyway

JOOQ

generate

DB

bookauthor

SchemaImpl

Tables

BOOK

AUTHOR

BookRecord

AuthorRecord

Page 12: JOOQ and Flyway

JOOQ

SchemaImpl

TablesBOOK

AUTHOR

domain-objects_<version>.jar

BookRecord

AuthorRecord

component_1domain-objects-version=1.1

component_2domain-objects-version=1.1

component_3domain-objects-version=1.2

Page 13: JOOQ and Flyway

Build systems support

Flyway: command line (java)java APIantmaven 2,3gradlesbt

Jooq: command line (java)java APImaven 2,3gradle (3d party)

Page 14: JOOQ and Flyway

domain-objects build workflow (gradle)

clean resource preparation deployflyway generated

compilation jarmain compilation

● resources filtering

clean

init

repair

validate

info

migrate

● generated compile

jooq

generateJooq

Page 15: JOOQ and Flyway

domain-objects deploy workflow (gradle)

clean deployflywayget dependencies

clean

init

repair

validate

info

migrate

... ... ...

● get domain-objects

● deploy or run application

Page 16: JOOQ and Flyway

Flyway in CI (jenkins)

Page 17: JOOQ and Flyway

Flyway versioningOne or more numeric parts

Separated by a dot (.) or an underscore (_)

Underscores are replaced by dots at runtime

Leading zeroes are ignored in each part

10015.25_2 (5.2 at runtime)1.2.3.4.5.6.7.8.9205.68201301151135562013.1.15.11.35.562013.01.15.11.35.56

1__CreateTable.sql001__AlterTable.sql5.2__InsertDate.sql5_2__DeleteData.sql1.2.3.4.5.6.7.8.9__AnotherScript.sql205.68__Transform.sql20130115113556__InsertView.sql2013.1.15.11.35.56__AddService.sql2013.01.15.11.35.56__Delete.sql

Page 18: JOOQ and Flyway

Let’s Do Some Programming!

Building queries / SQL generationFetching resultsUpdating records

Page 19: JOOQ and Flyway

Not covering:

● Lazy fetch● Transaction management● Constraints & Relationships

Page 20: JOOQ and Flyway

DSLContext - starting pointDSLContext create = DSL.using(conn, SQLDialect.MYSQL);

Result<Record> result = create.select().from(AUTHOR).fetch();

Page 21: JOOQ and Flyway

Building queriescreate.select(field("BOOK.TITLE"), field("AUTHOR.FIRST_NAME"), field

("AUTHOR.LAST_NAME"))

.from(table("BOOK"))

.join(table("AUTHOR"))

.on(field("BOOK.AUTHOR_ID").equal(field("AUTHOR.ID")))

.where(field("BOOK.PUBLISHED_IN").equal(1948))

.fetch();

Page 22: JOOQ and Flyway

Building queriescreate.select(BOOK.TITLE, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)

.from(BOOK)

.join(AUTHOR)

.on(BOOK.AUTHOR_ID.equal(AUTHOR.ID))

.where(BOOK.PUBLISHED_IN.equal(1948))

.fetch();

Page 23: JOOQ and Flyway

Building queriescreate.select(BOOK.TITLE, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)

.from(BOOK)

.join(AUTHOR)

.on(BOOK.AUTHOR_ID.equal(AUTHOR.ID))

.where(BOOK.PUBLISHED_IN.equal(1948))

.orderBy(BOOK.TITLE.asc())

.fetch();

Page 24: JOOQ and Flyway

Building queriescreate.select(BOOK.TITLE, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)

.from(BOOK)

.join(AUTHOR)

.on(BOOK.AUTHOR_ID.equal(AUTHOR.ID))

.where(BOOK.PUBLISHED_IN.equal(1948))

.orderBy(BOOK.TITLE.asc()).limit(10).offset(100)

.fetch();

Page 25: JOOQ and Flyway

SQL GenerationString sql = create.select(BOOK.TITLE, AUTHOR.FIRST_NAME, AUTHOR.

LAST_NAME)

.from(BOOK)

.join(AUTHOR)

.on(BOOK.AUTHOR_ID.equal(AUTHOR.ID))

.where(BOOK.PUBLISHED_IN.equal(1948))

.orderBy(BOOK.TITLE.asc()).limit(10).offset(100)

.getSQL();

Page 26: JOOQ and Flyway

Fetching results: Recordcreate.select(BOOK.TITLE, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)

.from(BOOK)

.join(AUTHOR)

.on(BOOK.AUTHOR_ID.equal(AUTHOR.ID))

.where(BOOK.PUBLISHED_IN.equal(1948))

.orderBy(BOOK.TITLE.asc()).limit(10).offset(100)

.fetch();

Page 27: JOOQ and Flyway

Fetching results: TableRecordcreate.selectFrom(BOOK)

.where(BOOK.PUBLISHED_IN.equal(1948))

.orderBy(BOOK.TITLE.asc()).limit(10).offset(100)

.fetch();

Page 28: JOOQ and Flyway

List<BookRecord> result1 = create.selectFrom(BOOK)...

Result<Record> result2 = create.select().from(BOOK)...

// somewhere in iteration loop

bookRecord.getTitle();

record.get(“title”);

Record VS. TableRecord

Page 29: JOOQ and Flyway

Fetching results: CSVcreate.selectFrom(BOOK)

.where(BOOK.PUBLISHED_IN.equal(1948))

.orderBy(BOOK.TITLE.asc()).limit(10).offset(100)

.fetch().formatCSV(‘,’,””);

Page 30: JOOQ and Flyway

Fetching results: JSONcreate.selectFrom(BOOK)

.where(BOOK.PUBLISHED_IN.equal(1948))

.orderBy(BOOK.TITLE.asc()).limit(10).offset(100)

.fetch().formatJSON();

Page 31: JOOQ and Flyway

Fetching results: XMLcreate.selectFrom(BOOK)

.where(BOOK.PUBLISHED_IN.equal(1948))

.orderBy(BOOK.TITLE.asc()).limit(10).offset(100)

.fetch().formatXML();

Page 32: JOOQ and Flyway

Fetching results: HTMLcreate.selectFrom(BOOK)

.where(BOOK.PUBLISHED_IN.equal(1948))

.orderBy(BOOK.TITLE.asc()).limit(10).offset(100)

.fetch().formatHTML();

Page 33: JOOQ and Flyway

Fetching results: POJOscreate.select(BOOK.TITLE, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)

.from(BOOK)

.join(AUTHOR)

.on(BOOK.AUTHOR_ID.equal(AUTHOR.ID))

.where(BOOK.PUBLISHED_IN.equal(1948))

.orderBy(BOOK.TITLE.asc()).limit(10).offset(100)

.fetchInto(BookDTO.class);

Page 34: JOOQ and Flyway

Fetching results: Mapscreate.select(BOOK.TITLE, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)

.from(BOOK)

.join(AUTHOR)

.on(BOOK.AUTHOR_ID.equal(AUTHOR.ID))

.where(BOOK.PUBLISHED_IN.equal(1948))

.orderBy(BOOK.TITLE.asc()).limit(10).offset(100)

.fetchMaps();

Page 35: JOOQ and Flyway

Records: create// Create a new record

BookRecord book1 = create.newRecord(BOOK);

// Insert the record: INSERT INTO BOOK (TITLE) VALUES ('1984');

book1.setTitle("1984");

book1.store();

Page 36: JOOQ and Flyway

Records: updateBookRecord book2 = create.fetchOne(BOOK, BOOK.ID.equal(id));

// Update the record: UPDATE BOOK SET TITLE = 'Animal Farm' WHERE ID

= [id]

book2.setTitle("Animal Farm");

book2.store();

Page 37: JOOQ and Flyway

Records: deleteBookRecord book = create.fetchOne(BOOK, BOOK.ID.equal(5));

// Delete the book

book.delete();

Page 38: JOOQ and Flyway

Records: batch changes// Fetch a bunch of books

List<BookRecord> books = create.fetch(BOOK);

// Modify the above books, and add some new ones:

modify(books);

addMore(books);

// Batch-update and/or insert all of the above books

create.batchStore(books);

Page 39: JOOQ and Flyway

Records: update using a query

create.update(BOOK) .set(BOOK.IS_BESTSELLER, 1) .where(BOOK.SELLED_COPIES.greater(100000)) .execute();

Page 40: JOOQ and Flyway

Q&A!

any questions?!

Page 41: JOOQ and Flyway