Created
June 20, 2014 21:18
-
-
Save ilayaperumalg/3f379eb7f4527f6f6da4 to your computer and use it in GitHub Desktop.
XD-1822 Migration script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2014 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 | |
* | |
* http://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.xd.dirt.plugins.job; | |
import java.util.Collection; | |
import org.springframework.jdbc.core.JdbcTemplate; | |
import org.springframework.jdbc.core.SingleColumnRowMapper; | |
import org.springframework.jdbc.datasource.DriverManagerDataSource; | |
/** | |
* Migration class to migrate the {@link DistributedJobLocator} data pre-RC1 releases. | |
* | |
* @author Ilayaperumal Gopinathan | |
*/ | |
public class JobLocatorMigrator { | |
private static JdbcTemplate jdbcTemplate; | |
private static final String OLD_GET_ALL_JOB_NAMES = "SELECT JOB_NAME FROM JOB_REGISTRY_NAMES"; | |
private static final String OLD_GET_ALL_RESTARTABLE_JOBS = "SELECT JOB_NAME FROM JOB_REGISTRY_RESTARTABLES WHERE IS_RESTARTABLE='true'"; | |
private static final String OLD_GET_ALL_INCREMENTABLE_JOBS = "SELECT JOB_NAME FROM JOB_REGISTRY_INCREMENTABLES WHERE IS_INCREMENTABLE='true'"; | |
private static final String OLD_GET_STEP_NAMES = "SELECT STEP_NAME FROM JOB_REGISTRY_STEP_NAMES WHERE JOB_NAME = ?"; | |
private static final String ADD_JOB_REGISTRY = "INSERT INTO XD_JOB_REGISTRY(IS_INCREMENTABLE, IS_RESTARTABLE, JOB_NAME) VALUES(?, ?, ?)"; | |
private static final String ADD_STEP_NAME = "INSERT INTO XD_JOB_REGISTRY_STEP_NAMES(JOB_NAME, STEP_NAME) VALUES(?, ?)"; | |
private void performUpgrade() { | |
//XD:1822 Migration changes | |
Collection<String> jobNames = jdbcTemplate.queryForList(OLD_GET_ALL_JOB_NAMES, String.class); | |
Collection<String> jobNamesWithRestartables = jdbcTemplate.queryForList(OLD_GET_ALL_RESTARTABLE_JOBS, | |
String.class); | |
Collection<String> jobNamesWithIncrementables = jdbcTemplate.queryForList( | |
OLD_GET_ALL_INCREMENTABLE_JOBS, | |
String.class); | |
for (String jobName : jobNames) { | |
Collection<String> stepNames = jdbcTemplate.query(OLD_GET_STEP_NAMES, | |
new SingleColumnRowMapper<String>( | |
String.class), jobName); | |
addJobName(jobName, jobNamesWithIncrementables.contains(jobName), | |
jobNamesWithRestartables.contains(jobName)); | |
addStepNames(jobName, stepNames); | |
} | |
} | |
/** | |
* Add a new job entry into the XD_JOB_REGISTRY table. | |
* | |
* @param name the name of the job | |
* @param incrementable flag to specify if the job parameter can be incremented | |
* @param restartable flag to specify if the job can be restarted upon failure/stoppage. | |
*/ | |
private void addJobName(String name, boolean incrementable, boolean restartable) { | |
jdbcTemplate.update(ADD_JOB_REGISTRY, incrementable, restartable, name); | |
} | |
/** | |
* Add the collection of step names into XD_JOB_REGISTRY_STEP_NAMES for a given job. | |
* | |
* @param jobName the job name | |
* @param stepNames the collection of step names associated with this job | |
*/ | |
protected void addStepNames(String jobName, Collection<String> stepNames) { | |
for (String stepName : stepNames) { | |
jdbcTemplate.update(ADD_STEP_NAME, jobName, stepName); | |
} | |
} | |
public static void main(String[] args) { | |
DriverManagerDataSource dataSource = new DriverManagerDataSource(); | |
dataSource.setDriverClassName("org.hsqldb.jdbc.JDBCDriver"); | |
dataSource.setUrl("jdbc:hsqldb:hsql://localhost:9101/xdjob"); | |
dataSource.setUsername("sa"); | |
dataSource.setPassword(""); | |
jdbcTemplate = new JdbcTemplate(dataSource); | |
JobLocatorMigrator migrator = new JobLocatorMigrator(); | |
migrator.performUpgrade(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment