Skip to content

Commit

Permalink
[#9506] More work on Migrations API:
Browse files Browse the repository at this point in the history
- Remove TODO doclink for now. We'll update all the exceptions later
- Rename Settings.migrationAllowsUndo to migrationAllowUndo
- Snapshots are a commercial only feature
- Add Settings.migrationAllowInvalidCommits
- Improve invalid commit exception message
- GitCommitProvider message uncommitted-and-untracked
  • Loading branch information
lukaseder committed Nov 28, 2024
1 parent 2991aa3 commit 82ded12
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,35 @@ public class AddSnapshotMojo extends AbstractMigrateMojo {

@Override
final void execute1(Migration migration) throws Exception {
History history = migration.dsl().migrations().history();
Queries queries = migration.queries();
migration.configuration().requireCommercial(() -> "Snapshots are a commercial only feature. Please upgrade to the jOOQ Professional Edition or jOOQ Enterprise Edition.");























if (queries.queries().length > 0) {
Queries queries2 = migration.queries();
getLog().warn("There are pending changes that have not been migrated yet, which are not in the snapshot:\n"
+ queries2);
}

HistoryVersion current = history.current();
File file = new File(file(directory), current.version().id() + "/snapshots/" + fileName(current.version().id(), fileName, "snapshot"));
file.getParentFile().mkdirs();

DDLExportConfiguration config = new DDLExportConfiguration();

// Don't create schema in snapshots if it is managed by the migration.
if (TRUE.equals(migration.settings().isMigrationSchemataCreateSchemaIfNotExists()))
config = config.flags(EnumSet.complementOf(EnumSet.of(DDLFlag.SCHEMA)));

Meta meta = current.version().meta();
String export = meta.ddl(config).toString();

if (getLog().isInfoEnabled())
getLog().info("Writing snapshot to: " + file + "\n" + export);

Files.write(file.toPath(), asList(export), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.util.io.DisabledOutputStream;
import org.jetbrains.annotations.NotNull;

/**
* A {@link CommitProvider} that produces versions from a git repository.
Expand Down Expand Up @@ -225,16 +224,18 @@ private final Commit commit(Commit commit, Status status) {
add(uncommitted, status.getChanged());
del(uncommitted, status.getRemoved());

String message = null;

// [#9506] TODO: It should be possible to migrate to uncommitted changes in dev mode.
if (!uncommitted.isEmpty())
commit = commit.commit("uncommitted", "uncommitted", uncommitted).valid(false);
commit = commit.commit(message = "uncommitted", message, uncommitted).valid(false);

add(untracked, status.getModified());
add(untracked, status.getUntracked());
del(untracked, status.getMissing());

if (!untracked.isEmpty())
commit = commit.commit("untracked", "untracked", untracked).valid(false);
commit = commit.commit(message = message == null ? "untracked" : "uncommitted-and-untracked", message, untracked).valid(false);

return commit;
}
Expand Down
70 changes: 58 additions & 12 deletions jOOQ/src/main/java/org/jooq/conf/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,9 @@ public class Settings
@XmlSchemaType(name = "string")
protected MigrationDefaultContentType migrationDefaultContentType = MigrationDefaultContentType.INCREMENT;
@XmlElement(defaultValue = "false")
protected Boolean migrationAllowsUndo = false;
protected Boolean migrationAllowUndo = false;
@XmlElement(defaultValue = "false")
protected Boolean migrationAllowInvalidCommits = false;
@XmlElement(defaultValue = "false")
protected Boolean migrationRevertUntracked = false;
@XmlElement(defaultValue = "false")
Expand Down Expand Up @@ -6223,8 +6225,8 @@ public void setMigrationDefaultContentType(MigrationDefaultContentType value) {
* {@link Boolean }
*
*/
public Boolean isMigrationAllowsUndo() {
return migrationAllowsUndo;
public Boolean isMigrationAllowUndo() {
return migrationAllowUndo;
}

/**
Expand All @@ -6235,8 +6237,32 @@ public Boolean isMigrationAllowsUndo() {
* {@link Boolean }
*
*/
public void setMigrationAllowsUndo(Boolean value) {
this.migrationAllowsUndo = value;
public void setMigrationAllowUndo(Boolean value) {
this.migrationAllowUndo = value;
}

/**
* Whether migrations to invalid commits ({@link org.jooq.Commit#valid()}) are allowed.<p><strong>This is a potentially destructive feature, which should not be turned on in production</strong>. It is useful mostly to quickly test uncommited or inconsistent changes in development.
*
* @return
* possible object is
* {@link Boolean }
*
*/
public Boolean isMigrationAllowInvalidCommits() {
return migrationAllowInvalidCommits;
}

/**
* Whether migrations to invalid commits ({@link org.jooq.Commit#valid()}) are allowed.<p><strong>This is a potentially destructive feature, which should not be turned on in production</strong>. It is useful mostly to quickly test uncommited or inconsistent changes in development.
*
* @param value
* allowed object is
* {@link Boolean }
*
*/
public void setMigrationAllowInvalidCommits(Boolean value) {
this.migrationAllowInvalidCommits = value;
}

/**
Expand Down Expand Up @@ -9415,8 +9441,17 @@ public Settings withMigrationDefaultContentType(MigrationDefaultContentType valu
* Whether migrations are allowed to be executed in inverse order.<p><strong>This is a potentially destructive feature, which should not be turned on in production</strong>. It is useful mostly to quickly switch between branches in a development environment. This feature is available only in commercial distributions.
*
*/
public Settings withMigrationAllowsUndo(Boolean value) {
setMigrationAllowsUndo(value);
public Settings withMigrationAllowUndo(Boolean value) {
setMigrationAllowUndo(value);
return this;
}

/**
* Whether migrations to invalid commits ({@link org.jooq.Commit#valid()}) are allowed.<p><strong>This is a potentially destructive feature, which should not be turned on in production</strong>. It is useful mostly to quickly test uncommited or inconsistent changes in development.
*
*/
public Settings withMigrationAllowInvalidCommits(Boolean value) {
setMigrationAllowInvalidCommits(value);
return this;
}

Expand Down Expand Up @@ -9985,7 +10020,8 @@ public final void appendTo(XMLBuilder builder) {
builder.append("migrationDefaultSchema", migrationDefaultSchema);
builder.append("migrationSchemataCreateSchemaIfNotExists", migrationSchemataCreateSchemaIfNotExists);
builder.append("migrationDefaultContentType", migrationDefaultContentType);
builder.append("migrationAllowsUndo", migrationAllowsUndo);
builder.append("migrationAllowUndo", migrationAllowUndo);
builder.append("migrationAllowInvalidCommits", migrationAllowInvalidCommits);
builder.append("migrationRevertUntracked", migrationRevertUntracked);
builder.append("migrationAutoBaseline", migrationAutoBaseline);
builder.append("migrationAutoVerification", migrationAutoVerification);
Expand Down Expand Up @@ -11841,12 +11877,21 @@ public boolean equals(Object that) {
return false;
}
}
if (migrationAllowsUndo == null) {
if (other.migrationAllowsUndo!= null) {
if (migrationAllowUndo == null) {
if (other.migrationAllowUndo!= null) {
return false;
}
} else {
if (!migrationAllowUndo.equals(other.migrationAllowUndo)) {
return false;
}
}
if (migrationAllowInvalidCommits == null) {
if (other.migrationAllowInvalidCommits!= null) {
return false;
}
} else {
if (!migrationAllowsUndo.equals(other.migrationAllowsUndo)) {
if (!migrationAllowInvalidCommits.equals(other.migrationAllowInvalidCommits)) {
return false;
}
}
Expand Down Expand Up @@ -12363,7 +12408,8 @@ public int hashCode() {
result = ((prime*result)+((migrationDefaultSchema == null)? 0 :migrationDefaultSchema.hashCode()));
result = ((prime*result)+((migrationSchemataCreateSchemaIfNotExists == null)? 0 :migrationSchemataCreateSchemaIfNotExists.hashCode()));
result = ((prime*result)+((migrationDefaultContentType == null)? 0 :migrationDefaultContentType.hashCode()));
result = ((prime*result)+((migrationAllowsUndo == null)? 0 :migrationAllowsUndo.hashCode()));
result = ((prime*result)+((migrationAllowUndo == null)? 0 :migrationAllowUndo.hashCode()));
result = ((prime*result)+((migrationAllowInvalidCommits == null)? 0 :migrationAllowInvalidCommits.hashCode()));
result = ((prime*result)+((migrationRevertUntracked == null)? 0 :migrationRevertUntracked.hashCode()));
result = ((prime*result)+((migrationAutoBaseline == null)? 0 :migrationAutoBaseline.hashCode()));
result = ((prime*result)+((migrationAutoVerification == null)? 0 :migrationAutoVerification.hashCode()));
Expand Down
16 changes: 10 additions & 6 deletions jOOQ/src/main/java/org/jooq/impl/MigrationImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,15 @@ private final void verify0(DefaultMigrationContext ctx) {
validateCommitProvider(ctx, to());
revertUntracked(ctx, null, currentRecord);

// [#9506] TODO: Better error handling helping the understand see their mistake
if (!to().valid())
throw new DataMigrationVerificationException("Commit is not a valid commit to migrate to: " + to());
if (!to().valid() && !TRUE.equals(ctx.settings().isMigrationAllowInvalidCommits()))
throw new DataMigrationVerificationException(
"""
Commit is not a valid commit to migrate to: {commit}
Invalid commits include:
- Uncommitted or untracked changes in the GitCommitProvider
- Commits leading to inconsistent migration states due to editing of commit paths
""".replace("{commit}", to().id())
);
}

private final void validateCommitProvider(DefaultMigrationContext ctx, Commit commit) {
Expand All @@ -216,9 +222,7 @@ private final void validateCommitProvider(DefaultMigrationContext ctx, Commit co
The commit referencing the schema: {commit}.
All schemas that are referenced from commits in a migration must be configured for
inclusion in the migration
TODO doclink
inclusion in the migration.
""".replace("{schema}", schema.toString())
.replace("{commit}", commit.toString())
);
Expand Down
6 changes: 5 additions & 1 deletion jOOQ/src/main/resources/org/jooq/xsd/jooq-runtime-3.20.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -1548,10 +1548,14 @@ deployed on an RDBMS that does not.]]></jxb:javadoc></jxb:property></appinfo></a
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The default {@link org.jooq.ContentType} that is used when loading migrations.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>

<element name="migrationAllowsUndo" type="boolean" minOccurs="0" maxOccurs="1" default="false">
<element name="migrationAllowUndo" type="boolean" minOccurs="0" maxOccurs="1" default="false">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Whether migrations are allowed to be executed in inverse order.<p><strong>This is a potentially destructive feature, which should not be turned on in production</strong>. It is useful mostly to quickly switch between branches in a development environment. This feature is available only in commercial distributions.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>

<element name="migrationAllowInvalidCommits" type="boolean" minOccurs="0" maxOccurs="1" default="false">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Whether migrations to invalid commits ({@link org.jooq.Commit#valid()}) are allowed.<p><strong>This is a potentially destructive feature, which should not be turned on in production</strong>. It is useful mostly to quickly test uncommited or inconsistent changes in development.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>

<element name="migrationRevertUntracked" type="boolean" minOccurs="0" maxOccurs="1" default="false">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Whether migrations revert any untracked changes in the schemas that are being migrated.<p><strong>This is a potentially destructive feature, which should not be turned on in production</strong>. It is useful mostly to quickly revert any elements created in a development environment. This feature is available only in commercial distributions.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
Expand Down

0 comments on commit 82ded12

Please sign in to comment.