Skip to content

Commit 19cf08d

Browse files
committed
fix: reverted migration for config
1 parent f88da4f commit 19cf08d

7 files changed

Lines changed: 168 additions & 252 deletions

File tree

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
package dev.objz.commandbridge.scripting.migration;
22

33
import java.util.List;
4-
import java.util.Map;
54

65
/**
76
* Represents a single version-to-version migration step loaded from a
87
* resource file (e.g. {@code migrations/2-3.yml}).
9-
* <p>
10-
* Rules are grouped by section {@code "scripts"}, {@code "configs"}.
118
*
12-
* @param from the source version
13-
* @param to the target version
14-
* @param sections map of section name to its list of migration rules
9+
* @param from the source version
10+
* @param to the target version
11+
* @param rules the list of rules to apply
1512
*/
16-
public record MigrationStep(int from, int to, Map<String, List<MigrationRule>> sections) {
13+
public record MigrationStep(int from, int to, List<MigrationRule> rules) {
1714

1815
public MigrationStep {
19-
sections = Map.copyOf(sections);
20-
}
21-
22-
public List<MigrationRule> rules(String section) {
23-
return sections.getOrDefault(section, List.of());
16+
rules = List.copyOf(rules);
2417
}
2518
}

core/src/main/java/dev/objz/commandbridge/scripting/migration/YamlMigrator.java

Lines changed: 18 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,9 @@
1616
* merges chains of rules into a single pass, and applies the merged rules
1717
* directly
1818
* on raw YAML text lines to preserve comments, formatting, and layout.
19-
* <p>
20-
* Migration files contain sections (e.g. {@code scripts}, {@code configs})
21-
* each with their own list of rules. When migrating, a specific section
22-
* is selected so only the relevant rules are applied.
2319
*/
2420
public final class YamlMigrator {
2521

26-
/** Well-known section name for script migrations. */
27-
public static final String SECTION_SCRIPTS = "scripts";
28-
29-
/** Well-known section name for config migrations. */
30-
public static final String SECTION_CONFIGS = "configs";
31-
3222
private static final Pattern VERSION_LINE = Pattern.compile("(version:\\s*)\\d+");
3323
private static final Pattern WILDCARD_PATTERN = Pattern.compile("^(.+?)\\[\\*]$");
3424

@@ -63,53 +53,29 @@ public int detectVersion(String yaml) {
6353
return -1;
6454
}
6555

66-
/**
67-
* @param yaml the raw YAML content
68-
* @param section the migration section to use
69-
* @return the result containing the migrated YAML or an error
70-
*/
71-
public MigrationResult migrate(String yaml, String section) {
72-
return migrate(yaml, section, -1);
73-
}
74-
75-
/**
76-
* Migrates a raw YAML string from its detected version to the current
77-
* version, using rules from the given section.
78-
*
79-
* @param yaml the raw YAML content
80-
* @param section the migration section to use (e.g. "scripts" or
81-
* "configs")
82-
* @param defaultVersion the version to assume if no {@code version:} field is
83-
* found,
84-
* or {@code -1} to require an explicit version field
85-
* @return the result containing the migrated YAML or an error
86-
*/
87-
public MigrationResult migrate(String yaml, String section, int defaultVersion) {
56+
public MigrationResult migrate(String yaml) {
8857
int from = detectVersion(yaml);
8958
if (from < 0) {
90-
if (defaultVersion > 0) {
91-
from = defaultVersion;
92-
} else {
93-
return MigrationResult.error("Could not detect version");
94-
}
59+
return MigrationResult.error("Could not detect script version");
9560
}
9661
if (from == currentVersion) {
9762
return MigrationResult.skip(from);
9863
}
9964
if (from > currentVersion) {
10065
return MigrationResult.error(String.format(
101-
"Version %d is newer than the current version %d", from, currentVersion));
66+
"Script version %d is newer than the current version %d", from,
67+
currentVersion));
10268
}
103-
return migrate(yaml, section, from, currentVersion);
69+
return migrate(yaml, from, currentVersion);
10470
}
10571

10672
/**
10773
* Migrates a raw YAML string from {@code from} to {@code to} by applying
108-
* merged rules for the given section directly on text lines, preserving
109-
* all comments, blank lines, and formatting.
74+
* merged rules directly on text lines, preserving all comments, blank
75+
* lines, and formatting.
11076
*/
111-
public MigrationResult migrate(String yaml, String section, int from, int to) {
112-
List<MigrationRule> merged = mergeRules(from, to, section);
77+
public MigrationResult migrate(String yaml, int from, int to) {
78+
List<MigrationRule> merged = mergeRules(from, to);
11379
List<String> lines = new ArrayList<>(Arrays.asList(yaml.split("\n", -1)));
11480

11581
List<TargetAction> modifyActions = new ArrayList<>();
@@ -461,7 +427,7 @@ private record TargetAction(MigrationRule rule, int start, int end) {
461427
private record InsertAction(int lineIndex, String content) {
462428
}
463429

464-
List<MigrationRule> mergeRules(int from, int to, String section) {
430+
List<MigrationRule> mergeRules(int from, int to) {
465431
Map<String, String> pathMap = new LinkedHashMap<>();
466432
Map<String, MigrationRule> addedRules = new LinkedHashMap<>();
467433
List<MigrationRule> effective = new ArrayList<>();
@@ -472,7 +438,7 @@ List<MigrationRule> mergeRules(int from, int to, String section) {
472438
continue;
473439
}
474440

475-
for (MigrationRule rule : step.rules(section)) {
441+
for (MigrationRule rule : step.rules()) {
476442
switch (rule.type()) {
477443
case REMOVE -> {
478444
String resolvedPath = resolveCurrentPath(pathMap, rule.path());
@@ -579,32 +545,16 @@ private void loadSteps() {
579545
continue;
580546
}
581547

582-
Map<String, List<MigrationRule>> sections = new LinkedHashMap<>();
583-
584-
for (var entry : map.entrySet()) {
585-
String key = Objects.toString(entry.getKey(), "");
586-
Object value = entry.getValue();
587-
588-
if (!(value instanceof List<?> ruleList)) {
589-
continue;
590-
}
591-
592-
List<MigrationRule> rules = parseRules(ruleList, filename);
593-
if (!rules.isEmpty()) {
594-
sections.put(key, List.copyOf(rules));
595-
}
548+
Object rulesObj = map.get("rules");
549+
List<MigrationRule> rules = new ArrayList<>();
550+
if (rulesObj instanceof List<?> ruleList) {
551+
rules.addAll(parseRules(ruleList, filename));
596552
}
597553

598-
if (sections.isEmpty()) {
599-
continue;
600-
}
601-
602-
MigrationStep step = new MigrationStep(from, from + 1, sections);
554+
MigrationStep step = new MigrationStep(from, from + 1, rules);
603555
steps.put(from, step);
604-
605-
int totalRules = sections.values().stream().mapToInt(List::size).sum();
606-
Log.debug("Loaded migration step {} -> {} with {} rule(s) across {} section(s)",
607-
from, from + 1, totalRules, sections.size());
556+
Log.debug("Loaded migration step {} -> {} with {} rule(s)",
557+
from, from + 1, rules.size());
608558

609559
} catch (Exception e) {
610560
Log.warn("Failed to load migration file '{}': {}", filename, e.getMessage());

core/src/main/resources/migrations/1-2.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

core/src/main/resources/migrations/2-3.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
scripts:
1+
rules:
22
- type: REMOVE
33
path: defaults.server.timeout
44
- type: REMOVE

core/src/main/resources/migrations/3-4.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
scripts:
1+
rules:
22
- type: ADD
33
path: defaults.server.player-arg
44
value: '""'

velocity/src/main/java/dev/objz/commandbridge/velocity/cli/CBCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void register() {
6969
var ping = new PingCommand(sessionHub, outNode, config);
7070
var debug = new DebugCommand();
7171
var dump = new DumpCommand(registrationManager, sessionHub, outNode, scriptManager, config, dataDir);
72-
var migrate = new MigrateCommand(scriptManager.scriptsDir(), dataDir);
72+
var migrate = new MigrateCommand(scriptManager.scriptsDir());
7373
var infoCmd = new InfoCommand();
7474

7575
new CommandAPICommand("commandbridge")

0 commit comments

Comments
 (0)