Skip to content

Commit

Permalink
feat: Adds convenience overload method SongodaPlugin#initDatabase
Browse files Browse the repository at this point in the history
Takes DataMigration... instead of an List. Similar how it used to be in the old API.
Reduces visual clutter in plugin code in my opinion
  • Loading branch information
SpraxDev committed Oct 24, 2023
1 parent eaf96d5 commit c79b835
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions Core/src/main/java/com/craftaro/core/SongodaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.sql.Connection;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
Expand Down Expand Up @@ -255,7 +256,7 @@ public Config getDatabaseConfig() {
* @return DataManager for this plugin.
*/
public DataManager getDataManager() {
return dataManager;
return this.dataManager;
}

/**
Expand All @@ -265,6 +266,10 @@ protected void initDatabase() {
initDatabase(Collections.emptyList());
}

protected void initDatabase(DataMigration... migrations) {
initDatabase(Arrays.asList(migrations));
}

/**
* Initialize the DataManager for this plugin and convert from SQLite to H2 if needed.
*
Expand All @@ -281,18 +286,19 @@ protected void initDatabase(List<DataMigration> migrations) {
this.dataManager = new DataManager(this, migrations);
}

if (dataManager.getDatabaseConnector().isInitialized()) {
if (this.dataManager.getDatabaseConnector().isInitialized()) {
//Check if the type is SQLite
if (dataManager.getDatabaseConnector().getType() == DatabaseType.SQLITE) {
if (this.dataManager.getDatabaseConnector().getType() == DatabaseType.SQLITE) {
//Let's convert it to H2
try {
DataManager newDataManager = DataMigration.convert(this, DatabaseType.H2);
if (newDataManager != null && newDataManager.getDatabaseConnector().isInitialized()) {
//Set the new data manager
setDataManager(newDataManager);
}
} catch (Exception e) {
e.printStackTrace();
} catch (Exception ex) {
// Throwing for keeping backwards compatible – Not a fan of just logging a potential critical error here
throw new RuntimeException(ex);
}
}
}
Expand All @@ -305,10 +311,10 @@ protected void initDatabase(List<DataMigration> migrations) {
public void setDataManager(DataManager dataManager) {
if (dataManager == null) throw new IllegalArgumentException("DataManager cannot be null!");
if (this.dataManager == dataManager) return;
//Make sure to shut down the old data manager.

// Make sure to shut down the old data manager.
if (this.dataManager != null) {
this.dataManager.shutdown();
this.dataManager = null;
}
this.dataManager = dataManager;
}
Expand Down

0 comments on commit c79b835

Please sign in to comment.