Skip to content

Commit e267e1f

Browse files
hpoettkerfmbenhassine
authored andcommitted
Adjust h2 schema to work with v2.0.x
Upgrade to H2 2.0.206 Issue #4043
1 parent ced3d06 commit e267e1f

File tree

3 files changed

+115
-4
lines changed

3 files changed

+115
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
<commons-dbcp2.version>2.9.0</commons-dbcp2.version>
8989
<slf4j.version>1.7.32</slf4j.version>
9090
<hsqldb.version>2.6.1</hsqldb.version>
91-
<h2.version>1.4.200</h2.version>
91+
<h2.version>2.0.206</h2.version>
9292
<sqlite.version>3.36.0.3</sqlite.version>
9393
<derby.version>10.14.2.0</derby.version>
9494
<artemis.version>2.19.0</artemis.version>

spring-batch-core/src/main/resources/org/springframework/batch/core/schema-h2.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
-- Autogenerated: do not edit this file
22

33
CREATE TABLE BATCH_JOB_INSTANCE (
4-
JOB_INSTANCE_ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
4+
JOB_INSTANCE_ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY ,
55
VERSION BIGINT ,
66
JOB_NAME VARCHAR(100) NOT NULL,
77
JOB_KEY VARCHAR(32) NOT NULL,
88
constraint JOB_INST_UN unique (JOB_NAME, JOB_KEY)
99
) ;
1010

1111
CREATE TABLE BATCH_JOB_EXECUTION (
12-
JOB_EXECUTION_ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
12+
JOB_EXECUTION_ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY ,
1313
VERSION BIGINT ,
1414
JOB_INSTANCE_ID BIGINT NOT NULL,
1515
CREATE_TIME TIMESTAMP NOT NULL,
@@ -37,7 +37,7 @@ CREATE TABLE BATCH_JOB_EXECUTION_PARAMS (
3737
) ;
3838

3939
CREATE TABLE BATCH_STEP_EXECUTION (
40-
STEP_EXECUTION_ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
40+
STEP_EXECUTION_ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY ,
4141
VERSION BIGINT NOT NULL,
4242
STEP_NAME VARCHAR(100) NOT NULL,
4343
JOB_EXECUTION_ID BIGINT NOT NULL,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 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+
package org.springframework.batch.core.test.repository;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
import java.util.UUID;
21+
22+
import javax.sql.DataSource;
23+
24+
import org.junit.Assert;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.Parameterized;
28+
import org.junit.runners.Parameterized.Parameters;
29+
30+
import org.springframework.batch.core.ExitStatus;
31+
import org.springframework.batch.core.Job;
32+
import org.springframework.batch.core.JobParameters;
33+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
34+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
35+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
36+
import org.springframework.batch.core.launch.JobLauncher;
37+
import org.springframework.batch.repeat.RepeatStatus;
38+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
39+
import org.springframework.context.annotation.Bean;
40+
import org.springframework.context.annotation.Configuration;
41+
import org.springframework.core.io.DefaultResourceLoader;
42+
import org.springframework.jdbc.core.JdbcTemplate;
43+
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
44+
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
45+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
46+
47+
/**
48+
* @author Henning Pöttker
49+
*/
50+
@RunWith(Parameterized.class)
51+
public class H2CompatibilityModeJobRepositoryIntegrationTests {
52+
53+
private final String compatibilityMode;
54+
55+
public H2CompatibilityModeJobRepositoryIntegrationTests(String compatibilityMode) {
56+
this.compatibilityMode = compatibilityMode;
57+
}
58+
59+
@Test
60+
public void testJobExecution() throws Exception {
61+
var context = new AnnotationConfigApplicationContext();
62+
context.register(TestConfiguration.class);
63+
context.registerBean(DataSource.class, this::buildDataSource);
64+
context.refresh();
65+
var jobLauncher = context.getBean(JobLauncher.class);
66+
var job = context.getBean(Job.class);
67+
68+
var jobExecution = jobLauncher.run(job, new JobParameters());
69+
70+
Assert.assertNotNull(jobExecution);
71+
Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
72+
73+
var jdbcTemplate = new JdbcTemplate(context.getBean(DataSource.class));
74+
jdbcTemplate.execute("SHUTDOWN");
75+
}
76+
77+
private DataSource buildDataSource() {
78+
var connectionUrl = String.format(
79+
"jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;MODE=%s",
80+
UUID.randomUUID(),
81+
this.compatibilityMode
82+
);
83+
var dataSource = new SimpleDriverDataSource(new org.h2.Driver(), connectionUrl, "sa", "");
84+
var populator = new ResourceDatabasePopulator();
85+
var resource = new DefaultResourceLoader()
86+
.getResource("/org/springframework/batch/core/schema-h2.sql");
87+
populator.addScript(resource);
88+
DatabasePopulatorUtils.execute(populator, dataSource);
89+
return dataSource;
90+
}
91+
92+
@Configuration
93+
@EnableBatchProcessing
94+
static class TestConfiguration {
95+
@Bean
96+
Job job(JobBuilderFactory jobs, StepBuilderFactory steps) {
97+
return jobs.get("job")
98+
.start(steps.get("step")
99+
.tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED)
100+
.build())
101+
.build();
102+
}
103+
}
104+
105+
@Parameters
106+
public static List<Object[]> data() throws Exception {
107+
return Arrays.stream(org.h2.engine.Mode.ModeEnum.values())
108+
.map(mode -> new Object[]{mode.toString()})
109+
.toList();
110+
}
111+
}

0 commit comments

Comments
 (0)