From f7fe559711b4b502b646af2de8f41baaa137dee1 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 21 Nov 2019 20:12:18 +0100 Subject: [PATCH] [jOOQ/jOOQ#9506] Add CHANGELOG.SQL and CHANGELOG.STATUS columns --- .../java/org/jooq/impl/MigrationImpl.java | 80 +++++++++++++++++-- 1 file changed, 72 insertions(+), 8 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/MigrationImpl.java b/jOOQ/src/main/java/org/jooq/impl/MigrationImpl.java index 723e9352ee..041e01627a 100644 --- a/jOOQ/src/main/java/org/jooq/impl/MigrationImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/MigrationImpl.java @@ -129,6 +129,9 @@ final class MigrationImpl extends AbstractScope implements Migration { @Override public final MigrationResult migrate() throws DataDefinitionException { + + // TODO: Transactions don't really make sense in most dialects. In some, they do + // e.g. PostgreSQL supports transactional DDL. Check if we're getting this right. return run(new ContextTransactionalCallable() { @Override public MigrationResult run() { @@ -142,8 +145,9 @@ final class MigrationImpl extends AbstractScope implements Migration { // TODO: Implement additional out-of-the-box sanity checks // TODO: Allow undo migrations only if enabled explicitly // TODO: Add number of statements to CHANGELOG - // TODO: Add option to log the entire changeset in CHANGELOG - // TODO: Add a migration state to the CHANGELOG: SUCCESS, PENDING, FAILURE + // TODO: Add some migration settings, e.g. whether CHANGELOG.SQL should be filled + // TODO: Migrate the CHANGELOG table with the Migration API + // TODO: Create an Enum for CHANGELOG.STATUS log.info("jOOQ Migrations", "Version " + from().id() + " is migrated to " + to().id()); @@ -162,14 +166,30 @@ final class MigrationImpl extends AbstractScope implements Migration { .setMigratedFrom(from().id()) .setMigratedTo(to().id()) .setMigrationTime(0L) + .setSql(queries().toString()) + .setStatus("PENDING") .insert(); // TODO: Make batching an option - queries().executeBatch(); + try { + queries().executeBatch(); - newRecord - .setMigrationTime(watch.split() / 1000000L) - .update(); + newRecord + .setMigrationTime(watch.split() / 1000000L) + .setStatus("SUCCESS") + .update(); + } + catch (DataAccessException e) { + + // TODO: Make sure this is committed, given that we're re-throwing the exception. + // TODO: How can we recover from failure? + newRecord + .setMigrationTime(watch.split() / 1000000L) + .setStatus("FAILURE") + .update(); + + throw e; + } return MIGRATION_RESULT; } @@ -210,9 +230,11 @@ final class MigrationImpl extends AbstractScope implements Migration { // XXX: Generated code // ------------------------------------------------------------------------- - // These classes have been generated and copied here. It would be desirable: + // TODO These classes have been generated and copied here. It would be desirable: // - [#6948] To be able to generate package private classes directly inside of other classes // - [#7444] Alternatively, have a simple public API replacing TableImpl + // - If the above cannot be implemented, generate these in the org.jooq.impl package + // and make them package private or @Internal /** * The migration log of jOOQ Migrations. @@ -265,6 +287,16 @@ final class MigrationImpl extends AbstractScope implements Migration { */ public final TableField JOOQ_VERSION = createField(DSL.name("JOOQ_VERSION"), org.jooq.impl.SQLDataType.VARCHAR(50).nullable(false), this, "The jOOQ version used to migrate to this database version."); + /** + * The column JOOQ_MIGRATIONS_CHANGELOG.SQL. The jOOQ version used to migrate to this database version. + */ + public final TableField SQL = createField(DSL.name("SQL"), org.jooq.impl.SQLDataType.CLOB, this, "The SQL statements that were run to install this database version."); + + /** + * The column JOOQ_MIGRATIONS_CHANGELOG.JOOQ_VERSION. The jOOQ version used to migrate to this database version. + */ + public final TableField STATUS = createField(DSL.name("STATUS"), org.jooq.impl.SQLDataType.VARCHAR(10).nullable(false), this, "The database version installation status."); + /** * Create a JOOQ_MIGRATIONS_CHANGELOG table reference */ @@ -415,6 +447,36 @@ final class MigrationImpl extends AbstractScope implements Migration { return (String) get(5); } + /** + * Setter for JOOQ_MIGRATIONS_CHANGELOG.SQL. The SQL statements that were run to install this database version. + */ + public JooqMigrationsChangelogRecord setSql(String value) { + set(6, value); + return this; + } + + /** + * Getter for JOOQ_MIGRATIONS_CHANGELOG.SQL. The SQL statements that were run to install this database version. + */ + public String getSql() { + return (String) get(6); + } + + /** + * Setter for JOOQ_MIGRATIONS_CHANGELOG.STATUS. The database version installation status. + */ + public JooqMigrationsChangelogRecord setStatus(String value) { + set(7, value); + return this; + } + + /** + * Getter for JOOQ_MIGRATIONS_CHANGELOG.STATUS. The database version installation status. + */ + public String getStatus() { + return (String) get(7); + } + // ------------------------------------------------------------------------- // Primary key information // ------------------------------------------------------------------------- @@ -438,7 +500,7 @@ final class MigrationImpl extends AbstractScope implements Migration { /** * Create a detached, initialised JooqMigrationsChangelogRecord */ - public JooqMigrationsChangelogRecord(Long id, String migratedFrom, String migratedTo, Timestamp migratedAt, Long migrationTime, String jooqVersion) { + public JooqMigrationsChangelogRecord(Long id, String migratedFrom, String migratedTo, Timestamp migratedAt, Long migrationTime, String jooqVersion, String sql, String status) { super(JooqMigrationsChangelog.JOOQ_MIGRATIONS_CHANGELOG); set(0, id); @@ -447,6 +509,8 @@ final class MigrationImpl extends AbstractScope implements Migration { set(3, migratedAt); set(4, migrationTime); set(5, jooqVersion); + set(6, sql); + set(7, status); } } }