jooq and flyway

Download JOOQ and Flyway

Post on 27-Aug-2014




18 download

Embed Size (px)




Getting Started with JOOQ and Flyway Wiring up your Java app with DB JUG.LV 08.05.2014 RIGA DMITRY LEBEDEV & RUSTAM ARSLANOV @ 28STONE About Dmitry - developer Rustam - DevOps guy Customer - finances Project - set of microservices Motivation Use simple tools Try something new Keep codebase maintainable Flyway SQL scripts for migration Java beans for complex logic Simple commands JOOQ Java API corresponds to SQL semantics ActiveRecord & JPA beans generation Strict typing in Java Simple Tool: Flyway flyway:clear flyway:init flyway:migrate Simple Tool: JOOQ List list ="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); Flyway migrate V1_1__create_book_table.sql V1_2__create_author_table.sql V1_3__alter_book_table.sql DB Database DB book author schema_version Database DB schema_version JOOQ generate DB book author SchemaImpl Tables BOOK AUTHOR BookRecord AuthorRecord JOOQ SchemaImpl Tables BOOK AUTHOR domain-objects_.jar BookRecord AuthorRecord component_1 domain-objects-version=1.1 component_2 domain-objects-version=1.1 component_3 domain-objects-version=1.2 Build systems support Flyway: command line (java) java API ant maven 2,3 gradle sbt Jooq: command line (java) java API maven 2,3 gradle (3d party) domain-objects build workflow (gradle) clean resource preparation deployflyway generated compilation jar main compilation resources filtering clean init repair validate info migrate generated compile jooq generateJooq domain-objects deploy workflow (gradle) clean deployflyway get dependencies clean init repair validate info migrate ... ... ... get domain- objects deploy or run application Flyway in CI (jenkins) Flyway versioning One 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 1 001 5.2 5_2 (5.2 at runtime) 205.68 20130115113556 2013. 2013. 1__CreateTable.sql 001__AlterTable.sql 5.2__InsertDate.sql 5_2__DeleteData.sql 205.68__Transform.sql 20130115113556__InsertView.sql 2013. 2013. Lets Do Some Programming! Building queries / SQL generation Fetching results Updating records Not covering: Lazy fetch Transaction management Constraints & Relationships DSLContext - starting point DSLContext create = DSL.using(conn, SQLDialect.MYSQL); Result result =; Building queries"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(); Building queries, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) .from(BOOK) .join(AUTHOR) .on(BOOK.AUTHOR_ID.equal(AUTHOR.ID)) .where(BOOK.PUBLISHED_IN.equal(1948)) .fetch(); Building queries, 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(); Building queries, 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(); SQL Generation String sql =, 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(); Fetching results: Record, 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(); Fetching results: TableRecord create.selectFrom(BOOK) .where(BOOK.PUBLISHED_IN.equal(1948)) .orderBy(BOOK.TITLE.asc()).limit(10).offset(100) .fetch(); List result1 = create.selectFrom(BOOK)... Result result2 = // somewhere in iteration loop bookRecord.getTitle(); record.get(title); Record VS. TableRecord Fetching results: CSV create.selectFrom(BOOK) .where(BOOK.PUBLISHED_IN.equal(1948)) .orderBy(BOOK.TITLE.asc()).limit(10).offset(100) .fetch().formatCSV(,,); Fetching results: JSON create.selectFrom(BOOK) .where(BOOK.PUBLISHED_IN.equal(1948)) .orderBy(BOOK.TITLE.asc()).limit(10).offset(100) .fetch().formatJSON(); Fetching results: XML create.selectFrom(BOOK) .where(BOOK.PUBLISHED_IN.equal(1948)) .orderBy(BOOK.TITLE.asc()).limit(10).offset(100) .fetch().formatXML(); Fetching results: HTML create.selectFrom(BOOK) .where(BOOK.PUBLISHED_IN.equal(1948)) .orderBy(BOOK.TITLE.asc()).limit(10).offset(100) .fetch().formatHTML(); Fetching results: POJOs, 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); Fetching results: Maps, 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(); Records: create // Create a new record BookRecord book1 = create.newRecord(BOOK); // Insert the record: INSERT INTO BOOK (TITLE) VALUES ('1984'); book1.setTitle("1984");; Records: update BookRecord book2 = create.fetchOne(BOOK, BOOK.ID.equal(id)); // Update the record: UPDATE BOOK SET TITLE = 'Animal Farm' WHERE ID = [id] book2.setTitle("Animal Farm");; Records: delete BookRecord book = create.fetchOne(BOOK, BOOK.ID.equal(5)); // Delete the book book.delete(); Records: batch changes // Fetch a bunch of books List 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); Records: update using a query create.update(BOOK) .set(BOOK.IS_BESTSELLER, 1) .where(BOOK.SELLED_COPIES.greater(100000)) .execute(); Q&A! any questions?!

View more