Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 4ce3639

Browse files
committed
#261 Can now deploy triggers to any database
1 parent b5f69a1 commit 4ce3639

File tree

8 files changed

+122
-7
lines changed

8 files changed

+122
-7
lines changed

src/main/java/com/marklogic/appdeployer/command/triggers/DeployTriggersCommand.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.marklogic.appdeployer.command.triggers;
22

3+
import com.marklogic.appdeployer.AppConfig;
4+
import com.marklogic.appdeployer.ConfigDir;
35
import com.marklogic.appdeployer.command.AbstractResourceCommand;
46
import com.marklogic.appdeployer.command.CommandContext;
57
import com.marklogic.appdeployer.command.SortOrderConstants;
@@ -11,28 +13,47 @@
1113
/**
1214
* Defaults to the triggers database name in the AppConfig instance. Can be overridden via the databaseNameOrId
1315
* property.
16+
*
17+
* As of version 3.7.0, this now supports a triggers "database resource directory" under "databases/(name of triggers database)/triggers".
1418
*/
1519
public class DeployTriggersCommand extends AbstractResourceCommand {
1620

1721
private String databaseIdOrName;
22+
private TriggerManager currentTriggerManager;
1823

1924
public DeployTriggersCommand() {
2025
setExecuteSortOrder(SortOrderConstants.DEPLOY_TRIGGERS);
2126
// Triggers are stored in a database, so we don't need to delete them as the database will be deleted
2227
setDeleteResourcesOnUndo(false);
2328
}
2429

25-
@Override
30+
@Override
31+
public void execute(CommandContext context) {
32+
AppConfig appConfig = context.getAppConfig();
33+
for (ConfigDir configDir : appConfig.getConfigDirs()) {
34+
final String initialTriggersDatabaseName = databaseIdOrName != null ? databaseIdOrName : appConfig.getTriggersDatabaseName();
35+
deployTriggers(context, configDir, initialTriggersDatabaseName);
36+
for (File databaseResourceDir : configDir.getDatabaseResourceDirectories()) {
37+
deployTriggers(context, new ConfigDir(databaseResourceDir), databaseResourceDir.getName());
38+
}
39+
}
40+
}
41+
42+
protected void deployTriggers(CommandContext context, ConfigDir configDir, String databaseIdOrName) {
43+
currentTriggerManager = new TriggerManager(context.getManageClient(), databaseIdOrName);
44+
processExecuteOnResourceDir(context, configDir.getTriggersDir());
45+
}
46+
47+
@Override
48+
protected ResourceManager getResourceManager(CommandContext context) {
49+
return currentTriggerManager;
50+
}
51+
52+
@Override
2653
protected File[] getResourceDirs(CommandContext context) {
2754
return findResourceDirs(context, configDir -> configDir.getTriggersDir());
2855
}
2956

30-
@Override
31-
protected ResourceManager getResourceManager(CommandContext context) {
32-
String db = databaseIdOrName != null ? databaseIdOrName : context.getAppConfig().getTriggersDatabaseName();
33-
return new TriggerManager(context.getManageClient(), db);
34-
}
35-
3657
public void setDatabaseIdOrName(String databaseIdOrName) {
3758
this.databaseIdOrName = databaseIdOrName;
3859
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.marklogic.appdeployer.command.databases;
2+
3+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
4+
import com.marklogic.appdeployer.ConfigDir;
5+
import com.marklogic.appdeployer.command.triggers.DeployTriggersCommand;
6+
import com.marklogic.mgmt.resource.triggers.TriggerManager;
7+
import org.junit.After;
8+
import org.junit.Test;
9+
10+
import java.io.File;
11+
12+
public class DeployTriggersToMultipleDatabasesTest extends AbstractAppDeployerTest {
13+
14+
@After
15+
public void teardown() {
16+
undeploySampleApp();
17+
}
18+
19+
@Test
20+
public void test() {
21+
appConfig.setConfigDir(new ConfigDir(new File("src/test/resources/sample-app/multiple-triggers-databases")));
22+
23+
initializeAppDeployer(new DeployContentDatabasesCommand(1), new DeployTriggersDatabaseCommand(),
24+
new DeployOtherDatabasesCommand(), new DeployTriggersCommand());
25+
26+
deploySampleApp();
27+
28+
TriggerManager triggerManager = new TriggerManager(manageClient, appConfig.getTriggersDatabaseName());
29+
assertTrue(triggerManager.exists("my-trigger"));
30+
31+
triggerManager = new TriggerManager(manageClient, "other-" + appConfig.getTriggersDatabaseName());
32+
assertTrue(triggerManager.exists("other-trigger"));
33+
}
34+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"database-name": "%%DATABASE%%",
3+
"triggers-database": "%%TRIGGERS_DATABASE%%"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"database-name": "other-%%DATABASE%%",
3+
"triggers-database": "other-%%TRIGGERS_DATABASE%%"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "other-trigger",
3+
"description": "other trigger",
4+
"event": {
5+
"data-event": {
6+
"directory-scope": {
7+
"uri": "/otherDir/",
8+
"depth": "1"
9+
},
10+
"document-content": {
11+
"update-kind": "create"
12+
},
13+
"when": "post-commit"
14+
}
15+
},
16+
"module": "/test.xqy",
17+
"module-db": "Modules",
18+
"module-root": "/modules/",
19+
"enabled": true,
20+
"recursive": true,
21+
"task-priority": "normal"
22+
}
23+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"database-name": "other-%%TRIGGERS_DATABASE%%"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"database-name": "%%TRIGGERS_DATABASE%%"
3+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "my-trigger",
3+
"description": "my trigger",
4+
"event": {
5+
"data-event": {
6+
"directory-scope": {
7+
"uri": "/myDir/",
8+
"depth": "1"
9+
},
10+
"document-content": {
11+
"update-kind": "create"
12+
},
13+
"when": "post-commit"
14+
}
15+
},
16+
"module": "/test.xqy",
17+
"module-db": "Modules",
18+
"module-root": "/modules/",
19+
"enabled": true,
20+
"recursive": true,
21+
"task-priority": "normal"
22+
}
23+

0 commit comments

Comments
 (0)