[jOOQ/jOOQ#9506] Support tags in file based migration

This commit is contained in:
Lukas Eder 2023-06-16 13:31:51 +02:00
parent 2868ea0968
commit 694c266de9
5 changed files with 113 additions and 69 deletions

View File

@ -168,12 +168,13 @@ final class CommitsImpl implements Commits {
// [#9506] TODO: Formalise this decoding, and make it part of the public API
static final class FileData {
final java.io.File file;
final String basename;
final String version;
final String message;
final String id;
final List<String> parentIds;
final java.io.File file;
final String basename;
final String version;
final String message;
final List<TagType> tags;
final String id;
final List<String> parentIds;
FileData(java.io.File file) {
this.file = file;
@ -184,19 +185,27 @@ final class CommitsImpl implements Commits {
/*
* An example:
* -----------
* v1-a
* v2-ab, v2-ac
* v1-a,tag1,tag2
* v2-ab
* v2-ac
* v3-acd
* v3-abc.v2-ab,v2-ac
* v4-abcd.v3-abc,v3-acd
* v4-abcd,tag4.v3-abc,v3-acd
*/
String[] idAndParentsArray = basename.split("\\.");
this.id = idAndParentsArray[0];
String[] idAndTagsArray = idAndParentsArray[0].split(",");
this.id = idAndTagsArray[0];
this.parentIds = idAndParentsArray.length > 1 ? asList(idAndParentsArray[1].split(",")) : asList();
String[] idArray = this.id.split("-");
this.version = idArray[0];
this.message = idArray.length > 1 ? idArray[1] : null;
this.tags = new ArrayList<>();
for (int i = 1; i < idAndTagsArray.length; i++) {
String[] tagArray = idAndTagsArray[i].split("-");
this.tags.add(new TagType().withId(tagArray[0]).withMessage(tagArray.length > 1 ? tagArray[1] : null));
}
}
}
@ -263,6 +272,7 @@ final class CommitsImpl implements Commits {
commit
.withMessage(f.message)
.withTags(f.tags)
// [#9506] TODO: Better define paths, relative paths, etc.
// [#9506] TOOD: Support other ContentType values than INCREMENT

View File

@ -48,6 +48,12 @@ class History extends TableImpl<HistoryRecord> {
*/
final TableField<HistoryRecord, Integer> ID = createField(DSL.name("ID"), SQLDataType.INTEGER.nullable(false).identity(true), this, "The database version ID.");
/**
* The column <code>JOOQ_MIGRATION_HISTORY.MIGRATED_AT</code>. The date/time
* when the database version was migrated to.
*/
final TableField<HistoryRecord, Timestamp> MIGRATED_AT = createField(DSL.name("MIGRATED_AT"), SQLDataType.TIMESTAMP(6).nullable(false), this, "The date/time when the database version was migrated to.");
/**
* The column <code>JOOQ_MIGRATION_HISTORY.MIGRATED_FROM</code>. The
* previous database version ID.
@ -55,15 +61,16 @@ class History extends TableImpl<HistoryRecord> {
final TableField<HistoryRecord, String> MIGRATED_FROM = createField(DSL.name("MIGRATED_FROM"), SQLDataType.VARCHAR(255).nullable(false), this, "The previous database version ID.");
/**
* The column <code>JOOQ_MIGRATION_HISTORY.MIGRATED_TO</code>.
* The column <code>JOOQ_MIGRATION_HISTORY.MIGRATED_TO</code>. The current
* database version ID.
*/
final TableField<HistoryRecord, String> MIGRATED_TO = createField(DSL.name("MIGRATED_TO"), SQLDataType.VARCHAR(255).nullable(false), this, "");
final TableField<HistoryRecord, String> MIGRATED_TO = createField(DSL.name("MIGRATED_TO"), SQLDataType.VARCHAR(255).nullable(false), this, "The current database version ID.");
/**
* The column <code>JOOQ_MIGRATION_HISTORY.MIGRATED_AT</code>. The date/time
* when the database version was migrated to.
* The column <code>JOOQ_MIGRATION_HISTORY.MIGRATED_TO_TAGS</code>. The
* current database version tags, if any, in JSON array format.
*/
final TableField<HistoryRecord, Timestamp> MIGRATED_AT = createField(DSL.name("MIGRATED_AT"), SQLDataType.TIMESTAMP(6).nullable(false), this, "The date/time when the database version was migrated to.");
final TableField<HistoryRecord, String> MIGRATED_TO_TAGS = createField(DSL.name("MIGRATED_TO_TAGS"), SQLDataType.CLOB.nullable(false), this, "The current database version tags, if any, in JSON array format.");
/**
* The column <code>JOOQ_MIGRATION_HISTORY.MIGRATION_TIME</code>. The time

View File

@ -36,44 +36,12 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
return (Integer) get(0);
}
/**
* Setter for <code>JOOQ_MIGRATION_HISTORY.MIGRATED_FROM</code>. The
* previous database version ID.
*/
HistoryRecord setMigratedFrom(String value) {
set(1, value);
return this;
}
/**
* Getter for <code>JOOQ_MIGRATION_HISTORY.MIGRATED_FROM</code>. The
* previous database version ID.
*/
String getMigratedFrom() {
return (String) get(1);
}
/**
* Setter for <code>JOOQ_MIGRATION_HISTORY.MIGRATED_TO</code>.
*/
HistoryRecord setMigratedTo(String value) {
set(2, value);
return this;
}
/**
* Getter for <code>JOOQ_MIGRATION_HISTORY.MIGRATED_TO</code>.
*/
String getMigratedTo() {
return (String) get(2);
}
/**
* Setter for <code>JOOQ_MIGRATION_HISTORY.MIGRATED_AT</code>. The date/time
* when the database version was migrated to.
*/
HistoryRecord setMigratedAt(Timestamp value) {
set(3, value);
set(1, value);
return this;
}
@ -82,7 +50,58 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* when the database version was migrated to.
*/
Timestamp getMigratedAt() {
return (Timestamp) get(3);
return (Timestamp) get(1);
}
/**
* Setter for <code>JOOQ_MIGRATION_HISTORY.MIGRATED_FROM</code>. The
* previous database version ID.
*/
HistoryRecord setMigratedFrom(String value) {
set(2, value);
return this;
}
/**
* Getter for <code>JOOQ_MIGRATION_HISTORY.MIGRATED_FROM</code>. The
* previous database version ID.
*/
String getMigratedFrom() {
return (String) get(2);
}
/**
* Setter for <code>JOOQ_MIGRATION_HISTORY.MIGRATED_TO</code>. The current
* database version ID.
*/
HistoryRecord setMigratedTo(String value) {
set(3, value);
return this;
}
/**
* Getter for <code>JOOQ_MIGRATION_HISTORY.MIGRATED_TO</code>. The current
* database version ID.
*/
String getMigratedTo() {
return (String) get(3);
}
/**
* Setter for <code>JOOQ_MIGRATION_HISTORY.MIGRATED_TO_TAGS</code>. The
* current database version tags, if any, in JSON array format.
*/
HistoryRecord setMigratedToTags(String value) {
set(4, value);
return this;
}
/**
* Getter for <code>JOOQ_MIGRATION_HISTORY.MIGRATED_TO_TAGS</code>. The
* current database version tags, if any, in JSON array format.
*/
String getMigratedToTags() {
return (String) get(4);
}
/**
@ -90,7 +109,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* in milliseconds it took to migrate to this database version.
*/
HistoryRecord setMigrationTime(Long value) {
set(4, value);
set(5, value);
return this;
}
@ -99,7 +118,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* in milliseconds it took to migrate to this database version.
*/
Long getMigrationTime() {
return (Long) get(4);
return (Long) get(5);
}
/**
@ -107,7 +126,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* version used to migrate to this database version.
*/
HistoryRecord setJooqVersion(String value) {
set(5, value);
set(6, value);
return this;
}
@ -116,7 +135,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* version used to migrate to this database version.
*/
String getJooqVersion() {
return (String) get(5);
return (String) get(6);
}
/**
@ -124,7 +143,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* that were run to install this database version.
*/
HistoryRecord setSql(String value) {
set(6, value);
set(7, value);
return this;
}
@ -133,7 +152,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* that were run to install this database version.
*/
String getSql() {
return (String) get(6);
return (String) get(7);
}
/**
@ -141,7 +160,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* SQL statements that were run to install this database version.
*/
HistoryRecord setSqlCount(Integer value) {
set(7, value);
set(8, value);
return this;
}
@ -150,7 +169,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* SQL statements that were run to install this database version.
*/
Integer getSqlCount() {
return (Integer) get(7);
return (Integer) get(8);
}
/**
@ -158,7 +177,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* version installation status.
*/
HistoryRecord setStatus(Status value) {
set(8, value);
set(9, value);
return this;
}
@ -167,7 +186,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* version installation status.
*/
Status getStatus() {
return (Status) get(8);
return (Status) get(9);
}
/**
@ -175,7 +194,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* or error message explaining the status.
*/
HistoryRecord setStatusMessage(String value) {
set(9, value);
set(10, value);
return this;
}
@ -184,7 +203,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* or error message explaining the status.
*/
String getStatusMessage() {
return (String) get(9);
return (String) get(10);
}
/**
@ -192,7 +211,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* resolution, if any.
*/
HistoryRecord setResolution(Resolution value) {
set(10, value);
set(11, value);
return this;
}
@ -201,7 +220,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* resolution, if any.
*/
Resolution getResolution() {
return (Resolution) get(10);
return (Resolution) get(11);
}
/**
@ -209,7 +228,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* info or error message explaining the resolution.
*/
HistoryRecord setResolutionMessage(String value) {
set(11, value);
set(12, value);
return this;
}
@ -218,7 +237,7 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
* info or error message explaining the resolution.
*/
String getResolutionMessage() {
return (String) get(11);
return (String) get(12);
}
// -------------------------------------------------------------------------
@ -244,13 +263,14 @@ class HistoryRecord extends UpdatableRecordImpl<HistoryRecord> {
/**
* Create a detached, initialised HistoryRecord
*/
HistoryRecord(Integer id, String migratedFrom, String migratedTo, Timestamp migratedAt, Long migrationTime, String jooqVersion, String sql, Integer sqlCount, Status status, String statusMessage, Resolution resolution, String resolutionMessage) {
HistoryRecord(Integer id, Timestamp migratedAt, String migratedFrom, String migratedTo, String migratedToTags, Long migrationTime, String jooqVersion, String sql, Integer sqlCount, Status status, String statusMessage, Resolution resolution, String resolutionMessage) {
super(History.HISTORY);
setId(id);
setMigratedAt(migratedAt);
setMigratedFrom(migratedFrom);
setMigratedTo(migratedTo);
setMigratedAt(migratedAt);
setMigratedToTags(migratedToTags);
setMigrationTime(migrationTime);
setJooqVersion(jooqVersion);
setSql(sql);

View File

@ -50,6 +50,7 @@ import static org.jooq.impl.MigrationImpl.Status.MIGRATING;
import static org.jooq.impl.MigrationImpl.Status.REVERTING;
import static org.jooq.impl.MigrationImpl.Status.STARTING;
import static org.jooq.impl.MigrationImpl.Status.SUCCESS;
import static org.jooq.impl.Tools.map;
import java.io.PrintWriter;
import java.io.StringWriter;
@ -70,12 +71,14 @@ import org.jooq.MigrationListener;
import org.jooq.Queries;
import org.jooq.Query;
import org.jooq.Schema;
import org.jooq.Tag;
import org.jooq.exception.DataAccessException;
import org.jooq.exception.DataMigrationException;
import org.jooq.exception.DataMigrationVerificationException;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.StopWatch;
import org.jooq.tools.StringUtils;
import org.jooq.tools.json.JSONArray;
/**
* @author Lukas Eder
@ -308,6 +311,7 @@ final class MigrationImpl extends AbstractScope implements Migration {
.setMigratedAt(new Timestamp(dsl().configuration().clock().instant().toEpochMilli()))
.setMigratedFrom(from().id())
.setMigratedTo(to().id())
.setMigratedToTags(new JSONArray(map(to().tags(), Tag::id)).toString())
.setMigrationTime(0L)
.setSql(queries().toString())
.setSqlCount(queries().queries().length)

View File

@ -1,8 +1,9 @@
CREATE TABLE jooq_migration_history (
id BIGINT NOT NULL IDENTITY,
migrated_at TIMESTAMP NOT NULL,
migrated_from VARCHAR(255) NOT NULL,
migrated_to VARCHAR(255) NOT NULL,
migrated_at TIMESTAMP NOT NULL,
migrated_to_tags CLOB NOT NULL,
migration_time BIGINT NULL,
jooq_version VARCHAR(50) NOT NULL,
sql CLOB NULL,
@ -21,8 +22,10 @@ CREATE INDEX jooq_migr_hist_i1 ON jooq_migration_history (migrated_at);
COMMENT ON TABLE jooq_migration_history IS 'The migration history of jOOQ Migrations.';
COMMENT ON COLUMN jooq_migration_history.id IS 'The database version ID.';
COMMENT ON COLUMN jooq_migration_history.migrated_from IS 'The previous database version ID.';
COMMENT ON COLUMN jooq_migration_history.migrated_at IS 'The date/time when the database version was migrated to.';
COMMENT ON COLUMN jooq_migration_history.migrated_from IS 'The previous database version ID.';
COMMENT ON COLUMN jooq_migration_history.migrated_to IS 'The current database version ID.';
COMMENT ON COLUMN jooq_migration_history.migrated_to_tags IS 'The current database version tags, if any, in JSON array format.';
COMMENT ON COLUMN jooq_migration_history.migration_time IS 'The time in milliseconds it took to migrate to this database version.';
COMMENT ON COLUMN jooq_migration_history.jooq_version IS 'The jOOQ version used to migrate to this database version.';
COMMENT ON COLUMN jooq_migration_history.sql_count IS 'The number of SQL statements that were run to install this database version.';