Skip to content

Commit

Permalink
#40 Can now create a triggers database via a property
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrudin committed Jul 5, 2015
1 parent 91b858f commit 6caeca3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/marklogic/appdeployer/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class AppConfig {
// Passed into the TokenReplacer that subclasses of AbstractCommand use
private Map<String, String> customTokens = new HashMap<>();

// Allows for creating a triggers database without a config file for one
private boolean createTriggerDatabase = true;

public AppConfig() {
this("src/main/ml-modules");
}
Expand Down Expand Up @@ -224,4 +227,12 @@ public void setCustomTokens(Map<String, String> customTokens) {
this.customTokens = customTokens;
}

public boolean isCreateTriggerDatabase() {
return createTriggerDatabase;
}

public void setCreateTriggerDatabase(boolean createTriggerDatabase) {
this.createTriggerDatabase = createTriggerDatabase;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;

import com.marklogic.appdeployer.AppConfig;
import com.marklogic.appdeployer.command.AbstractCommand;
import com.marklogic.appdeployer.command.CommandContext;
import com.marklogic.appdeployer.command.SortOrderConstants;
Expand All @@ -27,21 +28,27 @@ public Integer getUndoSortOrder() {

@Override
public void execute(CommandContext context) {
File f = context.getAppConfig().getConfigDir().getTriggersDatabaseFile();
AppConfig config = context.getAppConfig();
File f = config.getConfigDir().getTriggersDatabaseFile();
if (f.exists()) {
DatabaseManager dbMgr = new DatabaseManager(context.getManageClient());

String dbName = context.getAppConfig().getTriggersDatabaseName();
logger.info("Creating triggers database based on file at: " + f.getAbsolutePath());
String payload = copyFileToString(f);
payload = tokenReplacer.replaceTokens(payload, context.getAppConfig(), false);
dbMgr.save(payload);

createAndAttachForestOnEachHost(dbName, context.getManageClient());
payload = tokenReplacer.replaceTokens(payload, config, false);
createTriggersDatabase(payload, context);
} else if (config.isCreateTriggerDatabase()) {
logger.info("Creating triggers database because AppConfig property is set to true");
createTriggersDatabase(buildDefaultTriggersDatabasePayload(config), context);
} else {
logger.info("Not creating a triggers database, no file found at: " + f.getAbsolutePath());
}
}

protected void createTriggersDatabase(String payload, CommandContext context) {
DatabaseManager dbMgr = new DatabaseManager(context.getManageClient());
dbMgr.save(payload);
createAndAttachForestOnEachHost(context.getAppConfig().getTriggersDatabaseName(), context.getManageClient());
}

public void createAndAttachForestOnEachHost(String dbName, ManageClient client) {
ForestManager fmgr = new ForestManager(client);
String forestName = dbName + "-1";
Expand All @@ -53,13 +60,18 @@ public void createAndAttachForestOnEachHost(String dbName, ManageClient client)

@Override
public void undo(CommandContext context) {
File f = context.getAppConfig().getConfigDir().getTriggersDatabaseFile();
AppConfig config = context.getAppConfig();
File f = config.getConfigDir().getTriggersDatabaseFile();
if (f.exists()) {
DatabaseManager dbMgr = new DatabaseManager(context.getManageClient());
String payload = copyFileToString(f);
payload = tokenReplacer.replaceTokens(payload, context.getAppConfig(), false);
dbMgr.delete(payload);
new DatabaseManager(context.getManageClient()).delete(payload);
} else if (config.isCreateTriggerDatabase()) {
new DatabaseManager(context.getManageClient()).delete(buildDefaultTriggersDatabasePayload(config));
}
}

protected String buildDefaultTriggersDatabasePayload(AppConfig config) {
return format("{\"database-name\": \"%s\"}", config.getTriggersDatabaseName());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.marklogic.appdeployer.command.databases;

import java.io.File;

import org.junit.Test;

import com.marklogic.appdeployer.AbstractAppDeployerTest;
import com.marklogic.appdeployer.ConfigDir;
import com.marklogic.rest.mgmt.databases.DatabaseManager;
import com.marklogic.rest.mgmt.forests.ForestManager;

Expand All @@ -17,7 +20,7 @@ public void createAndDelete() {
DatabaseManager dbMgr = new DatabaseManager(manageClient);
ForestManager forestMgr = new ForestManager(manageClient);

String dbName = "sample-app-triggers";
String dbName = appConfig.getTriggersDatabaseName();
String forestName = dbName + "-1";

try {
Expand All @@ -31,4 +34,43 @@ public void createAndDelete() {
assertFalse("The triggers forest should have been deleted", forestMgr.forestExists(forestName));
}
}

@Test
public void createViaAppConfigProperty() {
appConfig.setConfigDir(new ConfigDir(new File("src/test/resources/sample-app/other-ml-config")));

initializeAppDeployer(new CreateTriggersDatabaseCommand());
appDeployer.deploy(appConfig);

DatabaseManager dbMgr = new DatabaseManager(manageClient);
String dbName = appConfig.getTriggersDatabaseName();

try {
assertTrue("The triggers database should have been created", dbMgr.exists(dbName));
} finally {
undeploySampleApp();
assertFalse("The triggers database should have been deleted", dbMgr.exists(dbName));
}
}

@Test
public void configPropertyIsSetToFalse() {
appConfig.setConfigDir(new ConfigDir(new File("src/test/resources/sample-app/other-ml-config")));
appConfig.setCreateTriggerDatabase(false);

initializeAppDeployer(new CreateTriggersDatabaseCommand());
appDeployer.deploy(appConfig);

DatabaseManager dbMgr = new DatabaseManager(manageClient);
String dbName = appConfig.getTriggersDatabaseName();

try {
assertFalse(
"No triggers database should have been created since the config directory doesn't have a triggers file and the property is set to false",
dbMgr.exists(dbName));
} finally {
undeploySampleApp();
}

}
}

0 comments on commit 6caeca3

Please sign in to comment.