Skip to content
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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,4 @@ EternalCore uses various dependencies for its functionality, including:

[<img src="https://user-images.githubusercontent.com/65517973/210912946-447a6b9a-2685-4796-9482-a44bffc727ce.png" alt="JetBrains" width="150">](https://www.jetbrains.com)

We extend our gratitude to JetBrains for providing [Open Source Licenses](https://www.jetbrains.com/opensource/) for their outstanding tools. We recommend using [IntelliJ IDEA](https://www.jetbrains.com/idea/) to work with our projects and boost your productivity!


We extend our gratitude to JetBrains for providing [Open Source Licenses](https://www.jetbrains.com/opensource/) for their outstanding tools. We recommend using [IntelliJ IDEA](https://www.jetbrains.com/idea/) to work with our projects and boost your productivity!
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.eternalcode.core.configuration.implementation.LocationsConfiguration;
import com.eternalcode.core.configuration.implementation.PlaceholdersConfiguration;
import com.eternalcode.core.configuration.implementation.PluginConfiguration;
import com.eternalcode.core.configuration.migration.MigrateFactory;
import com.eternalcode.core.configuration.migration.impl.PC1;
import com.eternalcode.core.database.DatabaseManager;
import com.eternalcode.core.database.NoneRepository;
import com.eternalcode.core.database.wrapper.AutoMessageRepositoryOrmLite;
Expand Down Expand Up @@ -243,6 +245,12 @@ public EternalCore(Plugin plugin) {
ConfigurationBackupService configurationBackupService = new ConfigurationBackupService(plugin.getDataFolder());
this.configurationManager = new ConfigurationManager(configurationBackupService, plugin.getDataFolder());

MigrateFactory.create(plugin, this.configurationManager)
.withMigration(
new PC1()
)
.migrate();

PluginConfiguration pluginConfiguration = this.configurationManager.load(new PluginConfiguration());
LocationsConfiguration locationsConfiguration = this.configurationManager.load(new LocationsConfiguration());
LanguageConfiguration languageConfiguration = this.configurationManager.load(new LanguageConfiguration());
Expand Down Expand Up @@ -434,7 +442,7 @@ public EternalCore(Plugin plugin) {
new IgnoreCommand(ignoreRepository, this.noticeService),
new UnIgnoreCommand(ignoreRepository, this.noticeService),

ChatManagerCommand.create(this.chatManager, this.noticeService, pluginConfiguration.chat.linesToClear)
ChatManagerCommand.create(this.chatManager, this.noticeService, pluginConfiguration.chat.numberOfLinesToClear)
)
.commandGlobalEditor(new CommandConfigurator(commandConfiguration))
.commandEditor(GameModeCommand.class, new GameModeConfigurator(commandConfiguration))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public <T extends ReloadableConfig> void save(T config) {
.orThrow(RuntimeException::new);
}

public void saveAll() {
for (ReloadableConfig config : this.configs) {
this.save(config);
}
}

public void reload() {
this.configurationBackupService.createBackup();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static class Chat implements ChatSettings {
public Duration chatDelay = Duration.ofSeconds(5);

@Description({ " ", "# Number of lines that will be cleared when using the /chat clear command" })
public int linesToClear = 128;
public int numberOfLinesToClear = 128;

public boolean chatEnabled = true;

Expand Down
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean this list ^

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 {
Copy link
Member

Choose a reason for hiding this comment

The 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() {
Copy link
Member

Choose a reason for hiding this comment

The 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";
}
}