Class SqlUpdater<T extends JdbcConfig<?>>

All Implemented Interfaces:
DataSourceProvider

public class SqlUpdater<T extends JdbcConfig<?>> extends QueryFactoryHolder
An SQL updater which performs database updates via upgrade scripts.

A version is defined via version identifier Major.Patch.

There are three types of scripts:

Setup

Every Major version directory has to contain a setup.sql file. This file represents the inital state of the database version.

During the initial setup the setup.sql script of the highest major version is executed.

Patch

Every patch version requires a patch_x.sql file where x is the number of the patch version.

These files get applied one after another until the current version is reached.

Migrate

Every Major version which has a following major version requires a migrate.sql script.

This script should update the database to the same state as a clean installation via the new setup.sql would do.


 database
 ├── postgres
 │   ├── 1                # 1.x
 │   │   ├── setup.sql    # 1.0
 │   │   ├── patch_1.sql  # 1.1
 │   │   ├── patch_2.sql  # 1.2
 │   │   ├── patch_3.sql  # 1.3
 │   │   └── migrate.sql  # 1.3 -> 2.0
 │   └── 2
 │       ├── setup.sql    # 2.0
 │       └── patch_1.sql  # 2.1
 └── mysql
     ├── 1
     │   ├── setup.sql
     │   ├── patch_1.sql
     │   ├── patch_2.sql
     │   ├── patch_3.sql
     │   └── migrate.sql
     └── 2
         ├── setup.sql
         └── patch_1.sql
       
Setup Process

During the update process the updater will look for a version table defined via SqlUpdater.SqlUpdaterBuilder.setVersionTable(String).

If this table is not present it will be created and the highest setup script will be executed and all existing patches.

Update Process

If the version table is present the updater will read the version and execute all following patches including the migration scripts.

If the current version would be 1.2, but the target version would be 2.1 the following script would be executed:

      - 1/patch_3.sql
      - 1/migrate.sql
      - 2/patch_1.sql
  
  • Method Details

    • builder

      public static <T extends JdbcConfig<?>> SqlUpdater.SqlUpdaterBuilder builder(DataSource dataSource, SqlType<T> type) throws IOException
      Creates a new SqlUpdater.SqlUpdaterBuilder with a version set to a string located in resources/database/version.

      This string requires the format Major.Patch. Patches are not supported.

      Type Parameters:
      T - type of the database defined by the SqlType
      Parameters:
      dataSource - the data source to connect to the database
      type - the sql type of the database
      Returns:
      new builder instance
      Throws:
      IOException - if the version file does not exist.
    • builder

      public static <T extends JdbcConfig<?>> SqlUpdater.SqlUpdaterBuilder<T> builder(DataSource dataSource, SqlVersion version, SqlType<T> type)
      Creates a new SqlUpdater.SqlUpdaterBuilder with a version set to a string located in resources/database/version.
      Type Parameters:
      T - type of the database defined by the SqlType
      Parameters:
      dataSource - the data source to connect to the database
      version - the version with Major.Patch
      type - the sql type of the database
      Returns:
      builder instance