Skip to content

Incorrect v5 migration script for SQLServer #4349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
iyerha opened this issue Apr 11, 2023 · 2 comments
Closed

Incorrect v5 migration script for SQLServer #4349

iyerha opened this issue Apr 11, 2023 · 2 comments

Comments

@iyerha
Copy link

iyerha commented Apr 11, 2023

Bug description
The migration script for sqlserver provided in spring-batch-core for version 5.0 is incorrect.

Environment
Spring Batch version: 5.0.1, Java version = 17.0.6_10, database=SQLServer 2019

Steps to reproduce
All bugs relate to the table BATCH_JOB_EXECUTION_PARAMS in migration-sqlserver.sql script under 5.0:

  1. You cannot drop column DATE_VAL without first dropping the default constraint on it.
  2. You cannot use MODIFY COLUMN to rename columns (TYPE_CD, KEY_NAME, STRING_VAL). You would need to use the sp_rename procedure as described here
@iyerha iyerha added status: waiting-for-triage Issues that we did not analyse yet type: bug labels Apr 11, 2023
@fmbenhassine
Copy link
Contributor

Thank you for raising this. This is similar to another issue we had with PostgreSQL and Oracle. The SQL syntax is not the same for each database, which was assumed when creating the migration scripts.. I created an issue to improve the testing process for next releases, see #4289.

I will plan the fix for the next release. You are welcome to contribute if you managed to correctly migrate your schema to v5 on SQLServer.

@fmbenhassine fmbenhassine added in: core and removed status: waiting-for-triage Issues that we did not analyse yet labels Apr 25, 2023
@fmbenhassine fmbenhassine added this to the 5.0.2 milestone Apr 25, 2023
@fmbenhassine fmbenhassine changed the title Spring Batch 5 sqlserver migration script is incorrect Incorrect v5 migration script for SQLServer Apr 25, 2023
fmbenhassine added a commit that referenced this issue May 8, 2023
@fmbenhassine
Copy link
Contributor

The renaming of columns was updated to use SP_RENAME. However, the deletion of DATE_VAL was omitted from the script as there are several way of doing that which depend on the version of SQLServer.

The script was verified with the following test, which I keep here for a later reference:

package org.springframework.batch.core.test.repository;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.MSSQLServerContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;

/**
 * @author Mahmoud Ben Hassine
 */
@Testcontainers
class SQLServerMigrationScriptIntegrationTests {

	private static final DockerImageName SQLSERVER_IMAGE = DockerImageName
			.parse("mcr.microsoft.com/mssql/server:2019-CU20-ubuntu-20.04");

	@Container
	public static MSSQLServerContainer<?> sqlserver = new MSSQLServerContainer<>(SQLSERVER_IMAGE).acceptLicense();

	@Test
	void migrationScriptShouldBeValid() {
		SQLServerDataSource dataSource = new SQLServerDataSource();
		dataSource.setUser(sqlserver.getUsername());
		dataSource.setPassword(sqlserver.getPassword());
		dataSource.setURL(sqlserver.getJdbcUrl());

		ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
		databasePopulator.addScript(new ClassPathResource("/org/springframework/batch/core/schema-sqlserver-v4.sql"));
		databasePopulator.addScript(new ClassPathResource("/org/springframework/batch/core/migration/5.0/migration-sqlserver.sql"));

		Assertions.assertDoesNotThrow(() -> databasePopulator.execute(dataSource));
	}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants