Database migrator

Database migrations made easy

License

License

Categories

Categories

Data
GroupId

GroupId

nl.myndocs
ArtifactId

ArtifactId

database-migrator
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Database migrator
Database migrations made easy

Download database-migrator

How to add to project

<!-- https://jarcasting.com/artifacts/nl.myndocs/database-migrator/ -->
<dependency>
    <groupId>nl.myndocs</groupId>
    <artifactId>database-migrator</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/nl.myndocs/database-migrator/
implementation 'nl.myndocs:database-migrator:1.1.0'
// https://jarcasting.com/artifacts/nl.myndocs/database-migrator/
implementation ("nl.myndocs:database-migrator:1.1.0")
'nl.myndocs:database-migrator:jar:1.1.0'
<dependency org="nl.myndocs" name="database-migrator" rev="1.1.0">
  <artifact name="database-migrator" type="jar" />
</dependency>
@Grapes(
@Grab(group='nl.myndocs', module='database-migrator', version='1.1.0')
)
libraryDependencies += "nl.myndocs" % "database-migrator" % "1.1.0"
[nl.myndocs/database-migrator "1.1.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.25

test (10)

Group / Artifact Type Version
org.arquillian.cube : arquillian-cube-docker jar
org.jboss.arquillian.junit : arquillian-junit-standalone jar 1.1.13.Final
junit : junit jar 4.12
org.hamcrest : hamcrest-all jar 1.3
org.postgresql : postgresql jar 42.1.4
mysql : mysql-connector-java jar 6.0.6
org.hsqldb : hsqldb jar 2.4.0
com.h2database : h2 jar 1.4.196
org.apache.derby : derby jar 10.13.1.1
org.slf4j : slf4j-simple jar 1.7.25

Project Modules

There are no modules declared in this project.

Database migration

Goal

The goals of this project:

  • Create a database agnostic migration tool
  • Configuration created with java code

This is only meant for simple database structures. It will not support database views, functions or procedures.

Maven

<dependency>
    <groupId>nl.myndocs</groupId>
    <artifactId>database-migrator</artifactId>
    <version>1.1.0</version>
</dependency>

Features

Supported databases

  • Derby
  • H2
  • HyperSQL
  • MySQL
  • PostgreSQL

Supported column types

  • BOOLEAN
  • TEXT
  • VARCHAR
  • CHAR
  • SMALLINT
  • INT
  • BIGINT
  • UUID
  • DATE
  • TIME
  • TIMESTAMP

Supported cascading types

  • RESTRICT
  • SET_NULL
  • SET_DEFAULT
  • NO_ACTION
  • CASCADE

Examples

Migrations

Creating migration script

public class FirstMigrationScript implements MigrationScript {
    @Override
    public String migrationId() {
        // Make sure the id is unique over all the migration scripts
        return "MIGRATION-1";
    }

    @Override
    public void migrate(Migration migration) {
        migration.table("some_table")
            .addColumn("id", Column.TYPE.INTEGER, column -> column.primary(true).autoIncrement(true))
            .addColumn("name", Column.TYPE.VARCHAR)
            .save();
    }
}

Bootstrapping

// Get the JDBC connection
Connection connection = ...;
Database database =  new Selector().loadFromConnection(connection);
Migrator migration = new Migrator(database);
migration.migrate(
        new FirstMigrationScript(),
        new SecondMigrationScript()
)

Migrations will be executed in the same order on how it is passed to .migrate()

Migration capabilities

Create table

migration.table("some_table")
    .addColumn("id", Column.TYPE.INTEGER, column -> column.primary(true).autoIncrement(true))
    .addColumn("name", Column.TYPE.VARCHAR)
    .save();

Create table with foreign keys

migration.table("some_other_table")
    .addColumn("id", Column.TYPE.INTEGER, column -> column.primary(true).autoIncrement(true))
    .addColumn("some_table_id", Column.TYPE.INTEGER)
    .addForeignKey("some_FK", "some_table", "some_table_id", "id", key -> {
        key.cascadeDelete(ForeignKey.CASCADE.RESTRICT);
        key.cascadeUpdate(ForeignKey.CASCADE.RESTRICT);
    })
    .save();

Rename column

migration.table("some_table")
    .changeColumn("name", column -> column.rename("renamed"))
    .save();

Change column type

migration.table("some_table")
    .changeColumn("name", column -> column.type(Column.TYPE.VARCHAR));

Column type size

migration.table("some_table")
    .addColumn("name", Column.TYPE.VARCHAR, column -> column.size(25));

Getting the JDBC connection

// Do not close the connection!
Connection connection = migration.getDatabase().getConnection();

Requirements

Runtime

  • Java 8+

Test

  • Docker

Versions

Version
1.1.0
1.0.0
1.0.0-beta4
1.0.0-beta3
1.0.0-beta2
1.0.0-beta1