Skip to content

Commit 20c7c13

Browse files
dieppaosantana85
authored andcommitted
refactor: removed order in file content
1 parent 5d2095d commit 20c7c13

File tree

7 files changed

+92
-24
lines changed

7 files changed

+92
-24
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2025 Flamingock (https://www.flamingock.io)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.flamingock.internal.common.core.preview;
17+
18+
import io.flamingock.internal.common.core.error.FlamingockException;
19+
20+
import java.util.Optional;
21+
import java.util.regex.Matcher;
22+
import java.util.regex.Pattern;
23+
24+
public final class ChangeOrderExtractor {
25+
26+
27+
// For template files: must start with _order__ (e.g., _002__whatever.yaml, _V1_2_3__whatever.yaml)
28+
// Recommended format: _YYYYMMDD_NN__description (e.g., _20250101_01__create_users.yaml)
29+
// Captures ORDER before double underscore separator
30+
private static final Pattern SIMPLE_FILE_ORDER_REGEX_PATTERN = Pattern.compile("^_(.+?)__(.+)$");
31+
32+
// For class names: must have _order__ at the beginning of the class name after package
33+
// (e.g., com.mycompany.mypackage._002__MyChange or com.mycompany.OuterClass$_V1_2_3__InnerChange)
34+
// Captures ORDER before double underscore separator
35+
private static final Pattern FILE_WITH_PACKAGE_ORDER_REGEX_PATTERN = Pattern.compile("[.$]_(.+?)__(.+)$");
36+
37+
private ChangeOrderExtractor() {
38+
}
39+
40+
/**
41+
* For TemplateLoadedChange - validates order from template file name
42+
*/
43+
public static String extractOrderFromFile(String changeId, String fileName) {
44+
return getOrderFromFileName(fileName, false)
45+
.orElseThrow(() -> getFlamingockException(changeId, "fileName", "yaml"));
46+
}
47+
48+
/**
49+
* For CodeLoadedChange - validates order from class name
50+
*/
51+
public static String extractOrderFromClassName(String changeId, String classPath) {
52+
return getOrderFromFileName(classPath, true)
53+
.orElseThrow(() -> getFlamingockException(changeId, "className", "java"));
54+
}
55+
56+
57+
private static Optional<String> getOrderFromFileName(String fileName, boolean withPackage) {
58+
if (fileName == null) {
59+
return Optional.empty();
60+
}
61+
Pattern pattern = withPackage ? FILE_WITH_PACKAGE_ORDER_REGEX_PATTERN : SIMPLE_FILE_ORDER_REGEX_PATTERN;
62+
63+
Matcher matcher = pattern.matcher(fileName);
64+
65+
if (matcher.find()) {
66+
return Optional.ofNullable(matcher.group(1));
67+
}
68+
69+
return Optional.empty();
70+
}
71+
72+
73+
private static FlamingockException getFlamingockException(String changeId, String fileType, String fileExt) {
74+
return new FlamingockException(String.format("Change[%s] : order must be present in the %s(e.g. _0001__%s.%s)",
75+
changeId, fileType, changeId, fileExt));
76+
}
77+
78+
}

core/flamingock-core-commons/src/main/java/io/flamingock/internal/common/core/preview/builder/CodePreviewTaskBuilder.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.flamingock.api.annotations.Recovery;
2121
import io.flamingock.api.annotations.Rollback;
2222
import io.flamingock.api.annotations.TargetSystem;
23+
import io.flamingock.internal.common.core.preview.ChangeOrderExtractor;
2324
import io.flamingock.internal.common.core.preview.CodePreviewChange;
2425
import io.flamingock.internal.common.core.preview.PreviewMethod;
2526
import io.flamingock.internal.common.core.task.RecoveryDescriptor;
@@ -135,11 +136,15 @@ CodePreviewTaskBuilder setTypeElement(TypeElement typeElement) {
135136
Change changeAnnotation = typeElement.getAnnotation(Change.class);
136137
TargetSystem targetSystemAnnotation = typeElement.getAnnotation(TargetSystem.class);
137138
Recovery recoveryAnnotation = typeElement.getAnnotation(Recovery.class);
139+
138140
if(changeAnnotation != null) {
139-
setId(changeAnnotation.id());
140-
setOrder(null);//TODO replace with order from class
141+
String changeId = changeAnnotation.id();
142+
String classPath = typeElement.getQualifiedName().toString();
143+
String order = ChangeOrderExtractor.extractOrderFromClassName(changeId, classPath);
144+
setId(changeId);
145+
setOrder(order);
141146
setAuthor(changeAnnotation.author());
142-
setSourceClassPath(typeElement.getQualifiedName().toString());
147+
setSourceClassPath(classPath);
143148
setExecutionMethod(getAnnotatedMethodInfo(typeElement, Apply.class).orElse(null));
144149
setRollbackMethod(getAnnotatedMethodInfo(typeElement, Rollback.class).orElse(null));
145150
setBeforeExecutionMethod(getAnnotatedMethodInfo(typeElement, BeforeExecution.class).orElse(null));

core/flamingock-core-commons/src/main/java/io/flamingock/internal/common/core/preview/builder/TemplatePreviewTaskBuilder.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.flamingock.internal.common.core.preview.builder;
1717

18+
import io.flamingock.internal.common.core.preview.ChangeOrderExtractor;
1819
import io.flamingock.internal.common.core.template.ChangeTemplateFileContent;
1920
import io.flamingock.internal.common.core.preview.TemplatePreviewChange;
2021
import io.flamingock.internal.common.core.task.RecoveryDescriptor;
@@ -31,7 +32,6 @@ class TemplatePreviewTaskBuilder implements PreviewTaskBuilder<TemplatePreviewCh
3132

3233
private String fileName;
3334
private String id;
34-
private String order;
3535
private String author;
3636
private String templateClassPath;
3737
private String profilesString;
@@ -65,11 +65,6 @@ public TemplatePreviewTaskBuilder setId(String id) {
6565
return this;
6666
}
6767

68-
public TemplatePreviewTaskBuilder setOrder(String order) {
69-
this.order = order;
70-
return this;
71-
}
72-
7368
public TemplatePreviewTaskBuilder setAuthor(String author) {
7469
this.author = author;
7570
return this;
@@ -117,6 +112,8 @@ public void setRecovery(RecoveryDescriptor recovery) {
117112
public TemplatePreviewChange build() {
118113

119114
List<String> profiles = getProfiles();
115+
String order = ChangeOrderExtractor.extractOrderFromFile(id, fileName);
116+
120117
return new TemplatePreviewChange(
121118
fileName,
122119
id,
@@ -147,8 +144,9 @@ private List<String> getProfiles() {
147144

148145

149146
TemplatePreviewTaskBuilder setFromDefinition(ChangeTemplateFileContent templateTaskDescriptor) {
147+
//fileName is set in "setFileName" method
148+
//order is extract from the fileName in the "build" method
150149
setId(templateTaskDescriptor.getId());
151-
setOrder(templateTaskDescriptor.getOrder());
152150
setAuthor(templateTaskDescriptor.getAuthor());
153151
setTemplate(templateTaskDescriptor.getTemplate());
154152
setProfilesString(templateTaskDescriptor.getProfiles());

core/flamingock-core-commons/src/main/java/io/flamingock/internal/common/core/template/ChangeTemplateFileContent.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
public class ChangeTemplateFileContent {
2222
private String id;
23-
private String order;
2423
private String author;
2524
private String template;
2625
private String profiles; //colon-separated list of profiles
@@ -35,7 +34,6 @@ public ChangeTemplateFileContent() {
3534
}
3635

3736
public ChangeTemplateFileContent(String id,
38-
String order,
3937
String author,
4038
String template,
4139
String profiles,
@@ -46,7 +44,6 @@ public ChangeTemplateFileContent(String id,
4644
TargetSystemDescriptor targetSystem,
4745
RecoveryDescriptor recovery) {
4846
this.id = id;
49-
this.order = order;
5047
this.author = author;
5148
this.template = template;
5249
this.profiles = profiles;
@@ -67,14 +64,6 @@ public void setId(String id) {
6764
this.id = id;
6865
}
6966

70-
public String getOrder() {
71-
return order;
72-
}
73-
74-
public void setOrder(String order) {
75-
this.order = order;
76-
}
77-
7867
public String getAuthor() {
7968
return author;
8069
}
@@ -99,7 +88,6 @@ public void setProfiles(String profiles) {
9988
this.profiles = profiles;
10089
}
10190

102-
10391
public Boolean getTransactional() {
10492
return transactional;
10593
}

platform-plugins/flamingock-springboot-integration/src/test/java/io/flamingock/springboot/SpringProfileFilterTemplateTaskTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ private AbstractLoadedTask getTemplateLoadedChange(String profiles) {
109109

110110
ChangeTemplateFileContent changeFileDescriptor = new ChangeTemplateFileContent(
111111
"template-base-change-id",
112-
"1",
113112
"test-author",
114113
TemplateSimulate.class.getSimpleName(),
115114
profiles,
@@ -121,7 +120,7 @@ private AbstractLoadedTask getTemplateLoadedChange(String profiles) {
121120
RecoveryDescriptor.getDefault()
122121
);
123122

124-
TemplatePreviewChange preview = PreviewTaskBuilder.getTemplateBuilder("TemplateBase.yaml", changeFileDescriptor).build();
123+
TemplatePreviewChange preview = PreviewTaskBuilder.getTemplateBuilder("_0001__ChangeUsingTemplate.yaml", changeFileDescriptor).build();
125124

126125
return LoadedTaskBuilder.getInstance(preview).build();
127126

templates/flamingock-mongodb-sync-template/src/test/java/io/flamingock/template/mongodb/changes/_0001_create_users_collections.yaml renamed to templates/flamingock-mongodb-sync-template/src/test/java/io/flamingock/template/mongodb/changes/_0001__create_users_collections.yaml

File renamed without changes.

templates/flamingock-mongodb-sync-template/src/test/java/io/flamingock/template/mongodb/changes/_0002_seed_users.yaml renamed to templates/flamingock-mongodb-sync-template/src/test/java/io/flamingock/template/mongodb/changes/_0002__seed_users.yaml

File renamed without changes.

0 commit comments

Comments
 (0)