#2507 - Script runner does not work with file system path

This commit is contained in:
Rob Bygrave
2022-02-02 13:02:49 +13:00
parent 376b3a9f13
commit 170e323ee9
3 changed files with 79 additions and 54 deletions
@@ -1,6 +1,7 @@
package io.ebean;
import java.net.URL;
import java.nio.file.Path;
import java.util.Map;
/**
@@ -23,10 +24,10 @@ public interface ScriptRunner {
/**
* Run a script given the resource path (that should start with "/").
*/
void run(String path);
void run(String resourcePath);
/**
* Run a script given the resource path (that should start with "/") and place holders.
* Run a script given the resource path (that should start with "/") and place-holders.
*
* <pre>{@code
*
@@ -38,7 +39,7 @@ public interface ScriptRunner {
*
* }</pre>
*/
void run(String path, Map<String, String> placeholderMap);
void run(String resourcePath, Map<String, String> placeholderMap);
/**
* Run a DDL or SQL script given the resource.
@@ -46,10 +47,20 @@ public interface ScriptRunner {
void run(URL resource);
/**
* Run a DDL or SQL script given the resource and place holders.
* Run a DDL or SQL script given the resource and place-holders.
*/
void run(URL resource, Map<String, String> placeholderMap);
/**
* Run a DDL or SQL script given the file.
*/
void run(Path file);
/**
* Run a DDL or SQL script given the file and place-holders.
*/
void run(Path file, Map<String, String> placeholderMap);
/**
* Run the raw provided DDL or SQL script.
*
@@ -8,15 +8,15 @@ import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.util.UrlHelper;
import javax.persistence.PersistenceException;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.*;
import java.net.URL;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import static java.util.Objects.requireNonNull;
final class DScriptRunner implements ScriptRunner {
private static final String NEWLINE = "\n";
@@ -30,8 +30,8 @@ final class DScriptRunner implements ScriptRunner {
}
@Override
public void run(String path) {
run(path, null);
public void run(String resourcePath) {
run(resourcePath, null);
}
@Override
@@ -49,27 +49,42 @@ final class DScriptRunner implements ScriptRunner {
run(resource, null, placeholderMap);
}
@Override
public void run(Path file) {
run(file, null);
}
@Override
public void run(Path file, Map<String, String> placeholderMap) {
requireNonNull(file);
String scriptName = file.toFile().getName();
String content = fileContent(file);
runScript(content, scriptName, placeholderMap, false);
}
private void run(URL resource, String scriptName, Map<String, String> placeholderMap) {
if (resource == null) {
throw new IllegalArgumentException("resource is null?");
}
requireNonNull(resource);
if (scriptName == null) {
scriptName = resource.getFile();
}
String content = content(resource);
runScript(content, scriptName, placeholderMap, false);
}
private String content(URL resource) {
if (resource == null) {
throw new IllegalArgumentException("resource is null?");
}
try (InputStream inputStream = UrlHelper.openNoCache(resource);
Reader reader = IOUtils.newReader(inputStream)) {
private String fileContent(Path file) {
try (InputStream inputStream = new FileInputStream(file.toFile());
Reader reader = IOUtils.newReader(inputStream)) {
return readContent(reader);
} catch (IOException e) {
throw new PersistenceException("Failed to read script content", e);
}
}
private String content(URL resource) {
requireNonNull(resource);
try (InputStream inputStream = UrlHelper.openNoCache(resource);
Reader reader = IOUtils.newReader(inputStream)) {
return readContent(reader);
} catch (IOException e) {
throw new PersistenceException("Failed to read script content", e);
}
@@ -85,10 +100,9 @@ final class DScriptRunner implements ScriptRunner {
*/
private void runScript(String content, String scriptName, Map<String, String> placeholderMap, boolean useAutoCommit) {
try {
if (placeholderMap != null) {
if (placeholderMap != null && !placeholderMap.isEmpty()) {
content = ScriptTransform.build(null, placeholderMap).transform(content);
}
try (Connection connection = obtainConnection()) {
DdlRunner runner = new DdlRunner(useAutoCommit, scriptName, platformName);
runner.runAll(content, connection);
@@ -110,7 +124,6 @@ final class DScriptRunner implements ScriptRunner {
}
private String readContent(Reader reader) throws IOException {
StringBuilder buf = new StringBuilder();
try (LineNumberReader lineReader = new LineNumberReader(reader)) {
String line;
@@ -6,18 +6,39 @@ import org.tests.model.basic.Order;
import org.tests.model.basic.OrderDetail;
import org.tests.model.basic.ResetBasicData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
public class EbeanServer_refresh {
class EbeanServer_refresh {
private Path createSqlFile() throws IOException {
File f = File.createTempFile("test-script", ".sql");
Path path = f.toPath();
List<String> lines = new ArrayList<>();
lines.add("select * from o_customer;");
Files.write(path, lines, StandardOpenOption.TRUNCATE_EXISTING);
return path;
}
@Test
public void basic() {
void script_run_asFile() throws IOException {
ResetBasicData.reset();
Path path = createSqlFile();
DB.script().run(path);
DB.script().run(path, Collections.emptyMap());
}
@Test
void basic() {
Map<String, String> map = new HashMap<>();
map.put("tableName", "e_basic");
@@ -47,8 +68,7 @@ public class EbeanServer_refresh {
}
@Test
public void refresh_when_oneToManyLoaded() {
void refresh_when_oneToManyLoaded() {
ResetBasicData.reset();
Order order = DB.find(Order.class, 1);
@@ -59,8 +79,7 @@ public class EbeanServer_refresh {
}
@Test
public void refresh_when_oneToManyVanilla() {
void refresh_when_oneToManyVanilla() {
ResetBasicData.reset();
Order order = DB.find(Order.class, 1);
@@ -71,8 +90,7 @@ public class EbeanServer_refresh {
}
@Test
public void refresh_when_oneToManyNull() {
void refresh_when_oneToManyNull() {
ResetBasicData.reset();
Order order = DB.find(Order.class, 1);
@@ -82,15 +100,11 @@ public class EbeanServer_refresh {
DB.refresh(order);
}
@Test
public void refresh_on_details_new() {
void refresh_on_details_new() {
ResetBasicData.reset();
Order order = DB.find(Order.class, 1);
DB.refresh(order); // call refresh BEFORE first access on "getDetail";
assertThat(order.getDetails()).hasSize(3);
@@ -100,13 +114,9 @@ public class EbeanServer_refresh {
DB.save(detail);
try {
assertThat(order.getDetails()).hasSize(3);
DB.refresh(order);
assertThat(order.getDetails()).hasSize(4);
} finally {
DB.delete(detail); // restore old state
}
@@ -116,13 +126,9 @@ public class EbeanServer_refresh {
assertThat(order.getDetails()).hasSize(3);
}
@Test
public void refresh_on_details_changed() {
void refresh_on_details_changed() {
ResetBasicData.reset();
Order order = DB.find(Order.class, 1);
DB.refresh(order); // call refresh BEFORE first access on "getDetail"
@@ -139,11 +145,8 @@ public class EbeanServer_refresh {
try {
assertThat(order.getDetails().get(0).getOrderQty()).isEqualTo(5);
DB.refresh(order);
assertThat(order.getDetails().get(0).getOrderQty()).isEqualTo(42);
} finally {
// restore old value
detail.setOrderQty(5);
@@ -151,8 +154,6 @@ public class EbeanServer_refresh {
}
DB.refresh(order);
assertThat(order.getDetails().get(0).getOrderQty()).isEqualTo(5);
}
}