Class SqlUpdater<T extends JdbcConfig<?>>
- All Implemented Interfaces:
DataSourceProvider
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classSqlUpdater.SqlUpdaterBuilder<T extends JdbcConfig<?>>Class to build aSqlUpdaterwith a builder pattern -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends JdbcConfig<?>>
SqlUpdater.SqlUpdaterBuilderbuilder(DataSource dataSource, SqlType<T> type) Creates a newSqlUpdater.SqlUpdaterBuilderwith a version set to a string located inresources/database/version.static <T extends JdbcConfig<?>>
SqlUpdater.SqlUpdaterBuilder<T>builder(DataSource dataSource, SqlVersion version, SqlType<T> type) Creates a newSqlUpdater.SqlUpdaterBuilderwith a version set to a string located inresources/database/version.Methods inherited from class de.chojo.sqlutil.base.QueryFactoryHolder
builder, builder, factory, sourceMethods inherited from class de.chojo.sqlutil.base.DataHolder
getConnection, logDbError, logDbError, queryBuilder, setupLogger
-
Method Details
-
builder
public static <T extends JdbcConfig<?>> SqlUpdater.SqlUpdaterBuilder builder(DataSource dataSource, SqlType<T> type) throws IOException Creates a newSqlUpdater.SqlUpdaterBuilderwith a version set to a string located inresources/database/version.This string requires the format
Major.Patch. Patches are not supported.- Type Parameters:
T- type of the database defined by theSqlType- Parameters:
dataSource- the data source to connect to the databasetype- 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 newSqlUpdater.SqlUpdaterBuilderwith a version set to a string located inresources/database/version.- Type Parameters:
T- type of the database defined by theSqlType- Parameters:
dataSource- the data source to connect to the databaseversion- the version withMajor.Patchtype- the sql type of the database- Returns:
- builder instance
-