Skip to content

Commit

Permalink
#374 Database resources can now be associated with a database via a m…
Browse files Browse the repository at this point in the history
…atching filename
  • Loading branch information
rjrudin committed Jul 29, 2019
1 parent d835dda commit 4188b49
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.marklogic.appdeployer.command;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.marklogic.appdeployer.ConfigDir;
import com.marklogic.client.ext.helper.LoggingObject;
import com.marklogic.mgmt.PayloadParser;
import com.marklogic.mgmt.SaveReceipt;
Expand All @@ -13,6 +14,7 @@
import com.marklogic.mgmt.mapper.DefaultResourceMapper;
import com.marklogic.mgmt.mapper.ResourceMapper;
import com.marklogic.mgmt.resource.ResourceManager;
import com.marklogic.mgmt.resource.databases.DatabaseManager;
import com.marklogic.mgmt.util.ObjectMapperFactory;
import com.marklogic.rest.util.JsonNodeUtil;
import com.marklogic.rest.util.PropertyBasedBiPredicate;
Expand Down Expand Up @@ -504,6 +506,44 @@ protected void setIncrementalMode(boolean incrementalMode) {
}
}

/**
* By default, the name of a database resource directory is assumed to be the name of the database that the resources
* within the directory should be associated with. But starting in 3.16.0, if the name of the directory doesn't
* match that of an existing database, then a check is made to see if there's a database file in the given ConfigDir
* that has the same name, minus its extension, as the database directory name. If so, then the database-name is
* extracted from that file and used as the database name. If not, an exception is thrown.
*
* @param context
* @param configDir
* @param databaseResourceDir
* @return
*/
protected String determineDatabaseNameForDatabaseResourceDirectory(CommandContext context, ConfigDir configDir, File databaseResourceDir) {
final String dirName = databaseResourceDir.getName();

if (new DatabaseManager(context.getManageClient()).exists(dirName)) {
return dirName;
}

File databasesDir = configDir.getDatabasesDir();
for (File f : listFilesInDirectory(databasesDir)) {
String name = f.getName();
int index = name.lastIndexOf('.');
name = index > 0 ? name.substring(0, index) : name;
if (dirName.equals(name)) {
logger.info("Found database file with same name, minus its extension, as the database resource directory; " +
"file: " + f);
String payload = copyFileToString(f, context);
String databaseName = new PayloadParser().getPayloadFieldValue(payload, "database-name");
logger.info("Associating database resource directory with database: " + databaseName);
return databaseName;
}
}

throw new RuntimeException("Could not determine database to associate with database resource directory: " +
databaseResourceDir);
}

