Skip to content

Commit d2a9b9f

Browse files
committed
1 parent d279139 commit d2a9b9f

File tree

3 files changed

+81
-51
lines changed

3 files changed

+81
-51
lines changed
Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,57 @@
11
package org.apache.ibatis.io;
22

3-
import java.io.Closeable;
4-
import java.io.File;
5-
import java.io.FileInputStream;
6-
import java.io.FileOutputStream;
7-
import java.io.IOException;
3+
import java.io.*;
84
import java.nio.channels.FileChannel;
95
import java.util.Properties;
106

117
public class ExternalResources {
128

13-
private ExternalResources() {
14-
// do nothing
15-
}
16-
17-
public static void copyExternalResource(File sourceFile, File destFile) throws IOException {
18-
if (!destFile.exists()) {
19-
destFile.createNewFile();
20-
}
21-
22-
FileChannel source = null;
23-
FileChannel destination = null;
24-
try {
25-
source = new FileInputStream(sourceFile).getChannel();
26-
destination = new FileOutputStream(destFile).getChannel();
27-
destination.transferFrom(source, 0, source.size());
28-
} finally {
29-
closeQuietly(source);
30-
closeQuietly(destination);
31-
}
9+
private ExternalResources() {
10+
// do nothing
11+
}
3212

13+
public static void copyExternalResource(File sourceFile, File destFile) throws IOException {
14+
if (!destFile.exists()) {
15+
destFile.createNewFile();
3316
}
3417

35-
private static void closeQuietly(Closeable closeable) {
36-
if (closeable != null) {
37-
try {
38-
closeable.close();
39-
} catch (IOException e) {
40-
// do nothing, close quietly
41-
}
42-
}
18+
FileChannel source = null;
19+
FileChannel destination = null;
20+
try {
21+
source = new FileInputStream(sourceFile).getChannel();
22+
destination = new FileOutputStream(destFile).getChannel();
23+
destination.transferFrom(source, 0, source.size());
24+
} finally {
25+
closeQuietly(source);
26+
closeQuietly(destination);
4327
}
4428

45-
public static String getConfiguredTemplate(String templatePath, String templateProperty) {
46-
String templateName = "";
47-
Properties migrationProperties = new Properties();
29+
}
4830

49-
try {
50-
migrationProperties.load(new FileInputStream(templatePath));
51-
templateName = migrationProperties.getProperty(templateProperty);
52-
} catch (Exception e) {
53-
e.printStackTrace();
54-
}
31+
private static void closeQuietly(Closeable closeable) {
32+
if (closeable != null) {
33+
try {
34+
closeable.close();
35+
} catch (IOException e) {
36+
// do nothing, close quietly
37+
}
38+
}
39+
}
40+
41+
public static String getConfiguredTemplate(String templatePath, String templateProperty) throws FileNotFoundException {
42+
String templateName = "";
43+
Properties migrationProperties = new Properties();
44+
45+
try {
46+
migrationProperties.load(new FileInputStream(templatePath));
47+
templateName = migrationProperties.getProperty(templateProperty);
48+
} catch (FileNotFoundException e) {
49+
throw e;
50+
} catch (Exception e) {
51+
e.printStackTrace();
52+
}
5553

56-
return templateName;
57-
}
54+
return templateName;
55+
}
5856

5957
}

src/main/java/org/apache/ibatis/migration/commands/NewCommand.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package org.apache.ibatis.migration.commands;
22

3-
import java.io.File;
4-
import java.util.Properties;
5-
63
import org.apache.ibatis.io.ExternalResources;
74
import org.apache.ibatis.migration.MigrationException;
85

6+
import java.io.File;
7+
import java.io.FileNotFoundException;
8+
import java.util.Properties;
9+
910
public class NewCommand extends BaseCommand {
1011

1112
private static final String MIGRATIONS_HOME = "MIGRATIONS_HOME";
13+
private static final String MIGRATIONS_HOME_PROPERTY = "migrationHome";
1214
private static final String CUSTOM_NEW_COMMAND_TEMPATE_PROPERTY = "new_command.template";
1315
private static final String MIGRATIONS_PROPERTIES = "migration.properties";
14-
protected String customConfiguredTemplate = "";
1516

1617
public NewCommand(File repository, String environment, String template, boolean force) {
1718
super(repository, environment, template, force);
@@ -29,17 +30,31 @@ public void execute(String... params) {
2930
String migrationsHome = "";
3031
migrationsHome = System.getenv(MIGRATIONS_HOME);
3132

33+
// Check if there is a system property
34+
if (migrationsHome == null) {
35+
migrationsHome = System.getProperty(MIGRATIONS_HOME_PROPERTY);
36+
}
37+
3238
if (this.template != null) {
3339
copyExternalResourceTo(template, scriptFile(filename), variables);
3440
} else if ((migrationsHome != null) && (!migrationsHome.equals(""))) {
35-
//get template name from properties file
36-
customConfiguredTemplate = ExternalResources.getConfiguredTemplate(migrationsHome + "/" + MIGRATIONS_PROPERTIES, CUSTOM_NEW_COMMAND_TEMPATE_PROPERTY);
37-
copyExternalResourceTo(migrationsHome + "/" + customConfiguredTemplate, scriptFile(filename), variables);
41+
try {
42+
//get template name from properties file
43+
final String customConfiguredTemplate = ExternalResources.getConfiguredTemplate(migrationsHome + "/" + MIGRATIONS_PROPERTIES, CUSTOM_NEW_COMMAND_TEMPATE_PROPERTY);
44+
copyExternalResourceTo(migrationsHome + "/" + customConfiguredTemplate, scriptFile(filename), variables);
45+
} catch (FileNotFoundException e) {
46+
printStream.append("Your migrations configuration did not find your custom template. Using the default template.");
47+
copyDefaultTemplate(variables, filename);
48+
}
3849
} else {
39-
copyResourceTo("org/apache/ibatis/migration/template_migration.sql", scriptFile(filename), variables);
50+
copyDefaultTemplate(variables, filename);
4051
}
4152

4253
printStream.println("Done!");
4354
printStream.println();
4455
}
56+
57+
private void copyDefaultTemplate(Properties variables, String filename) {
58+
copyResourceTo("org/apache/ibatis/migration/template_migration.sql", scriptFile(filename), variables);
59+
}
4560
}

src/test/java/org/apache/ibatis/migration/MigratorTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.ibatis.BaseDataTest;
2020
import org.apache.ibatis.io.Resources;
2121
import org.apache.ibatis.jdbc.SqlRunner;
22+
import org.apache.ibatis.migration.commands.NewCommand;
2223
import org.junit.AfterClass;
2324
import org.junit.BeforeClass;
2425
import org.junit.Test;
@@ -204,6 +205,22 @@ public void useCustomTemplateWithNoValue() throws Exception {
204205
templatePath.delete();
205206
}
206207

208+
@Test
209+
public void useCustomTemplateWithBadPath() throws Exception {
210+
System.setProperty("migrationHome", "/tmp");
211+
File basePath = getTempDir();
212+
safeMigratorMain(args("--path=" + basePath.getAbsolutePath(), "init"));
213+
assertNotNull(basePath.list());
214+
assertEquals(4, basePath.list().length);
215+
File scriptPath = new File(basePath.getCanonicalPath() + File.separator + "scripts");
216+
assertEquals(3, scriptPath.list().length);
217+
218+
safeMigratorMain(args("--path=" + basePath.getAbsolutePath(), "new", "test new migration"));
219+
assertEquals(4, scriptPath.list().length);
220+
221+
assertTrue(buffer.toString().contains("Your migrations configuration did not find your custom template. Using the default template."));
222+
}
223+
207224
private String[] args(String... args) {
208225
return args;
209226
}

0 commit comments

Comments
 (0)