mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
DbMigration is also a plugin now
This commit is contained in:
+18
-2
@@ -2,6 +2,7 @@ package io.ebeaninternal.dbmigration;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.plugin.Plugin;
|
||||
import io.ebean.plugin.SpiServer;
|
||||
|
||||
@@ -9,6 +10,9 @@ public class DbMigrationPlugin implements Plugin {
|
||||
|
||||
private DefaultDbMigration dbMigration;
|
||||
|
||||
private static String lastMigration;
|
||||
private static String lastInit;
|
||||
|
||||
@Override
|
||||
public void configure(SpiServer server) {
|
||||
dbMigration = new DefaultDbMigration();
|
||||
@@ -18,11 +22,16 @@ public class DbMigrationPlugin implements Plugin {
|
||||
@Override
|
||||
public void online(boolean online) {
|
||||
try {
|
||||
lastInit = null;
|
||||
lastMigration = null;
|
||||
if (dbMigration.generate) {
|
||||
dbMigration.generateMigration();
|
||||
String tmp = lastMigration = dbMigration.generateMigration();
|
||||
if (tmp == null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (dbMigration.generateInit) {
|
||||
dbMigration.generateInitMigration();
|
||||
lastInit = dbMigration.generateInitMigration();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Error while generating migration");
|
||||
@@ -34,4 +43,11 @@ public class DbMigrationPlugin implements Plugin {
|
||||
dbMigration = null;
|
||||
}
|
||||
|
||||
public static String getLastInit() {
|
||||
return lastInit;
|
||||
}
|
||||
|
||||
public static String getLastMigration() {
|
||||
return lastMigration;
|
||||
}
|
||||
}
|
||||
|
||||
+9
-6
@@ -134,6 +134,7 @@ public class DefaultDbMigration implements DbMigration {
|
||||
protected boolean includeIndex;
|
||||
protected boolean generate = true;
|
||||
protected boolean generateInit = false;
|
||||
private boolean keepLastInit = true;
|
||||
|
||||
/**
|
||||
* Create for offline migration generation.
|
||||
@@ -428,7 +429,7 @@ public class DefaultDbMigration implements DbMigration {
|
||||
for (String pendingDrop : request.getPendingDrops()) {
|
||||
sj.add(generatePendingDrop(request, pendingDrop));
|
||||
}
|
||||
return sj.toString();
|
||||
return sj.length() == 0 ? null : sj.toString();
|
||||
} else if (pendingVersion != null) {
|
||||
return generatePendingDrop(request, pendingVersion);
|
||||
} else {
|
||||
@@ -666,11 +667,8 @@ public class DefaultDbMigration implements DbMigration {
|
||||
logError("migration already exists, not generating DDL");
|
||||
return null;
|
||||
} else {
|
||||
if (request.initMigration) {
|
||||
FIXME: Hier sollten wir dann die vorhandenen löschen
|
||||
}
|
||||
if (!platforms.isEmpty()) {
|
||||
writeExtraPlatformDdl(fullVersion, request.currentModel, dbMigration, request.migrationDir);
|
||||
writeExtraPlatformDdl(fullVersion, request.currentModel, dbMigration, request.migrationDir, request.initMigration && keepLastInit);
|
||||
|
||||
} else if (databasePlatform != null) {
|
||||
// writer needs the current model to provide table/column details for
|
||||
@@ -742,12 +740,17 @@ public class DefaultDbMigration implements DbMigration {
|
||||
/**
|
||||
* Write any extra platform ddl.
|
||||
*/
|
||||
private void writeExtraPlatformDdl(String fullVersion, CurrentModel currentModel, Migration dbMigration, File writePath) throws IOException {
|
||||
private void writeExtraPlatformDdl(String fullVersion, CurrentModel currentModel, Migration dbMigration, File writePath, boolean clear) throws IOException {
|
||||
DdlOptions options = new DdlOptions(addForeignKeySkipCheck);
|
||||
for (Pair pair : platforms) {
|
||||
DdlWrite platformBuffer = new DdlWrite(new MConfiguration(), currentModel.read(), options);
|
||||
PlatformDdlWriter platformWriter = createDdlWriter(pair.platform);
|
||||
File subPath = platformWriter.subPath(writePath, pair.prefix);
|
||||
if (clear) {
|
||||
for (File existing : subPath.listFiles()) {
|
||||
existing.delete();
|
||||
}
|
||||
}
|
||||
platformWriter.processMigration(dbMigration, platformBuffer, subPath, fullVersion);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user