Skip to content

Commit

Permalink
#204 Clean-up of Git configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoraboeuf committed Dec 30, 2014
1 parent db8d414 commit 235f64f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package net.nemerosa.ontrack.extension.git.support;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import net.nemerosa.ontrack.extension.git.model.BasicGitConfiguration;
import net.nemerosa.ontrack.json.ObjectMapperFactory;
import net.nemerosa.ontrack.model.support.DBMigrationAction;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Set;

/**
* Migration task for #204, for cleaning up the Git configurations.
*/
@Component
public class GitConfigurationMigrationAction11 implements DBMigrationAction {

private final ObjectMapper objectMapper = ObjectMapperFactory.create();

private final Set<String> validFields = Sets.newHashSet(
"fileAtCommitLink",
"commitLink",
"issueServiceConfigurationIdentifier",
"remote",
"indexationInterval",
"name",
"password",
"user"
);

@Override
public int getPatch() {
return 11;
}

@Override
public void migrate(Connection connection) throws Exception {
// For all Git configurations
try (PreparedStatement ps = connection.prepareStatement("SELECT * FROM CONFIGURATIONS WHERE TYPE = ?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) {
ps.setString(1, BasicGitConfiguration.class.getName());
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
// Configuration as JSON
String json = rs.getString("CONTENT");
// Parses the configuration
ObjectNode node = (ObjectNode) objectMapper.readTree(json);
// Gets all fields of the node
List<String> fieldNames = Lists.newArrayList(node.fieldNames());
// Removes any field that does not belong to the BasicGitConfiguration class
fieldNames.stream()
.filter(fieldName -> !validFields.contains(fieldName))
.forEach(node::remove);
// Updating
rs.updateString("CONTENT", objectMapper.writeValueAsString(node));
rs.updateRow();
}
}
}
}

@Override
public String getDisplayName() {
return "Git configuration migration (patch 11)";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@Component
public class MainDBInitConfig extends AbstractDBInitConfig {

public static final int VERSION = 10;
public static final int VERSION = 11;

private final ApplicationContext applicationContext;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- 11. Cleanup of Git configurations (#204)

-- No update. The version increment is used to trigger a migration service

-- Migration action: net.nemerosa.ontrack.extension.git.support.GitConfigurationMigrationAction11

0 comments on commit 235f64f

Please sign in to comment.