-
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GH-475 Add config migrations #475
Changes from all commits
2884381
2d6c337
a0dd948
653eea6
da88a46
403750d
c273de9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.eternalcode.core.configuration.migration; | ||
|
||
public abstract class AbstractMigration implements Migration { | ||
|
||
private final String description; | ||
private final MigrationStep[] steps; | ||
|
||
public AbstractMigration(String description, MigrationStep... steps) { | ||
this.description = description; | ||
this.steps = steps; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return this.description; | ||
} | ||
|
||
@Override | ||
public MigrationStep[] getSteps() { | ||
return this.steps; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.eternalcode.core.configuration.migration; | ||
|
||
public class MigrateException extends RuntimeException { | ||
|
||
public MigrateException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.eternalcode.core.configuration.migration; | ||
|
||
import com.eternalcode.core.configuration.ConfigurationManager; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
public class MigrateFactory { | ||
|
||
private final List<Migration> migrations; | ||
private final MigrateService migrationService; | ||
|
||
private MigrateFactory(Plugin plugin, ConfigurationManager configurationManager) { | ||
this.migrations = new ArrayList<>(); | ||
this.migrationService = new MigrateService(plugin, configurationManager); | ||
} | ||
|
||
public static MigrateFactory create(Plugin plugin, ConfigurationManager configurationManager) { | ||
return new MigrateFactory(plugin, configurationManager); | ||
} | ||
|
||
public MigrateFactory withMigration(Migration migration) { | ||
this.migrations.add(migration); | ||
return this; | ||
} | ||
|
||
public void migrate() { | ||
this.migrations.sort(Comparator.comparingInt(Migration::migrationNumber)); | ||
|
||
for (Migration migration : this.migrations) { | ||
this.migrationService.migrateSingleMigration(migration); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.eternalcode.core.configuration.migration; | ||
|
||
import com.eternalcode.core.configuration.ConfigurationManager; | ||
import com.eternalcode.core.configuration.implementation.PluginConfiguration; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
import org.bukkit.configuration.file.YamlConfiguration; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
|
||
public class MigrateService { | ||
|
||
private final Plugin plugin; | ||
private final ConfigurationManager configurationManager; | ||
|
||
public MigrateService(Plugin plugin, ConfigurationManager configurationManager) { | ||
this.plugin = plugin; | ||
this.configurationManager = configurationManager; | ||
} | ||
|
||
public void migrateSingleMigration(Migration migration) { | ||
Path filePath = Path.of(this.plugin.getDataFolder() + File.separator + migration.file()); | ||
MigrationStep[] steps = migration.getSteps(); | ||
|
||
try { | ||
YamlConfiguration config = YamlConfiguration.loadConfiguration(filePath.toFile()); | ||
|
||
for (MigrationStep step : steps) { | ||
this.updateYamlData(config, step.getOldKey(), step.getNewKey()); | ||
} | ||
|
||
this.plugin.getLogger().info(String.format("Applying migration: %s", migration.getDescription())); | ||
|
||
this.configurationManager.save(new PluginConfiguration()); | ||
|
||
this.plugin.getLogger().info("Successfully saved configuration after migrations"); | ||
} | ||
catch (Exception exception) { | ||
throw new MigrateException("An error occurred while migrating the configuration file", exception); | ||
} | ||
} | ||
|
||
private void updateYamlData(ConfigurationSection section, String oldKey, String newKey) { | ||
if (section.contains(oldKey)) { | ||
Object value = section.get(oldKey); | ||
|
||
section.set(oldKey, null); | ||
section.set(newKey, value); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.eternalcode.core.configuration.migration; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
|
||
public interface Migration { | ||
int migrationNumber(); | ||
|
||
String file(); | ||
|
||
String getDescription(); | ||
|
||
MigrationStep[] getSteps(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.eternalcode.core.configuration.migration; | ||
|
||
public class MigrationStep { | ||
|
||
private final String oldKey; | ||
private final String newKey; | ||
|
||
public MigrationStep(String oldKey, String newKey) { | ||
this.oldKey = oldKey; | ||
this.newKey = newKey; | ||
} | ||
|
||
public String getOldKey() { | ||
return this.oldKey; | ||
} | ||
|
||
public String getNewKey() { | ||
return this.newKey; | ||
} | ||
|
||
public static MigrationStep move(String oldKey, String newKey) { | ||
return new MigrationStep(oldKey, newKey); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.eternalcode.core.configuration.migration.impl; | ||
|
||
import com.eternalcode.core.configuration.migration.AbstractMigration; | ||
|
||
import static com.eternalcode.core.configuration.migration.MigrationStep.move; | ||
|
||
public class PC1 extends AbstractMigration { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure that this name isn’t too generic? We should use something more understandable. |
||
|
||
public PC1() { | ||
super("Cosmetic change for number of lines to clear.", | ||
move("chat.linesToClear", "chat.numberOfLinesToClear")); | ||
} | ||
|
||
@Override | ||
public int migrationNumber() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can base order of migrations on list order. #475 (comment) |
||
return 1; | ||
} | ||
|
||
@Override | ||
public String file() { | ||
return "config.yml"; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean this list ^