From 4188b49dbffbba950984d624b3b3d6e3ca723556 Mon Sep 17 00:00:00 2001 From: Rob Rudin Date: Mon, 29 Jul 2019 09:04:12 -0400 Subject: [PATCH] #374 Database resources can now be associated with a database via a matching filename --- .../appdeployer/command/AbstractCommand.java | 40 +++++++++++++++++++ .../command/AbstractResourceCommand.java | 1 - .../alert/DeployAlertActionsCommand.java | 4 +- .../alert/DeployAlertConfigsCommand.java | 3 +- .../alert/DeployAlertRulesCommand.java | 3 +- .../command/flexrep/DeployConfigsCommand.java | 3 +- .../command/flexrep/DeployPullsCommand.java | 3 +- .../command/flexrep/DeployTargetsCommand.java | 3 +- .../command/schemas/LoadSchemasCommand.java | 3 +- .../temporal/DeployTemporalAxesCommand.java | 3 +- .../DeployTemporalCollectionsCommand.java | 3 +- .../DeployTemporalCollectionsLSQTCommand.java | 3 +- .../triggers/DeployTriggersCommand.java | 7 +++- .../viewschemas/DeployViewSchemasCommand.java | 3 +- ...DeployTriggersToMultipleDatabasesTest.java | 3 ++ .../databases/third-triggers-database.json | 3 ++ .../triggers/third-trigger.json | 23 +++++++++++ 17 files changed, 96 insertions(+), 15 deletions(-) create mode 100644 src/test/resources/sample-app/multiple-triggers-databases/databases/third-triggers-database.json create mode 100644 src/test/resources/sample-app/multiple-triggers-databases/databases/third-triggers-database/triggers/third-trigger.json diff --git a/src/main/java/com/marklogic/appdeployer/command/AbstractCommand.java b/src/main/java/com/marklogic/appdeployer/command/AbstractCommand.java index 3733f00ac..5a5b86a0c 100644 --- a/src/main/java/com/marklogic/appdeployer/command/AbstractCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/AbstractCommand.java @@ -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; @@ -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; @@ -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; } diff --git a/src/main/java/com/marklogic/appdeployer/command/AbstractResourceCommand.java b/src/main/java/com/marklogic/appdeployer/command/AbstractResourceCommand.java index ba5dfdf95..118617f94 100644 --- a/src/main/java/com/marklogic/appdeployer/command/AbstractResourceCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/AbstractResourceCommand.java @@ -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; diff --git a/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertActionsCommand.java b/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertActionsCommand.java index b2d93ac8d..277a59516 100644 --- a/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertActionsCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertActionsCommand.java @@ -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); } } } diff --git a/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertConfigsCommand.java b/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertConfigsCommand.java index 182216535..f49863531 100644 --- a/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertConfigsCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertConfigsCommand.java @@ -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); } } } diff --git a/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertRulesCommand.java b/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertRulesCommand.java index ed676d941..6911c56f9 100644 --- a/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertRulesCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/alert/DeployAlertRulesCommand.java @@ -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); } } } diff --git a/src/main/java/com/marklogic/appdeployer/command/flexrep/DeployConfigsCommand.java b/src/main/java/com/marklogic/appdeployer/command/flexrep/DeployConfigsCommand.java index d394d1497..df89f96f1 100644 --- a/src/main/java/com/marklogic/appdeployer/command/flexrep/DeployConfigsCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/flexrep/DeployConfigsCommand.java @@ -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); } } } diff --git a/src/main/java/com/marklogic/appdeployer/command/flexrep/DeployPullsCommand.java b/src/main/java/com/marklogic/appdeployer/command/flexrep/DeployPullsCommand.java index 82f7c8286..32f66291a 100644 --- a/src/main/java/com/marklogic/appdeployer/command/flexrep/DeployPullsCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/flexrep/DeployPullsCommand.java @@ -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); } } } diff --git a/src/main/java/com/marklogic/appdeployer/command/flexrep/DeployTargetsCommand.java b/src/main/java/com/marklogic/appdeployer/command/flexrep/DeployTargetsCommand.java index 0f7b03b42..0d9e6ed0a 100644 --- a/src/main/java/com/marklogic/appdeployer/command/flexrep/DeployTargetsCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/flexrep/DeployTargetsCommand.java @@ -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); } } } diff --git a/src/main/java/com/marklogic/appdeployer/command/schemas/LoadSchemasCommand.java b/src/main/java/com/marklogic/appdeployer/command/schemas/LoadSchemasCommand.java index 7914bd4d1..d004e48f8 100644 --- a/src/main/java/com/marklogic/appdeployer/command/schemas/LoadSchemasCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/schemas/LoadSchemasCommand.java @@ -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); } }); }); diff --git a/src/main/java/com/marklogic/appdeployer/command/temporal/DeployTemporalAxesCommand.java b/src/main/java/com/marklogic/appdeployer/command/temporal/DeployTemporalAxesCommand.java index 4ed310e95..d262a2d77 100644 --- a/src/main/java/com/marklogic/appdeployer/command/temporal/DeployTemporalAxesCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/temporal/DeployTemporalAxesCommand.java @@ -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); } } } diff --git a/src/main/java/com/marklogic/appdeployer/command/temporal/DeployTemporalCollectionsCommand.java b/src/main/java/com/marklogic/appdeployer/command/temporal/DeployTemporalCollectionsCommand.java index 5a26f50e6..b1d3ae33e 100644 --- a/src/main/java/com/marklogic/appdeployer/command/temporal/DeployTemporalCollectionsCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/temporal/DeployTemporalCollectionsCommand.java @@ -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); } } } diff --git a/src/main/java/com/marklogic/appdeployer/command/temporal/DeployTemporalCollectionsLSQTCommand.java b/src/main/java/com/marklogic/appdeployer/command/temporal/DeployTemporalCollectionsLSQTCommand.java index f8f807f2f..74ec671ea 100644 --- a/src/main/java/com/marklogic/appdeployer/command/temporal/DeployTemporalCollectionsLSQTCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/temporal/DeployTemporalCollectionsLSQTCommand.java @@ -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); } } } diff --git a/src/main/java/com/marklogic/appdeployer/command/triggers/DeployTriggersCommand.java b/src/main/java/com/marklogic/appdeployer/command/triggers/DeployTriggersCommand.java index db871befa..1bc6241f4 100644 --- a/src/main/java/com/marklogic/appdeployer/command/triggers/DeployTriggersCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/triggers/DeployTriggersCommand.java @@ -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; @@ -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); } } } diff --git a/src/main/java/com/marklogic/appdeployer/command/viewschemas/DeployViewSchemasCommand.java b/src/main/java/com/marklogic/appdeployer/command/viewschemas/DeployViewSchemasCommand.java index 29a2094fd..dc0303c5b 100644 --- a/src/main/java/com/marklogic/appdeployer/command/viewschemas/DeployViewSchemasCommand.java +++ b/src/main/java/com/marklogic/appdeployer/command/viewschemas/DeployViewSchemasCommand.java @@ -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); } } } diff --git a/src/test/java/com/marklogic/appdeployer/command/databases/DeployTriggersToMultipleDatabasesTest.java b/src/test/java/com/marklogic/appdeployer/command/databases/DeployTriggersToMultipleDatabasesTest.java index 53f8a3563..99486b6a8 100644 --- a/src/test/java/com/marklogic/appdeployer/command/databases/DeployTriggersToMultipleDatabasesTest.java +++ b/src/test/java/com/marklogic/appdeployer/command/databases/DeployTriggersToMultipleDatabasesTest.java @@ -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")); } } diff --git a/src/test/resources/sample-app/multiple-triggers-databases/databases/third-triggers-database.json b/src/test/resources/sample-app/multiple-triggers-databases/databases/third-triggers-database.json new file mode 100644 index 000000000..081d83193 --- /dev/null +++ b/src/test/resources/sample-app/multiple-triggers-databases/databases/third-triggers-database.json @@ -0,0 +1,3 @@ +{ + "database-name": "third-%%TRIGGERS_DATABASE%%" +} diff --git a/src/test/resources/sample-app/multiple-triggers-databases/databases/third-triggers-database/triggers/third-trigger.json b/src/test/resources/sample-app/multiple-triggers-databases/databases/third-triggers-database/triggers/third-trigger.json new file mode 100644 index 000000000..24101cf88 --- /dev/null +++ b/src/test/resources/sample-app/multiple-triggers-databases/databases/third-triggers-database/triggers/third-trigger.json @@ -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" +} +