Skip to content
Permalink
Browse files
JENKINS-40707
better method names, better rollback build step test
  • Loading branch information
prospero238 committed Jan 2, 2017
1 parent 4e6d4db commit 37eee90824cbde24c5f631ac1097e1f33ad2ff46
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 24 deletions.
@@ -50,15 +50,42 @@ is as follows:

liquibaseUpdate()

Parameters include testRollbacks, tagOnSuccessfulBuild, and dropAll.
Available parameters:
testRollbacks
tagOnSuccessfulBuild
dropAll.

Roll back operations may be done thusly:

liquibasaeRollback()

Parameters include:
Available parameters:

rollbackCount
rollbackToTag
rollbackToDate
rollbackLastHours




Common to each operation are the following parameters:
databaseEngine: Can be set to "MySQL", "PostgreSQL", "Derby", "H2", or "Hypersonic". Providing this value
eliminates the need to supply driverClassname.
credentialsId: The ID of the Jenkins credentials to use when the database requires username & password
changeLogFile: Path to the change log file
url: Database JDBC URL
See liquibase execution for an explaination for these configuration elements:
defaultSchemaName
contexts
liquibasePropertiesPath
classpath
driverClassname
labels
changeLogParameters
basePath

Supplying only "changeLogFile" will cause the buildstep to use an H2 in-memory database.


h2. Usage Tips
@@ -73,6 +100,7 @@ h3. Version History
h4. Version 1.2.0

* Credentials integration
* [JENKINS-40707|https://issues.jenkins-ci.org/browse/JENKINS-40707] Pipeline Support

h4. Version 1.1.0 (Aug 25, 2016)

@@ -2,15 +2,15 @@

import hudson.Extension;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

public class RollbackStep extends AbstractLiquibaseStep {
private String rollbackType;
protected int rollbackCount;
protected Integer rollbackCount;
private String rollbackLastHours;
private String rollbackToTag;
private String rollbackToDate;
@@ -40,11 +40,6 @@ public String getDisplayName() {
}


@DataBoundSetter
public void setRollbackType(String rollbackType) {
this.rollbackType = rollbackType;
}

@DataBoundSetter
public void setRollbackCount(int rollbackCount) {
this.rollbackCount = rollbackCount;
@@ -65,22 +60,22 @@ public void setRollbackToDate(String rollbackToDate) {
this.rollbackToDate = rollbackToDate;
}

public String getRollbackType() {
return rollbackType;
}

public int getRollbackCount() {
@CheckForNull
public Integer getRollbackCount() {
return rollbackCount;
}

@CheckForNull
public String getRollbackLastHours() {
return rollbackLastHours;
}

@CheckForNull
public String getRollbackToTag() {
return rollbackToTag;
}

@CheckForNull
public String getRollbackToDate() {
return rollbackToDate;
}
@@ -13,7 +13,6 @@
import com.google.inject.Inject;

public class RollbackStepExecution extends AbstractSynchronousStepExecution<Void> {

@Inject
private transient RollbackStep step;

@@ -38,8 +37,22 @@ protected Void run() throws Exception {
RollbackBuildStep rollbackBuildStep = new RollbackBuildStep();
LiquibaseWorkflowUtil.setCommonConfiguration(rollbackBuildStep, step);

rollbackBuildStep.setNumberOfChangesetsToRollback(String.valueOf(step.getRollbackCount()));
rollbackBuildStep.setRollbackToTag(step.getRollbackToTag());
if (step.getRollbackCount() != null) {
rollbackBuildStep.setNumberOfChangesetsToRollback(String.valueOf(step.getRollbackCount()));
rollbackBuildStep.setRollbackType(RollbackBuildStep.RollbackStrategy.COUNT.name());
}
if (step.getRollbackToDate() != null) {
rollbackBuildStep.setRollbackToDate(step.getRollbackToDate());
rollbackBuildStep.setRollbackType(RollbackBuildStep.RollbackStrategy.DATE.name());
}
if (step.getRollbackToTag() != null) {
rollbackBuildStep.setRollbackToTag(step.getRollbackToTag());
rollbackBuildStep.setRollbackType(RollbackBuildStep.RollbackStrategy.TAG.name());
}
if (step.getRollbackLastHours() != null) {
rollbackBuildStep.setRollbackLastHours(step.getRollbackLastHours());
rollbackBuildStep.setRollbackType(RollbackBuildStep.RollbackStrategy.RELATIVE.name());
}

rollbackBuildStep.perform(run, ws, launcher, listener);

@@ -81,7 +81,7 @@ public static void createDatabase(String dbUrl, File changeset) throws IOExcepti
liquibase.update(new Contexts());
}

public static String composeJdbcUrl(File inmemoryDatabaseFile) {
return "jdbc:h2:file:" + inmemoryDatabaseFile.getAbsolutePath();
public static String composeJdbcUrl(File databaseFile) {
return "jdbc:h2:file:" + databaseFile.getAbsolutePath();
}
}
@@ -10,6 +10,7 @@

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.jenkinsci.plugins.liquibase.evaluator.RolledbackChangesetAction;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
@@ -22,6 +23,7 @@
import org.slf4j.LoggerFactory;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;


@@ -56,14 +58,16 @@ public void should_allow_friendly_update_name() throws ExecutionException, Inter
@Test
public void should_allow_rollback_dsl()
throws IOException, SQLException, LiquibaseException, ExecutionException, InterruptedException {
String jdbcUrl = LiquibaseTestUtil.composeJdbcUrl(temporaryFolder.newFile());
LiquibaseTestUtil.createDatabase(jdbcUrl, changeLogFile);
String script = generatePipelineScript(workspace, "/rollback-pipeline-template.groovy");
File databaseFile = temporaryFolder.newFile();
String jdbcUrl = LiquibaseTestUtil.composeJdbcUrl(databaseFile);
String baseScript = generatePipelineScript(workspace, "/rollback-pipeline-template.groovy");
String script = baseScript.replaceAll("@DB_URL@", jdbcUrl);
CpsFlowDefinition cpsFlowDefinition = new CpsFlowDefinition(script);
job.setDefinition(cpsFlowDefinition);
WorkflowRun run = job.scheduleBuild2(0).get();
assertThat(run.getResult(), is(Result.SUCCESS));

RolledbackChangesetAction action = run.getAction(RolledbackChangesetAction.class);
assertThat(action.getRolledbackChangesets().size(), not(0));
}

private static File copyChangeLogFileToWorkspace(File workspace) throws IOException {
@@ -1,7 +1,11 @@
node {
ws('@WORKSPACE@') {
liquibaseUpdate(url: '@DB_URL@',
changeLogFile: 'sunny-day-changeset.xml')

liquibaseRollback(changeLogFile: 'sunny-day-changeset.xml', rollbackCount: 2)
liquibaseRollback(
url: '@DB_URL@',
changeLogFile: 'sunny-day-changeset.xml', rollbackCount: 2)
}
}

0 comments on commit 37eee90

Please sign in to comment.