From 85fd1fe319277f8f052fa51cdc152ba1bfa027c8 Mon Sep 17 00:00:00 2001 From: Henning Poettker Date: Fri, 26 Aug 2022 01:19:53 +0200 Subject: [PATCH] Adjust SQL statement to allow added columns --- .../listener/support/JdbcTaskBatchDao.java | 10 +-- .../task/batch/listener/PrimaryKeyTests.java | 90 +++++++++++++++++++ .../resources/schema-with-primary-keys-h2.sql | 39 ++++++++ 3 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/listener/PrimaryKeyTests.java create mode 100644 spring-cloud-task-batch/src/test/resources/schema-with-primary-keys-h2.sql diff --git a/spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/listener/support/JdbcTaskBatchDao.java b/spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/listener/support/JdbcTaskBatchDao.java index a2f366db..c484f201 100644 --- a/spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/listener/support/JdbcTaskBatchDao.java +++ b/spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/listener/support/JdbcTaskBatchDao.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 the original author or authors. + * Copyright 2016-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,14 +37,14 @@ import org.springframework.util.StringUtils; */ public class JdbcTaskBatchDao implements TaskBatchDao { - private static final String INSERT_STATEMENT = "INSERT INTO %PREFIX%TASK_BATCH VALUES(?, ?)"; + private static final String INSERT_STATEMENT = "INSERT INTO %PREFIX%TASK_BATCH (TASK_EXECUTION_ID, JOB_EXECUTION_ID) VALUES (?, ?)"; private String tablePrefix = TaskProperties.DEFAULT_TABLE_PREFIX; - private JdbcOperations jdbcTemplate; + private final JdbcOperations jdbcTemplate; /** - * Intializes the JdbcTaskBatchDao. + * Initializes the JdbcTaskBatchDao. * @param dataSource {@link DataSource} where the task batch table resides. * @param tablePrefix the table prefix to use for this dao. */ @@ -55,7 +55,7 @@ public class JdbcTaskBatchDao implements TaskBatchDao { } /** - * Intializes the JdbcTaskBatchDao and defaults the table prefix to + * Initializes the JdbcTaskBatchDao and defaults the table prefix to * {@link TaskProperties#DEFAULT_TABLE_PREFIX}. * @param dataSource {@link DataSource} where the task batch table resides. */ diff --git a/spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/listener/PrimaryKeyTests.java b/spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/listener/PrimaryKeyTests.java new file mode 100644 index 00000000..008b0be5 --- /dev/null +++ b/spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/listener/PrimaryKeyTests.java @@ -0,0 +1,90 @@ +/* + * Copyright 2022-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.task.batch.listener; + +import java.util.Set; + +import javax.sql.DataSource; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.boot.SpringApplication; +import org.springframework.cloud.task.batch.configuration.TaskBatchTest; +import org.springframework.cloud.task.configuration.EnableTask; +import org.springframework.cloud.task.repository.TaskExplorer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Henning Pƶttker + */ +class PrimaryKeyTests { + + private ConfigurableApplicationContext applicationContext; + + @AfterEach + void tearDown() { + if (this.applicationContext != null && this.applicationContext.isActive()) { + this.applicationContext.close(); + } + } + + @Test + void testSchemaWithPrimaryKeys() { + this.applicationContext = SpringApplication.run(JobConfiguration.class); + + TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class); + + Set jobIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(1); + assertThat(jobIds).containsExactly(1L); + } + + @Configuration(proxyBeanMethods = false) + @EnableBatchProcessing + @TaskBatchTest + @EnableTask + static class JobConfiguration { + + @Bean + Job job(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) { + return jobBuilderFactory.get("job") + .start(stepBuilderFactory.get("step1").tasklet((contribution, chunkContext) -> { + System.out.println("Executed"); + return RepeatStatus.FINISHED; + }).build()).build(); + } + + @Bean + DataSource dataSource() { + return new EmbeddedDatabaseBuilder().addScript("classpath:schema-with-primary-keys-h2.sql") + .setType(EmbeddedDatabaseType.H2).build(); + } + + } + +} diff --git a/spring-cloud-task-batch/src/test/resources/schema-with-primary-keys-h2.sql b/spring-cloud-task-batch/src/test/resources/schema-with-primary-keys-h2.sql new file mode 100644 index 00000000..be51dfe0 --- /dev/null +++ b/spring-cloud-task-batch/src/test/resources/schema-with-primary-keys-h2.sql @@ -0,0 +1,39 @@ + +CREATE TABLE TASK_EXECUTION ( + TASK_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY , + START_TIME TIMESTAMP DEFAULT NULL , + END_TIME TIMESTAMP DEFAULT NULL , + TASK_NAME VARCHAR(100) , + EXIT_CODE INTEGER , + EXIT_MESSAGE VARCHAR(2500) , + ERROR_MESSAGE VARCHAR(2500) , + LAST_UPDATED TIMESTAMP, + EXTERNAL_EXECUTION_ID VARCHAR(255), + PARENT_EXECUTION_ID BIGINT +); + +CREATE TABLE TASK_EXECUTION_PARAMS ( + ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + TASK_EXECUTION_ID BIGINT NOT NULL , + TASK_PARAM VARCHAR(2500) , + constraint TASK_EXEC_PARAMS_FK foreign key (TASK_EXECUTION_ID) + references TASK_EXECUTION(TASK_EXECUTION_ID) +) ; + +CREATE TABLE TASK_TASK_BATCH ( + ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + TASK_EXECUTION_ID BIGINT NOT NULL , + JOB_EXECUTION_ID BIGINT NOT NULL , + constraint TASK_EXEC_BATCH_FK foreign key (TASK_EXECUTION_ID) + references TASK_EXECUTION(TASK_EXECUTION_ID) +) ; + +CREATE SEQUENCE TASK_SEQ ; + +CREATE TABLE TASK_LOCK ( + LOCK_KEY CHAR(36) NOT NULL, + REGION VARCHAR(100) NOT NULL, + CLIENT_ID CHAR(36), + CREATED_DATE TIMESTAMP NOT NULL, + constraint LOCK_PK primary key (LOCK_KEY, REGION) +);