public void setPayloadTokenReplacer(PayloadTokenReplacer payloadTokenReplacer) {
this.payloadTokenReplacer = payloadTokenReplacer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.marklogic.appdeployer.ConfigDir;
import com.marklogic.mgmt.SaveReceipt;
import com.marklogic.mgmt.api.configuration.Configuration;
import com.marklogic.mgmt.api.configuration.Configurations;
import com.marklogic.mgmt.resource.ResourceManager;

import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public void execute(CommandContext context) {
AppConfig appConfig = context.getAppConfig();
for (ConfigDir configDir : appConfig.getConfigDirs()) {
deployActions(context, configDir, appConfig.getContentDatabaseName());

for (File dir : configDir.getDatabaseResourceDirectories()) {
deployActions(context, new ConfigDir(dir), dir.getName());
String databaseName = determineDatabaseNameForDatabaseResourceDirectory(context, configDir, dir);
deployActions(context, new ConfigDir(dir), databaseName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public void execute(CommandContext context) {
for (ConfigDir configDir : appConfig.getConfigDirs()) {
deployAlertConfigs(context, configDir, appConfig.getContentDatabaseName());
for (File dir : configDir.getDatabaseResourceDirectories()) {
deployAlertConfigs(context, new ConfigDir(dir), dir.getName());
String databaseName = determineDatabaseNameForDatabaseResourceDirectory(context, configDir, dir);
deployAlertConfigs(context, new ConfigDir(dir), databaseName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public void execute(CommandContext context) {
for (ConfigDir configDir : appConfig.getConfigDirs()) {
deployRules(context, configDir, appConfig.getContentDatabaseName());
for (File dir : configDir.getDatabaseResourceDirectories()) {
deployRules(context, new ConfigDir(dir), dir.getName());
String databaseName = determineDatabaseNameForDatabaseResourceDirectory(context, configDir, dir);
deployRules(context, new ConfigDir(dir), databaseName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public void execute(CommandContext context) {
for (ConfigDir configDir : appConfig.getConfigDirs()) {
deployConfigs(context, configDir, appConfig.getContentDatabaseName());
for (File dir : configDir.getDatabaseResourceDirectories()) {
deployConfigs(context, new ConfigDir(dir), dir.getName());
String databaseName = determineDatabaseNameForDatabaseResourceDirectory(context, configDir, dir);
deployConfigs(context, new ConfigDir(dir), databaseName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public void execute(CommandContext context) {
for (ConfigDir configDir : appConfig.getConfigDirs()) {
deployFlexRepPulls(context, configDir, appConfig.getContentDatabaseName());
for (File dir : configDir.getDatabaseResourceDirectories()) {
deployFlexRepPulls(context, new ConfigDir(dir), dir.getName());
String databaseName = determineDatabaseNameForDatabaseResourceDirectory(context, configDir, dir);
deployFlexRepPulls(context, new ConfigDir(dir), databaseName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public void execute(CommandContext context) {
for (ConfigDir configDir : appConfig.getConfigDirs()) {
deployTargets(context, configDir, appConfig.getContentDatabaseName());
for (File dir : configDir.getDatabaseResourceDirectories()) {
deployTargets(context, new ConfigDir(dir), dir.getName());
String databaseName = determineDatabaseNameForDatabaseResourceDirectory(context, configDir, dir);
deployTargets(context, new ConfigDir(dir), databaseName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ protected void loadSchemasFromDatabaseSpecificPaths(CommandContext context) {
configDir.getDatabaseResourceDirectories().forEach(dir -> {
File schemasDir = new File(dir, "schemas");
if (schemasDir.exists()) {
loadSchemas(schemasDir.getAbsolutePath(), dir.getName(), context);
String databaseName = determineDatabaseNameForDatabaseResourceDirectory(context, configDir, dir);
loadSchemas(schemasDir.getAbsolutePath(), databaseName, context);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public void execute(CommandContext context) {
for (ConfigDir configDir : appConfig.getConfigDirs()) {
deployTemporalAxes(context, configDir, appConfig.getContentDatabaseName());
for (File dir : configDir.getDatabaseResourceDirectories()) {
deployTemporalAxes(context, new ConfigDir(dir), dir.getName());
String databaseName = determineDatabaseNameForDatabaseResourceDirectory(context, configDir, dir);
deployTemporalAxes(context, new ConfigDir(dir), databaseName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public void execute(CommandContext context) {
for (ConfigDir configDir : appConfig.getConfigDirs()) {
deployTemporalCollections(context, configDir, appConfig.getContentDatabaseName());
for (File dir : configDir.getDatabaseResourceDirectories()) {
deployTemporalCollections(context, new ConfigDir(dir), dir.getName());
String databaseName = determineDatabaseNameForDatabaseResourceDirectory(context, configDir, dir);
deployTemporalCollections(context, new ConfigDir(dir), databaseName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public void execute(CommandContext context) {
for (ConfigDir configDir : appConfig.getConfigDirs()) {
deployTemporalCollectionsLsqt(context, configDir, appConfig.getContentDatabaseName());
for (File dir : configDir.getDatabaseResourceDirectories()) {
deployTemporalCollectionsLsqt(context, new ConfigDir(dir), dir.getName());
String databaseName = determineDatabaseNameForDatabaseResourceDirectory(context, configDir, dir);
deployTemporalCollectionsLsqt(context, new ConfigDir(dir), databaseName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.marklogic.appdeployer.command.AbstractResourceCommand;
import com.marklogic.appdeployer.command.CommandContext;
import com.marklogic.appdeployer.command.SortOrderConstants;
import com.marklogic.mgmt.PayloadParser;
import com.marklogic.mgmt.resource.ResourceManager;
import com.marklogic.mgmt.resource.databases.DatabaseManager;
import com.marklogic.mgmt.resource.triggers.TriggerManager;

import java.io.File;
Expand Down Expand Up @@ -33,8 +35,9 @@ public void execute(CommandContext context) {
for (ConfigDir configDir : appConfig.getConfigDirs()) {
final String initialTriggersDatabaseName = databaseIdOrName != null ? databaseIdOrName : appConfig.getTriggersDatabaseName();
deployTriggers(context, configDir, initialTriggersDatabaseName);
for (File databaseResourceDir : configDir.getDatabaseResourceDirectories()) {
deployTriggers(context, new ConfigDir(databaseResourceDir), databaseResourceDir.getName());
for (File dir : configDir.getDatabaseResourceDirectories()) {
String databaseName = determineDatabaseNameForDatabaseResourceDirectory(context, configDir, dir);
deployTriggers(context, new ConfigDir(dir), databaseName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public void execute(CommandContext context) {
for (ConfigDir configDir : appConfig.getConfigDirs()) {
deployViewSchemas(context, configDir, appConfig.getContentDatabaseName());
for (File dir : configDir.getDatabaseResourceDirectories()) {
deployViewSchemas(context, new ConfigDir(dir), dir.getName());
String databaseName = determineDatabaseNameForDatabaseResourceDirectory(context, configDir, dir);
deployViewSchemas(context, new ConfigDir(dir), databaseName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ public void test() {

triggerManager = new TriggerManager(manageClient, "other-" + appConfig.getTriggersDatabaseName());
assertTrue(triggerManager.exists("other-trigger"));

triggerManager = new TriggerManager(manageClient, "third-" + appConfig.getTriggersDatabaseName());
assertTrue(triggerManager.exists("third-trigger"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"database-name": "third-%%TRIGGERS_DATABASE%%"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "third-trigger",
"description": "third trigger",
"event": {
"data-event": {
"directory-scope": {
"uri": "/thirdDir/",
"depth": "1"
},
"document-content": {
"update-kind": "create"
},
"when": "post-commit"
}
},
"module": "/test.xqy",
"module-db": "Modules",
"module-root": "/modules/",
"enabled": true,
"recursive": true,
"task-priority": "normal"
}

0 comments on commit 4188b49

Please sign in to comment.