Rolling Back from Spring Batch 6 to Spring Batch 5.
If you are migrating from Spring boot 2.x.x to Spring boot 3.x.x and if you are using Spring Batch in your application, then you would likely be migrating it from Spring Batch 5 to Spring Batch 6.
In Spring Batch 6, there are quite a few modifications to the table structure. Some modifications are such that no reversion could be done (like dropping columns) which would involve in loss of data if in case you would want to revert back to Spring Batch 5.
To avoid this, you can follow this process to backup your tables and use these tables if you want to revert back to Spring Batch 5.
Spring Batch 5 involves below tables:
- BATCH_JOB_INSTANCE
- BATCH_JOB_EXECUTION
- BATCH_JOB_EXECUTION_PARAMS
- BATCH_STEP_EXECUTION
- BATCH_JOB_EXECUTION_CONTEXT
- BATCH_STEP_EXECUTION_CONTEXT
- BATCH_JOB_SEQ
- BATCH_JOB_EXECUTION_SEQ
- BATCH_STEP_EXECUTION_SEQ
According to the official documentation, to migrate to Spring Batch 6 you need to alter these below tables:
- BATCH_JOB_EXECUTION
- BATCH_JOB_EXECUTION_PARAMS
- BATCH_STEP_EXECUTION
The upgrade also requires you to drop and recreate BATCH_STEP_EXECUTION_SEQ, BATCH_JOB_EXECUTION_SEQ and BATCH_JOB_SEQ tables.
The issue here is that if we are upgrading to Spring Batch 6 without backup of these tables, the rollback would be a nightmare.
To resolve this, we can take backup of all these tables and rename them.
The key point to note here is renaming and adding the foreign keys as well.
Here is the script to take backup of these tables and use it in case of rollback.
CREATE TABLE IF NOT EXISTS `BATCH_JOB_INSTANCE_backup` LIKE `BATCH_JOB_INSTANCE`;
INSERT INTO `BATCH_JOB_INSTANCE_backup` SELECT * FROM `BATCH_JOB_INSTANCE`;
CREATE TABLE IF NOT EXISTS `BATCH_JOB_EXECUTION_SEQ_backup` LIKE `BATCH_JOB_EXECUTION_SEQ`;
INSERT INTO `BATCH_JOB_EXECUTION_SEQ_backup` SELECT * FROM `BATCH_JOB_EXECUTION_SEQ`;
CREATE TABLE IF NOT EXISTS `BATCH_JOB_SEQ_backup` LIKE `BATCH_JOB_SEQ`;
INSERT INTO `BATCH_JOB_SEQ_backup` SELECT * FROM `BATCH_JOB_SEQ`;
CREATE TABLE IF NOT EXISTS `BATCH_STEP_EXECUTION_SEQ_backup` LIKE `BATCH_STEP_EXECUTION_SEQ`;
INSERT INTO `BATCH_STEP_EXECUTION_SEQ_backup` SELECT * FROM `BATCH_STEP_EXECUTION_SEQ`;
CREATE TABLE IF NOT EXISTS `BATCH_JOB_EXECUTION_backup` LIKE `BATCH_JOB_EXECUTION`;
ALTER TABLE `BATCH_JOB_EXECUTION_backup`
ADD CONSTRAINT `JOB_INST_EXEC_FK_backup` FOREIGN KEY (`JOB_INSTANCE_ID`) REFERENCES `BATCH_JOB_INSTANCE_backup` (`JOB_INSTANCE_ID`);
INSERT INTO `BATCH_JOB_EXECUTION_backup` SELECT * FROM `BATCH_JOB_EXECUTION`;
CREATE TABLE IF NOT EXISTS `BATCH_JOB_EXECUTION_PARAMS_backup` LIKE `BATCH_JOB_EXECUTION_PARAMS`;
ALTER TABLE `BATCH_JOB_EXECUTION_PARAMS_backup`
ADD CONSTRAINT `JOB_EXEC_PARAMS_FK_backup` FOREIGN KEY (`JOB_EXECUTION_ID`) REFERENCES `BATCH_JOB_EXECUTION_backup` (`JOB_EXECUTION_ID`);
INSERT INTO `BATCH_JOB_EXECUTION_PARAMS_backup` SELECT * FROM `BATCH_JOB_EXECUTION_PARAMS`;
CREATE TABLE IF NOT EXISTS `BATCH_STEP_EXECUTION_backup` LIKE `BATCH_STEP_EXECUTION`;
ALTER TABLE `BATCH_STEP_EXECUTION_backup`
ADD CONSTRAINT `JOB_EXEC_STEP_FK_backup` FOREIGN KEY (`JOB_EXECUTION_ID`) REFERENCES `BATCH_JOB_EXECUTION_backup` (`JOB_EXECUTION_ID`);
INSERT INTO `BATCH_STEP_EXECUTION_backup` SELECT * FROM `BATCH_STEP_EXECUTION`;
CREATE TABLE IF NOT EXISTS `BATCH_STEP_EXECUTION_CONTEXT_backup` LIKE `BATCH_STEP_EXECUTION_CONTEXT`;
ALTER TABLE `BATCH_STEP_EXECUTION_CONTEXT_backup`
ADD CONSTRAINT `STEP_EXEC_CTX_FK_backup` FOREIGN KEY (`STEP_EXECUTION_ID`) REFERENCES `BATCH_STEP_EXECUTION_backup` (`STEP_EXECUTION_ID`);
INSERT INTO `BATCH_STEP_EXECUTION_CONTEXT_backup` SELECT * FROM `BATCH_STEP_EXECUTION_CONTEXT`;
CREATE TABLE IF NOT EXISTS `BATCH_JOB_EXECUTION_CONTEXT_backup` LIKE `BATCH_JOB_EXECUTION_CONTEXT`;
ALTER TABLE `BATCH_JOB_EXECUTION_CONTEXT_backup`
ADD CONSTRAINT `JOB_EXEC_CTX_FK_backup` FOREIGN KEY (`JOB_EXECUTION_ID`) REFERENCES `BATCH_JOB_EXECUTION_backup` (`JOB_EXECUTION_ID`);
INSERT INTO `BATCH_JOB_EXECUTION_CONTEXT_backup` SELECT * FROM `BATCH_JOB_EXECUTION_CONTEXT`;
Now original tables are renamed with suffix “_backup”. When needed we can add rename these tables to original names and rename the newly created tables with “_backup_springboot”
After executing this script both the newly created tables and old tables will have the same data, which ensures there is no loss of data.
We have new added foreign keys as well to ensure the new tables are also in accordance with Spring Batch schema.