Skip to content

Commit f857359

Browse files
committed
Add support for mariadb
resolves #833 PagingProvider and integration tests added
1 parent be2be98 commit f857359

File tree

10 files changed

+526
-11
lines changed

10 files changed

+526
-11
lines changed

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@
120120
</maven-checkstyle-plugin.includeTestSourceDirectory>
121121
<java.version>17</java.version>
122122
<spring-javaformat-maven-plugin.version>0.0.34</spring-javaformat-maven-plugin.version>
123+
<test.containers.version>1.17.5</test.containers.version>
124+
<test.ducttape.version>1.0.8</test.ducttape.version>
123125
</properties>
124126

125127
<build>

spring-cloud-task-core/pom.xml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<dependency>
6060
<groupId>org.springframework.batch</groupId>
6161
<artifactId>spring-batch-infrastructure</artifactId>
62+
<version>5.0.0-SNAPSHOT</version>
6263
</dependency>
6364
<dependency>
6465
<groupId>org.springframework.data</groupId>
@@ -164,6 +165,35 @@
164165
<artifactId>zipkin-sender-urlconnection</artifactId>
165166
<scope>test</scope>
166167
</dependency>
167-
</dependencies>
168+
<dependency>
169+
<groupId>org.testcontainers</groupId>
170+
<artifactId>testcontainers</artifactId>
171+
<version>${test.containers.version}</version>
172+
<scope>test</scope>
173+
</dependency>
174+
<dependency>
175+
<groupId>org.testcontainers</groupId>
176+
<artifactId>junit-jupiter</artifactId>
177+
<version>${test.containers.version}</version>
178+
<scope>test</scope>
179+
</dependency>
180+
<dependency>
181+
<groupId>org.testcontainers</groupId>
182+
<artifactId>mariadb</artifactId>
183+
<version>${test.containers.version}</version>
184+
<scope>test</scope>
185+
</dependency>
186+
<dependency>
187+
<groupId>org.rnorth.duct-tape</groupId>
188+
<artifactId>duct-tape</artifactId>
189+
<version>${test.ducttape.version}</version>
190+
<scope>test</scope>
191+
</dependency>
192+
<dependency>
193+
<groupId>org.mariadb.jdbc</groupId>
194+
<artifactId>mariadb-java-client</artifactId>
195+
<scope>test</scope>
196+
</dependency>
197+
</dependencies>
168198

