Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to use their own implementation of FileMigrationLoader. R… #107

Merged
merged 1 commit into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
import org.apache.ibatis.migration.utils.Util;

public class FileMigrationLoader implements MigrationLoader {
private final File scriptsDir;
protected final File scriptsDir;

private final String charset;
protected final String charset;

private final Properties variables;
protected final Properties variables;

public FileMigrationLoader(File scriptsDir, String charset, Properties variables) {
super();
Expand Down Expand Up @@ -59,11 +59,11 @@ public List<Change> getMigrations() {
return migrations;
}

private boolean isSpecialFile(String filename) {
protected boolean isSpecialFile(String filename) {
return "bootstrap.sql".equals(filename) || "onabort.sql".equals(filename);
}

private Change parseChangeFromFilename(String filename) {
protected Change parseChangeFromFilename(String filename) {
try {
Change change = new Change();
int lastIndexOfDot = filename.lastIndexOf(".");
Expand Down Expand Up @@ -105,7 +105,7 @@ public Reader getOnAbortReader() {
return getSoleScriptReader(fileName);
}

private Reader getSoleScriptReader(String fileName) {
protected Reader getSoleScriptReader(String fileName) {
try {
File scriptFile = Util.file(scriptsDir, fileName);
if (scriptFile.exists()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 2010-2017 the original author or authors.
*
* 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.
*/
package org.apache.ibatis.migration;

import java.io.File;
import java.util.Properties;

public interface FileMigrationLoaderFactory {
MigrationLoader create(File scriptsDir, String charset, Properties variables);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.TimeZone;

import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
Expand All @@ -45,6 +46,7 @@
import org.apache.ibatis.migration.DataSourceConnectionProvider;
import org.apache.ibatis.migration.Environment;
import org.apache.ibatis.migration.FileMigrationLoader;
import org.apache.ibatis.migration.FileMigrationLoaderFactory;
import org.apache.ibatis.migration.MigrationException;
import org.apache.ibatis.migration.MigrationLoader;
import org.apache.ibatis.migration.hook.FileHookScriptFactory;
Expand Down Expand Up @@ -301,8 +303,17 @@ private File getCustomDriverPath() {
}

protected MigrationLoader getMigrationLoader() {
return new FileMigrationLoader(paths.getScriptPath(), environment().getScriptCharset(),
environment().getVariables());
File scriptPath = paths.getScriptPath();
String scriptCharset = environment().getScriptCharset();
Properties variables = environment().getVariables();
MigrationLoader migrationLoader = null;
for (FileMigrationLoaderFactory factory : ServiceLoader.load(FileMigrationLoaderFactory.class)) {
if (migrationLoader != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea. How about taking it one step higher to MigrationLoaderFactory so that it's no longer file based? Basically, you can abstract the Migration and get an iterator of them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I agree, we have a MigrationLoader so a MigrationLoader factory makes a lot of sense. The FileMigrationLoader is just an implementation.
It might be more useful to just give it the path(),environment() for flexibility.

Copy link
Member Author

@harawata harawata Dec 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you guys for the quick feedback!

How about taking it one step higher to MigrationLoaderFactory so that it's no longer file based?

I thought about that.
The return type of FileMigrationLoaderFactory#create() is MigrationLoader, so by design, it is possible to return a custom MigrationLoader that loads migrations from some other source (i.e. it does not have to be a subclass of FileMigrationLoader).

But this is BaseCommand which is the base class for the command line API and the current command line workflow cannot be separated from files completely (e.g. migrate new).
That is why I named it FileMigrationLoaderFactory.

It might be more useful to just give it the path(),environment() for flexibility.

It was my first implementation and I'm still not that sure which is better, so if you think that is better, I'll change. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea it's tied to Files atm but we also have for example JavaMigrationLoader that is useful for runtime migrations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that. But

JavaMigrationLoader has a method getMigrations() which returns a List<Change> (counter conventional, btw).

When you look at Change you see

public class Change implements Comparable<Change>, Cloneable {

  private BigDecimal id;
  private String description;
  private String appliedTimestamp;
  private String filename;
}

Notice the part where it says fileName? It's still anchored to the file system.

Side question: Why is the id BigDecimal? Do you expect a version like 3.1415926?

Copy link
Member

@h3adache h3adache Dec 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean @chb0github, both have getMigrations which returns a list of changes.

Even if we change the signature of create() method as @h3adache suggested, it still is coupled with file system and FileMigrationLoaderFactory describes the purpose of this class more accurately, IMO.

That is true. I was just thinking about it from a Factory standpoint. The factory author can create the loader any way they like so I just assumed they would have the proper classLoader/packageNames to create the JavaMigrationLoader

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harawata if you have no more changes let's pull this in. Don't worry about the Factory idea for now. We can deal with it when we have to like you said

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h3adache ,
Okay!
Regarding the arguments of create(), I re-checked SelectedPaths and Environment and couldn't think of a good use case for other properties.
Let's merge this as it is and see what users say.

I'll add documentation and tests as soon as possible.

Thank you both for insightful comments!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use it to create a JarFileMigrationLoader. :)
ATM our FileMigrationLoader doesn't read jar bundled migrations properly.
I create a JarFileMigrationLoader to handle that for me but I read the properties from the bundled environment file in the jar as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new MigrationException("Found multiple implementations of FileMigrationLoaderFactory via SPI.");
}
migrationLoader = factory.create(scriptPath, scriptCharset, variables);
}
return migrationLoader != null ? migrationLoader : new FileMigrationLoader(scriptPath, scriptCharset, variables);
}

protected MigrationHook createUpHook() {
Expand Down