Skip to content

Commit 2651976

Browse files
refactor(sql-template): adapt template to latest templates-core changes (#5)
1 parent 39ef310 commit 2651976

10 files changed

Lines changed: 183 additions & 41 deletions

File tree

.github/workflows/build.yml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
name: Build
22
on: [pull_request]
3-
43
jobs:
54
build:
65
runs-on: ubuntu-latest
76

87
steps:
9-
- name: Checkout flamingock-java (core library)
10-
uses: actions/checkout@v4
11-
with:
12-
repository: flamingock/flamingock-java
13-
path: flamingock-java
14-
ref: develop
15-
168
- name: Checkout project
17-
uses: actions/checkout@v4
18-
with:
19-
path: flamingock-java-template-sql
9+
uses: actions/checkout@v2
2010

2111
- name: Set up Java
2212
uses: graalvm/setup-graalvm@v1
@@ -26,5 +16,6 @@ jobs:
2616
github-token: ${{ secrets.FLAMINGOCK_JRELEASER_GITHUB_TOKEN }}
2717

2818
- name: Unit and Integration tests
29-
working-directory: ./flamingock-java-template-sql
30-
run: ./gradlew clean build --include-build ../flamingock-java
19+
run: |
20+
./gradlew clean build \
21+
-Psql.test.dialects=mysql

build.gradle.kts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ plugins {
88
id("com.diffplug.spotless") version "6.25.0"
99
}
1010

11+
val flamingockVersion = "1.2.0-beta.1"
12+
1113
group = "io.flamingock"
1214
version = "1.0.0-beta.1"
1315

@@ -17,12 +19,14 @@ repositories {
1719
}
1820

1921
dependencies {
20-
implementation("io.flamingock:flamingock-core-commons:1.1.0-rc.2")
21-
implementation("io.flamingock:sql-util:1.1.0-rc.2")
22+
implementation("io.flamingock:flamingock-core-commons:$flamingockVersion")
23+
implementation("io.flamingock:flamingock-template-api:$flamingockVersion")
24+
implementation("io.flamingock:sql-util:$flamingockVersion")
2225
implementation("org.slf4j:slf4j-api:1.7.36")
2326

24-
testAnnotationProcessor("io.flamingock:flamingock-processor:1.1.0-rc.2")
25-
testImplementation("io.flamingock:flamingock-auditstore-sql:1.1.0-rc.2")
27+
testAnnotationProcessor("io.flamingock:flamingock-processor:$flamingockVersion")
28+
testAnnotationProcessor(files(sourceSets.main.get().output))
29+
testImplementation("io.flamingock:flamingock-auditstore-sql:$flamingockVersion")
2630
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
2731
testImplementation("org.mockito:mockito-inline:4.11.0")
2832
testImplementation("com.zaxxer:HikariCP:4.0.3")

src/main/java/io/flamingock/template/sql/SqlTemplate.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
*/
1616
package io.flamingock.template.sql;
1717

18-
import io.flamingock.api.annotations.Apply;
19-
import io.flamingock.api.annotations.Rollback;
18+
import io.flamingock.api.annotations.ApplyTemplate;
19+
import io.flamingock.api.annotations.ChangeTemplate;
20+
import io.flamingock.api.annotations.RollbackTemplate;
2021
import io.flamingock.api.template.AbstractChangeTemplate;
22+
import io.flamingock.api.template.wrappers.TemplateString;
2123
import io.flamingock.internal.util.log.FlamingockLoggerFactory;
2224
import io.flamingock.template.sql.util.SqlSplitterFactory;
2325
import io.flamingock.template.sql.util.SqlStatement;
@@ -30,23 +32,25 @@
3032
import java.util.Collections;
3133
import java.util.List;
3234

33-
public class SqlTemplate extends AbstractChangeTemplate<SqlTemplateConfig, String, String> {
35+
@ChangeTemplate(name = "sql-template", rollbackPayloadRequired = false)
36+
public class SqlTemplate extends AbstractChangeTemplate<SqlTemplateConfig, TemplateString, TemplateString> {
3437

3538
private static final Logger logger = FlamingockLoggerFactory.getLogger(SqlTemplate.class);
3639

3740
public SqlTemplate() {
3841
super();
3942
}
4043

41-
@Apply
44+
@ApplyTemplate
4245
public void apply(Connection connection) {
43-
execute(connection, applyPayload);
46+
execute(connection, applyPayload.getValue());
4447
}
4548

46-
@Rollback
49+
@RollbackTemplate
4750
public void rollback(Connection connection) {
48-
if (rollbackPayload != null && !rollbackPayload.trim().isEmpty()) {
49-
execute(connection, rollbackPayload);
51+
if (rollbackPayload != null && rollbackPayload.getValue() != null
52+
&& !rollbackPayload.getValue().trim().isEmpty()) {
53+
execute(connection, rollbackPayload.getValue());
5054
}
5155
}
5256

src/main/java/io/flamingock/template/sql/SqlTemplateConfig.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@
1515
*/
1616
package io.flamingock.template.sql;
1717

18+
import io.flamingock.api.template.TemplateField;
19+
import io.flamingock.api.template.TemplatePayloadValidationError;
20+
import io.flamingock.api.template.TemplateValidationContext;
21+
22+
import java.util.Collections;
23+
import java.util.List;
24+
1825
/**
1926
* Configuration class for SqlTemplate.
2027
* Allows customization of SQL splitting behavior.
2128
*/
22-
public class SqlTemplateConfig {
29+
public class SqlTemplateConfig implements TemplateField {
2330

2431
/**
2532
* Whether to split the SQL string into multiple statements according to its dialect.
@@ -35,4 +42,9 @@ public void setSplitStatements(boolean splitStatements) {
3542
this.splitStatements = splitStatements;
3643
}
3744

45+
@Override
46+
public List<TemplatePayloadValidationError> validate(TemplateValidationContext context) {
47+
return Collections.emptyList();
48+
}
49+
3850
}

src/test/java/io/flamingock/template/sql/SqlTemplateConfigTest.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434

3535
import javax.sql.DataSource;
3636
import java.sql.Connection;
37+
import java.sql.ResultSet;
3738
import java.sql.Statement;
3839
import java.util.Arrays;
3940
import java.util.Collections;
4041

41-
import static org.junit.jupiter.api.Assertions.assertThrows;
42+
import static org.junit.jupiter.api.Assertions.*;
4243

4344
class SqlTemplateConfigTest {
4445

@@ -81,23 +82,30 @@ static void tearDownAll() {
8182
}
8283

8384
@Test
84-
@DisplayName("WHEN run with splitStatements=false and multiple statements THEN should fail")
85-
void noSplitting() {
85+
@DisplayName("WHEN run with splitStatements=false and multiple statements THEN succeeds (H2 supports multi-statement execute)")
86+
void noSplitting() throws Exception {
8687
try (MockedStatic<Deserializer> mocked = Mockito.mockStatic(Deserializer.class)) {
8788

8889
SqlTemplateConfig config = new SqlTemplateConfig();
8990
config.setSplitStatements(false);
9091

9192
mocked.when(Deserializer::readMetadataFromFile).thenReturn(new FlamingockMetadata(createPipeline(config), null, null));
9293

93-
assertThrows(RuntimeException.class, () -> {
94-
SqlTargetSystem sqlTargetSystem = new SqlTargetSystem("sql", dataSource);
95-
FlamingockFactory.getCommunityBuilder()
96-
.setAuditStore(SqlAuditStore.from(sqlTargetSystem))
97-
.addTargetSystem(sqlTargetSystem)
98-
.build()
99-
.run();
100-
});
94+
SqlTargetSystem sqlTargetSystem = new SqlTargetSystem("sql", dataSource);
95+
FlamingockFactory.getCommunityBuilder()
96+
.setAuditStore(SqlAuditStore.from(sqlTargetSystem))
97+
.addTargetSystem(sqlTargetSystem)
98+
.build()
99+
.run();
100+
101+
// Verify table was created and data inserted (H2 2.x supports multi-statement execute())
102+
try (Connection conn = dataSource.getConnection();
103+
Statement stmt = conn.createStatement();
104+
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + TEST_TABLE)) {
105+
106+
assertTrue(rs.next());
107+
assertEquals(3, rs.getInt(1));
108+
}
101109
}
102110
}
103111

@@ -108,7 +116,7 @@ private PreviewPipeline createPipeline(SqlTemplateConfig config) {
108116
"create-test-users-table",
109117
"0001",
110118
"test-author",
111-
"SqlTemplate",
119+
"sql-template",
112120
Collections.emptyList(),
113121
true,
114122
false,
@@ -126,7 +134,7 @@ private PreviewPipeline createPipeline(SqlTemplateConfig config) {
126134
"insert-test-users",
127135
"0002",
128136
"test-author",
129-
"SqlTemplate",
137+
"sql-template",
130138
Collections.emptyList(),
131139
true,
132140
false,
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2026 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.template.sql;
17+
18+
import com.zaxxer.hikari.HikariConfig;
19+
import com.zaxxer.hikari.HikariDataSource;
20+
import io.flamingock.api.annotations.EnableFlamingock;
21+
import io.flamingock.internal.core.builder.FlamingockFactory;
22+
import io.flamingock.store.sql.SqlAuditStore;
23+
import io.flamingock.targetsystem.sql.SqlTargetSystem;
24+
import org.junit.jupiter.api.AfterAll;
25+
import org.junit.jupiter.api.AfterEach;
26+
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.DisplayName;
28+
import org.junit.jupiter.api.Test;
29+
30+
import javax.sql.DataSource;
31+
import java.sql.Connection;
32+
import java.sql.ResultSet;
33+
import java.sql.Statement;
34+
35+
import static org.junit.jupiter.api.Assertions.assertEquals;
36+
import static org.junit.jupiter.api.Assertions.assertTrue;
37+
38+
@EnableFlamingock(configFile = "flamingock/pipeline.yaml")
39+
class SqlTemplateIntegrationTest {
40+
41+
private static final String TEST_TABLE = "sql_template_test";
42+
43+
private static DataSource dataSource;
44+
45+
@BeforeAll
46+
static void beforeAll() {
47+
HikariConfig config = new HikariConfig();
48+
config.setJdbcUrl("jdbc:h2:mem:integrationdb;DB_CLOSE_DELAY=-1");
49+
config.setUsername("sa");
50+
config.setPassword("");
51+
config.setDriverClassName("org.h2.Driver");
52+
53+
dataSource = new HikariDataSource(config);
54+
}
55+
56+
@AfterEach
57+
void tearDown() {
58+
if (dataSource != null) {
59+
try (Connection conn = dataSource.getConnection();
60+
Statement stmt = conn.createStatement()) {
61+
stmt.execute("DROP TABLE IF EXISTS " + TEST_TABLE);
62+
stmt.execute("DROP TABLE IF EXISTS flamingockAuditLog");
63+
stmt.execute("DROP TABLE IF EXISTS flamingockLock");
64+
} catch (Exception e) {
65+
// Ignore cleanup errors
66+
}
67+
}
68+
}
69+
70+
@AfterAll
71+
static void tearDownAll() {
72+
if (dataSource instanceof HikariDataSource) {
73+
((HikariDataSource) dataSource).close();
74+
}
75+
}
76+
77+
@Test
78+
@DisplayName("WHEN running YAML-based SQL template changes THEN all changes are applied")
79+
void happyPath() throws Exception {
80+
SqlTargetSystem sqlTargetSystem = new SqlTargetSystem("sql", dataSource);
81+
FlamingockFactory.getCommunityBuilder()
82+
.setAuditStore(SqlAuditStore.from(sqlTargetSystem))
83+
.addTargetSystem(sqlTargetSystem)
84+
.build()
85+
.run();
86+
87+
try (Connection conn = dataSource.getConnection();
88+
Statement stmt = conn.createStatement();
89+
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + TEST_TABLE)) {
90+
91+
assertTrue(rs.next());
92+
assertEquals(2, rs.getInt(1));
93+
}
94+
95+
try (Connection conn = dataSource.getConnection();
96+
Statement stmt = conn.createStatement();
97+
ResultSet rs = stmt.executeQuery("SELECT name FROM " + TEST_TABLE + " ORDER BY id")) {
98+
99+
assertTrue(rs.next());
100+
assertEquals("Admin", rs.getString(1));
101+
assertTrue(rs.next());
102+
assertEquals("Backup", rs.getString(1));
103+
}
104+
}
105+
}

src/test/java/io/flamingock/template/sql/SqlTemplateTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private PreviewPipeline createPipeline(Boolean apply, Boolean rollback) {
197197
"create-test-users-table",
198198
"0001",
199199
"test-author",
200-
"SqlTemplate",
200+
"sql-template",
201201
Collections.emptyList(),
202202
true,
203203
false,
@@ -215,7 +215,7 @@ private PreviewPipeline createPipeline(Boolean apply, Boolean rollback) {
215215
"insert-test-users",
216216
"0002",
217217
"test-author",
218-
"SqlTemplate",
218+
"sql-template",
219219
Collections.emptyList(),
220220
true,
221221
false,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
id: create-test-table
2+
transactional: true
3+
template: sql-template
4+
targetSystem:
5+
id: "sql"
6+
apply: "CREATE TABLE sql_template_test (id INT PRIMARY KEY, name VARCHAR(100), role VARCHAR(50))"
7+
rollback: "DROP TABLE sql_template_test"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
id: insert-test-data
2+
transactional: true
3+
template: sql-template
4+
targetSystem:
5+
id: "sql"
6+
apply: "INSERT INTO sql_template_test (id, name, role) VALUES (1, 'Admin', 'superuser'); INSERT INTO sql_template_test (id, name, role) VALUES (2, 'Backup', 'readonly')"
7+
rollback: "DELETE FROM sql_template_test WHERE id IN (1, 2)"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pipeline:
2+
stages:
3+
- description: "SQL template integration changes"
4+
location: "io.flamingock.template.sql.changes"

0 commit comments

Comments
 (0)