ENH: Add DB Migration runner (as alternative to running via FlywayDB etc) #636

This commit is contained in:
Robin Bygrave committed 2016-04-06 14:31:20 +12:00
commit a7b239a1b7
27 files changed
+846 -22

No files matched your search

@@ -14,6 +14,7 @@ import com.avaje.ebean.config.EncryptKeyManager;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
import com.avaje.ebean.dbmigration.DdlGenerator;
import com.avaje.ebean.dbmigration.MigrationRunner;
import com.avaje.ebean.event.BeanPersistController;
import com.avaje.ebean.event.readaudit.ReadAuditLogger;
import com.avaje.ebean.event.readaudit.ReadAuditPrepare;
@@ -339,6 +340,9 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
if (migrationConfig != null) {
migrationConfig.generateOnStart(this);
}
MigrationRunner migrationRunner = new MigrationRunner(this);
migrationRunner.run();
}
/**
@@ -0,0 +1,99 @@
package com.avaje.ebeaninternal.util;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* Utilities for encoding and decoding strings.
*/
public final class EncodeUtil {
private EncodeUtil() {
/* no instances */
}
/**
* URL-encodes the specified UTF-8 string.
*/
public static String urlEncode(String string) {
if (string == null) return null;
try {
return URLEncoder.encode(string, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Support for UTF-8 is mandated by the Java spec", e);
}
}
/**
* URL-decodes the specified string as a UTF-8 string.
*/
public static String urlDecode(String string) {
if (string == null) return null;
try {
return URLDecoder.decode(string, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Support for UTF-8 is mandated by the Java spec", e);
}
}
/**
* Returns the bytes corresponding to the specified ASCII string.
*/
public static byte[] asciiToBytes(String string) {
if (string == null) return null;
try {
return string.getBytes("US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Support for US-ASCII is mandated by the Java spec", e);
}
}
/**
* Returns the ASCII string corresponding to the specified bytes.
*/
public static String bytesToAscii(byte[] data) {
try {
return new String(data, "US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Support for US-ASCII is mandated by the Java spec", e);
}
}
/**
* Returns the bytes corresponding to the specified UTF-8 string.
*/
public static byte[] utf8ToBytes(String string) {
if (string == null) return null;
try {
return string.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Support for UTF-8 is mandated by the Java spec", e);
}
}
/**
* Returns the UTF-8 string corresponding to the specified bytes.
*/
public static String bytesToUtf8(byte[] data) {
try {
return new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Support for UTF-8 is mandated by the Java spec", e);
}
}
/**
* Returns the UTF-8 string corresponding to the specified bytes.
*/
public static String decodeBytes(byte[] data, String encoding) {
try {
return new String(data, encoding);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Error decoding bytes with "+encoding, e);
}
}
}
@@ -0,0 +1,95 @@
package com.avaje.ebeaninternal.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
/**
* Created by rob on 5/02/16.
*/
public class IOUtils {
/**
* Reads the entire contents of the specified input stream and returns them
* as a byte array.
*/
public static byte[] read(InputStream in) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
pump(in, buffer);
return buffer.toByteArray();
}
/**
* Reads the entire contents of the specified input stream and returns them
* as an ASCII string.
*/
public static String readAscii(InputStream in) throws IOException {
return EncodeUtil.bytesToAscii(read(in));
}
/**
* Reads the entire contents of the specified input stream and returns them
* as UTF-8 string.
*/
public static String readUtf8(InputStream in) throws IOException {
return EncodeUtil.bytesToUtf8(read(in));
}
/**
* Reads the entire contents of the specified input stream and returns them
* as a string using the encoding supplied.
*/
public static String readEncoded(InputStream in, String encoding) throws IOException {
return EncodeUtil.decodeBytes(read(in), encoding);
}
public static String read(Reader reader) throws IOException {
StringBuilder sb = new StringBuilder();
try {
char[] buffer = new char[4096];
for (; ; ) {
int len = reader.read(buffer);
if (len < 0) {
break;
}
sb.append(buffer, 0, len);
}
} finally {
reader.close();
}
return sb.toString();
}
/**
* Reads data from the specified input stream and copies it to the specified
* output stream, until the input stream is at EOF. Both streams are then
* closed.
*
* @throws IOException if the input or output stream is <code>null</code>
*/
public static void pump(InputStream in, OutputStream out) throws IOException {
if (in == null) throw new IOException("Input stream is null");
if (out == null) throw new IOException("Output stream is null");
try {
try {
byte[] buffer = new byte[4096];
for (; ; ) {
int bytes = in.read(buffer);
if (bytes < 0) {
break;
}
out.write(buffer, 0, bytes);
}
} finally {
in.close();
}
} finally {
out.close();
}
}
}
@@ -0,0 +1,26 @@
package com.avaje.ebeaninternal.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.SQLException;
/**
* Utility for closing raw Jdbc resources.
*/
public class JdbcClose {
private static final Logger logger = LoggerFactory.getLogger(JdbcClose.class);
/**
* Close the connection logging if an error occurs.
*/
public static void close(Connection connection) {
try {
connection.close();
} catch (SQLException e) {
logger.warn("Error closing connection", e);
}
}
}