Code Monkey home page Code Monkey logo

rules_jooq_flyway_codegen's Introduction

Bazel rules for jOOQ codegen from Flyway migrations

This Bazel rule will apply Flyway migrations to a database launched in a Testcontainer, use those to run jOOQ's code generator, and produce a source JAR containing your generated classes.

Please note that this rule is still in an alpha-quality state, and the steps taken to import it may change in the future.

To import the rules:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "rules_jooq_flyway_codegen",
    urls = ["https://github.com/richardstephens/rules_jooq_flyway_codegen/releases/download/v0.4/rules_jooq_flyway_codegen-v0.4.tgz"],
    sha256 = "658ded9ac534d81833984576ec21b93dcf608fc89e72ee4734625f90e20be217",
)

You must also define a maven install for the codegen's dependencies: (Note that these dependencies are only for the code generator, they are not propagated to anything that imports the generated classes)

load("@rules_jvm_external//:defs.bzl", rules_jooq_flyway_codegen_maven_install = "maven_install")
rules_jooq_flyway_codegen_maven_install(
    name = "rules_jooq_flyway_codegen_maven",
    artifacts = [
        "org.flywaydb:flyway-core:6.4.4",
        "org.jooq:jooq:3.13.2",
        "org.jooq:jooq-meta:3.13.2",
        "org.jooq:jooq-codegen:3.13.2",
        "org.testcontainers:testcontainers:1.15.1",
        "org.testcontainers:postgresql:1.15.1",
        "org.testcontainers:mariadb:1.15.1",
        "org.testcontainers:mysql:1.15.1",
        "org.postgresql:postgresql:42.2.14",
        "org.mariadb.jdbc:mariadb-java-client:2.6.2",
        "mysql:mysql-connector-java:8.0.21",
        "org.xerial:sqlite-jdbc:3.32.3.2",
    ],
    fetch_sources = True,
    repositories = [
        "https://repo1.maven.org/maven2",
    ],
)

The codegen rule takes as a parameter a resource jar containing your application's flyway migrations. Your directory structure should look something like this:

src/
  myservice/
    db/
      db/
        migration/
          V01_00_00__first_migration.sql
      codegen.xml
      BUILD

Note the double-nested db folders. This is needed because Flyway expects to find its migrations at a path in the form of db/migration by default. We might try to change how this works or make it customisable in the future.

In the BUILD file, you can build a resource jar with your migrations as follows:

java_library(
    name = "migration-jar",
    resource_strip_prefix = "src/myservice/db",
    resources = glob(["db/migration/*.sql"]),
    visibility = ["//src/myservice:__subpackages__"],
)

Note the resource_strip_prefix. That should be the path to this BUILD file

Now you need to set up your codegen.xml file. This will be passed to jOOQ's code generator as is, with the exception of overriding the output directory you specify to a temporary directory for the generated classes. An example codgen.xml file is here, and the jOOQ documentation for the codegen.xml file is here.

Now that you have a resource jar containing your migrations, you can call the code generator like so:

load("@rules_jooq_flyway_codegen//rules_jooq_flyway_codegen:jooqflyway.bzl", "jooqflyway")
jooqflyway(
    name = "myservice-db-classes",
    codegen_xml = "codegen.xml",
    db_type = "mysql",
    jooq_dep = "@maven//:org_jooq_jooq",
    jooq_meta_dep = "@maven//:org_jooq_jooq_meta",
    migration_jar = ":migration-jar",
    visibility = ["//src/myservice:__subpackages__"],
)

Valid options for db type are postgres, mariadb, or mysql at present, but more will be supported in the future.

And that's it! You can now depend on //src/myservice/db:myservice-db-classes

rules_jooq_flyway_codegen's People

Contributors

richardstephens avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.