169199
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2022-2022 the original author or authors.
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+
* https://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+
17+
package org.springframework.cloud.task.repository.database.support;
18+
19+
import org.springframework.cloud.task.repository.database.PagingQueryProvider;
20+
import org.springframework.data.domain.Pageable;
21+
22+
/**
23+
* MariaDB implementation of a {@link PagingQueryProvider} using database specific
24+
* features.
25+
*
26+
* @author Glenn Renfro
27+
*/
28+
public class MariaDbPagingQueryProvider extends AbstractSqlPagingQueryProvider {
29+
30+
@Override
31+
public String getPageQuery(Pageable pageable) {
32+
String topClause = new StringBuilder().append("LIMIT ").append(pageable.getOffset()).append(", ")
33+
.append(pageable.getPageSize()).toString();
34+
return SqlPagingQueryUtils.generateLimitJumpToQuery(this, topClause);
35+
}
36+
37+
}

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/database/support/SqlPagingQueryProviderFactoryBean.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static org.springframework.cloud.task.repository.support.DatabaseType.DB2ZOS;
3636
import static org.springframework.cloud.task.repository.support.DatabaseType.H2;
3737
import static org.springframework.cloud.task.repository.support.DatabaseType.HSQL;
38+
import static org.springframework.cloud.task.repository.support.DatabaseType.MARIADB;
3839
import static org.springframework.cloud.task.repository.support.DatabaseType.MYSQL;
3940
import static org.springframework.cloud.task.repository.support.DatabaseType.ORACLE;
4041
import static org.springframework.cloud.task.repository.support.DatabaseType.POSTGRES;
@@ -67,6 +68,7 @@ public class SqlPagingQueryProviderFactoryBean implements FactoryBean<PagingQuer
6768
this.providers.put(HSQL, new HsqlPagingQueryProvider());
6869
this.providers.put(H2, new H2PagingQueryProvider());
6970
this.providers.put(MYSQL, new MySqlPagingQueryProvider());
71+
this.providers.put(MARIADB, new MariaDbPagingQueryProvider());
7072
this.providers.put(POSTGRES, new PostgresPagingQueryProvider());
7173
this.providers.put(ORACLE, new OraclePagingQueryProvider());
7274
this.providers.put(SQLSERVER, new SqlServerPagingQueryProvider());

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/DatabaseType.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public enum DatabaseType {
5757
*/
5858
MYSQL("MySQL"),
5959

60+
/**
61+
* MySQL DB.
62+
*/
63+
MARIADB("MariaDB"),
64+
6065
/**
6166
* PostgreSQL DB.
6267
*/
@@ -145,7 +150,9 @@ else if (databaseProductName.indexOf("AS") != -1
145150
}
146151
}
147152
else {
148-
databaseProductName = JdbcUtils.commonDatabaseName(databaseProductName);
153+
if (!databaseProductName.equals(MARIADB.getProductName())) {
154+
databaseProductName = JdbcUtils.commonDatabaseName(databaseProductName);
155+
}
149156
}
150157
return fromProductName(databaseProductName);
151158
}
@@ -157,9 +164,6 @@ else if (databaseProductName.indexOf("AS") != -1
157164
* @throws IllegalArgumentException if none is found.
158165
*/
159166
public static DatabaseType fromProductName(String productName) {
160-
if (productName.equals("MariaDB")) {
161-
productName = "MySQL";
162-
}
163167
if (!dbNameMap.containsKey(productName)) {
164168
throw new IllegalArgumentException("DatabaseType not found for product name: [" + productName + "]");
165169
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
CREATE TABLE TASK_EXECUTION (
3+
TASK_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY ,
4+
START_TIME DATETIME(6) DEFAULT NULL ,
5+
END_TIME DATETIME(6) DEFAULT NULL ,
6+
TASK_NAME VARCHAR(100) ,
7+
EXIT_CODE INTEGER ,
8+
EXIT_MESSAGE VARCHAR(2500) ,
9+
ERROR_MESSAGE VARCHAR(2500) ,
10+
LAST_UPDATED TIMESTAMP,
11+
EXTERNAL_EXECUTION_ID VARCHAR(255),
12+
PARENT_EXECUTION_ID BIGINT
13+
) ENGINE=InnoDB;
14+
15+
CREATE TABLE TASK_EXECUTION_PARAMS (
16+
TASK_EXECUTION_ID BIGINT NOT NULL ,
17+
TASK_PARAM VARCHAR(2500) ,
18+
constraint TASK_EXEC_PARAMS_FK foreign key (TASK_EXECUTION_ID)
19+
references TASK_EXECUTION(TASK_EXECUTION_ID)
20+
) ENGINE=InnoDB;
21+
22+
CREATE TABLE TASK_TASK_BATCH (
23+
TASK_EXECUTION_ID BIGINT NOT NULL ,
24+
JOB_EXECUTION_ID BIGINT NOT NULL ,
25+
constraint TASK_EXEC_BATCH_FK foreign key (TASK_EXECUTION_ID)
26+
references TASK_EXECUTION(TASK_EXECUTION_ID)
27+
) ENGINE=InnoDB;
28+
29+
CREATE TABLE TASK_LOCK (
30+
LOCK_KEY CHAR(36) NOT NULL,
31+
REGION VARCHAR(100) NOT NULL,
32+
CLIENT_ID CHAR(36),
33+
CREATED_DATE DATETIME(6) NOT NULL,
34+
constraint LOCK_PK primary key (LOCK_KEY, REGION)
35+
) ENGINE=InnoDB;
36+
37+
CREATE SEQUENCE TASK_SEQ START WITH 1 MINVALUE 1 MAXVALUE 9223372036854775806 INCREMENT BY 1 NOCACHE NOCYCLE ENGINE=InnoDB;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2022-2022 the original author or authors.
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+
* https://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+
17+
package org.springframework.cloud.task.repository;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.junit.jupiter.api.Tag;
22+
import org.junit.jupiter.api.Test;
23+
import org.mariadb.jdbc.MariaDbDataSource;
24+
import org.testcontainers.containers.MariaDBContainer;
25+
import org.testcontainers.junit.jupiter.Container;
26+
import org.testcontainers.junit.jupiter.Testcontainers;
27+
import org.testcontainers.utility.DockerImageName;
28+
29+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
30+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
31+
import org.springframework.cloud.task.configuration.EnableTask;
32+
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
33+
import org.springframework.context.annotation.Bean;
34+
import org.springframework.core.io.ClassPathResource;
35+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
36+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
37+
38+
import static org.assertj.core.api.Assertions.assertThat;
39+
40+
@Tag("DockerRequired")
41+
@Testcontainers
42+
@SpringJUnitConfig
43+
public class MariaDbTaskRepositoryIntegrationTests {
44+
45+
private static final DockerImageName MARIADB_IMAGE = DockerImageName.parse("mariadb:10.9.3");
46+
47+
@Container
48+
public static MariaDBContainer<?> mariaDBContainer = new MariaDBContainer<>(MARIADB_IMAGE);
49+
50+
@Test
51+
public void testTaskExplorer() {
52+
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
53+
.withUserConfiguration(MariaDbTaskRepositoryIntegrationTests.TestConfiguration.class);
54+
55+
applicationContextRunner.run((context -> {
56+
TaskExplorer taskExplorer = context.getBean(TaskExplorer.class);
57+
assertThat(taskExplorer.getTaskExecutionCount()).isOne();
58+
}));
59+
applicationContextRunner.run((context -> {
60+
TaskExplorer taskExplorer = context.getBean(TaskExplorer.class);
61+
assertThat(taskExplorer.getTaskExecutionCount()).isEqualTo(2);
62+
}));
63+
}
64+
65+
@EnableTask
66+
@ImportAutoConfiguration(SimpleTaskAutoConfiguration.class)
67+
static class TestConfiguration {
68+
69+
public static boolean firstTime = true;
70+
71+
@Bean
72+
public DataSource dataSource() throws Exception {
73+
MariaDbDataSource datasource = new MariaDbDataSource();
74+
datasource.setUrl(mariaDBContainer.getJdbcUrl());
75+
datasource.setUser(mariaDBContainer.getUsername());
76+
datasource.setPassword(mariaDBContainer.getPassword());
77+
if (firstTime) {
78+
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
79+
databasePopulator
80+
.addScript(new ClassPathResource("/org/springframework/cloud/task/schema-mariadb.sql"));
81+
databasePopulator.execute(datasource);
82+
firstTime = false;
83+
}
84+
return datasource;
85+
}
86+
87+
}
88+
89+
}

0 commit comments

Comments
 (0)