diff --git a/jOOQ/src/main/java/org/jooq/VersionProvider.java b/jOOQ/src/main/java/org/jooq/VersionProvider.java index 7d04d4c6ce..368e000dde 100644 --- a/jOOQ/src/main/java/org/jooq/VersionProvider.java +++ b/jOOQ/src/main/java/org/jooq/VersionProvider.java @@ -37,8 +37,6 @@ */ package org.jooq; -import java.util.Set; - /** * An SPI that allows for providing a graph of versions. * @@ -55,6 +53,5 @@ public interface VersionProvider { * {@link Version#migrateTo(Version)} and other operations are undefined * if two versions do not have a common ancestor. */ - @Internal // TODO Produce a better type than Set. Possibly, #current() can be obtained from the new result type. - Set provide(); + Versions provide(); } diff --git a/jOOQ/src/main/java/org/jooq/Versions.java b/jOOQ/src/main/java/org/jooq/Versions.java new file mode 100644 index 0000000000..906f139c66 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/Versions.java @@ -0,0 +1,57 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq; + +/** + * A directed, acyclic graph of {@link Version} objects + * + * @author Lukas Eder + */ +public interface Versions { + + /** + * The root version of this graph. + */ + Version root(); + + /** + * Find a version by its id, or null, if no such version was + * found. + */ + Version get(String id); +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultVersionProvider.java b/jOOQ/src/main/java/org/jooq/impl/DefaultVersionProvider.java index 4dcfc684a3..40a18de811 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultVersionProvider.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultVersionProvider.java @@ -37,15 +37,13 @@ */ package org.jooq.impl; -import java.util.LinkedHashSet; -import java.util.Set; - import org.jooq.Configuration; import org.jooq.DSLContext; import org.jooq.Parser; import org.jooq.Source; import org.jooq.Version; import org.jooq.VersionProvider; +import org.jooq.Versions; /** * A default implementation of the {@link VersionProvider} SPI, which provides @@ -67,10 +65,9 @@ public class DefaultVersionProvider implements VersionProvider { } @Override - public Set provide() { - Set result = new LinkedHashSet<>(); + public Versions provide() { Version parent; - result.add(parent = ctx.version("initial")); + VersionsImpl result = new VersionsImpl(parent = ctx.version("initial")); Parser parser = ctx.parser(); for (int i = 0; i < sources.length; i++) diff --git a/jOOQ/src/main/java/org/jooq/impl/MigrationImpl.java b/jOOQ/src/main/java/org/jooq/impl/MigrationImpl.java index 876469745e..55d676bebe 100644 --- a/jOOQ/src/main/java/org/jooq/impl/MigrationImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/MigrationImpl.java @@ -54,10 +54,8 @@ import static org.jooq.impl.MigrationImpl.Status.SUCCESS; import java.sql.Timestamp; import java.util.Arrays; import java.util.Collection; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import org.jooq.Configuration; @@ -77,6 +75,7 @@ import org.jooq.Table; import org.jooq.TableField; import org.jooq.UniqueKey; import org.jooq.Version; +import org.jooq.Versions; import org.jooq.conf.InterpreterSearchSchema; import org.jooq.exception.DataAccessException; import org.jooq.exception.DataMigrationException; @@ -96,7 +95,7 @@ final class MigrationImpl extends AbstractScope implements Migration { private final Version to; private Version from; private Queries queries; - private Map versions; + private Versions versions; MigrationImpl(Configuration configuration, Version to) { super(configuration.derive(new ThreadLocalTransactionProvider(configuration.systemConnectionProvider()))); @@ -127,13 +126,9 @@ final class MigrationImpl extends AbstractScope implements Migration { return queries; } - private final Map versions() { - if (versions == null) { - versions = new HashMap<>(); - - for (Version version : configuration().versionProvider().provide()) - versions.put(version.id(), version); - } + private final Versions versions() { + if (versions == null) + versions = configuration().versionProvider().provide(); return versions; } @@ -159,7 +154,7 @@ final class MigrationImpl extends AbstractScope implements Migration { } private final void validateVersionProvider(Version version) { - if (!versions().containsKey(version.id())) + if (versions().get(version.id()) == null) throw new DataMigrationValidationException("Version is not available from VersionProvider: " + version.id()); } diff --git a/jOOQ/src/main/java/org/jooq/impl/VersionsImpl.java b/jOOQ/src/main/java/org/jooq/impl/VersionsImpl.java new file mode 100644 index 0000000000..9fa4510f77 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/VersionsImpl.java @@ -0,0 +1,84 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.impl; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.jooq.Version; +import org.jooq.Versions; + +/** + * @author Lukas Eder + */ +final class VersionsImpl implements Versions { + + final Version root; + final Map versions; + + VersionsImpl(Version root) { + this.root = root; + this.versions = new HashMap<>(); + + add(root); + } + + void add(Version version) { + versions.put(version.id(), version); + } + + void addAll(Collection v) { + for (Version version : v) + add(version); + } + + void remove(Version version) { + versions.remove(version.id()); + } + + @Override + public final Version root() { + return root; + } + + @Override + public final Version get(String id) { + return versions.get(id); + } +}