cleanup
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
package jef.main;
|
||||
|
||||
import jef.Database;
|
||||
import jef.model.DbContext;
|
||||
import jef.model.DbContextOptions;
|
||||
import jef.model.ModelBuilder;
|
||||
import jef.model.migration.creator.MigrationCreator;
|
||||
import jef.mysql.MysqlDatabase;
|
||||
import jef.platform.nul.NullPlatform;
|
||||
import jef.util.Util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@@ -18,16 +16,15 @@ import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.OpenOption;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
@@ -69,6 +66,7 @@ public class MigrationCommandHandler {
|
||||
String migrationPackage = null;
|
||||
String targetFolder = null;
|
||||
String name = null;
|
||||
String platformType = null;
|
||||
var classPath = new ArrayList<String>();
|
||||
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
@@ -93,6 +91,13 @@ public class MigrationCommandHandler {
|
||||
}
|
||||
targetFolder = args[++i];
|
||||
break;
|
||||
case "--platform":
|
||||
case "-p":
|
||||
if (i + 1 >= args.length) {
|
||||
Main.printHelp();
|
||||
}
|
||||
platformType = args[++i];
|
||||
break;
|
||||
default:
|
||||
if (name != null) {
|
||||
Main.printHelp();
|
||||
@@ -119,6 +124,7 @@ public class MigrationCommandHandler {
|
||||
var targetFolderFile = new File(targetFolder);
|
||||
targetFolderFile.mkdirs();
|
||||
migrationPackage = findMigrationPackageName(targetFolder);
|
||||
var sqlPlatform = new NullPlatform();//TODO find out from migrations or -p param
|
||||
|
||||
//find data
|
||||
var currentSnapshotFile = new File(targetFolderFile, "CurrentSnapshot.java");
|
||||
@@ -132,12 +138,13 @@ public class MigrationCommandHandler {
|
||||
var date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
|
||||
var className = "M" + date + "_" + name;
|
||||
|
||||
var creator = new MigrationCreator();
|
||||
var result = creator.createMigration(from, to, className, migrationPackage, currentSnapshotFile.isFile() ? Files.readString(currentSnapshotFile.toPath()) : null);
|
||||
var creator = new MigrationCreator(sqlPlatform, from, to, className, migrationPackage, currentSnapshotFile.isFile() ? Files.readString(currentSnapshotFile.toPath()) : null);
|
||||
var result = creator.createMigration();
|
||||
|
||||
Files.writeString(new File(targetFolder, className + ".java").toPath(), result.getMigration(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
|
||||
Files.writeString(new File(targetFolder, className + "Snapshot.java").toPath(), result.getMigrationSnapshot(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
|
||||
Files.writeString(new File(targetFolder, "CurrentSnapshot.java").toPath(), result.getCurrentSnapshot(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
|
||||
var fileOptions = new OpenOption[]{StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE};
|
||||
Files.writeString(new File(targetFolder, result.getMigrationClassName() + ".java").toPath(), result.getMigration(), fileOptions);
|
||||
Files.writeString(new File(targetFolder, result.getMigrationSnapshotClassName() + ".java").toPath(), result.getMigrationSnapshot(), fileOptions);
|
||||
Files.writeString(new File(targetFolder, result.getCurrentSnapshotClassName() + ".java").toPath(), result.getCurrentSnapshot(), fileOptions);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
@@ -147,35 +154,13 @@ public class MigrationCommandHandler {
|
||||
|
||||
private static Optional<DbContext> findContextByName(URLClassLoader cl, String contextName) {
|
||||
Util.ThrowableFunction<String, DbContext> finder = (String entryName) -> {
|
||||
var cls = cl.loadClass(entryName.substring(0, entryName.length() - ".class".length()).replace("/", "."));
|
||||
var parent = cls;
|
||||
// var interfaces = cls.getInterfaces();
|
||||
// while (parent != Object.class && !Set.of(interfaces).contains(DbContext.class)) {
|
||||
var cls = (Class<? extends DbContext>) cl.loadClass(entryName.substring(0, entryName.length() - ".class".length()).replace("/", "."));
|
||||
var parent = (Class<?>) cls;
|
||||
while (parent != Object.class && parent != DbContext.class) {
|
||||
parent = parent.getSuperclass();
|
||||
// interfaces = cls.getInterfaces();
|
||||
}
|
||||
// if (Set.of(interfaces).contains(DbContext.class)) {
|
||||
if (parent == DbContext.class) {
|
||||
var paramInitializer = new HashMap<Class<?>, Supplier<?>>();
|
||||
paramInitializer.put(Database.class, () -> new MysqlDatabase(null, null));
|
||||
paramInitializer.put(DbContextOptions.class, () -> new DbContextOptions(null));
|
||||
|
||||
var ctors = cls.getDeclaredConstructors();//TODO create a function for initializing db contexts
|
||||
if (ctors.length == 0) {
|
||||
System.err.println("No constructor found in " + cls.getName());
|
||||
return null;
|
||||
}
|
||||
|
||||
var ctor = Arrays.stream(ctors)
|
||||
.filter(e -> Arrays.stream(e.getParameterTypes())
|
||||
.allMatch(p -> Database.class.isAssignableFrom(p) || DbContextOptions.class.isAssignableFrom(p)))
|
||||
.findFirst().orElse(null);
|
||||
if (ctor == null) {
|
||||
return null;
|
||||
}
|
||||
var args = Arrays.stream(ctor.getParameterTypes()).map(p -> paramInitializer.get(p).get()).toArray();
|
||||
return Util.tryGet(() -> (DbContext) ctor.newInstance(args)).orElse(null);
|
||||
return Util.tryGet(() -> cls.getDeclaredConstructor().newInstance()).orElse(null);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user