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

Commit 4b9e863

Browse files
committed
#310 Not undeploying default databases
1 parent 6b9d815 commit 4b9e863

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

src/main/java/com/marklogic/appdeployer/command/databases/DeployDatabaseCommand.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.ArrayList;
1818
import java.util.List;
1919
import java.util.Map;
20+
import java.util.Set;
2021

2122
/**
2223
* Can be used for creating any kind of database with any sorts of forests. Specifying a config file for the database or
@@ -77,6 +78,11 @@ public class DeployDatabaseCommand extends AbstractCommand implements UndoableCo
7778
private boolean subDatabase = false;
7879
private String superDatabaseName;
7980

81+
/**
82+
* Expected to be set by DeployOtherDatabasesCommand; a list of database names (should default to the ones MarkLogic
83+
* provides out-of-the-box) that won't be undeployed during an "undo" operation.
84+
*/
85+
private Set<String> databasesToNotUndeploy;
8086

8187
public DeployDatabaseCommand() {
8288
setExecuteSortOrder(SortOrderConstants.DEPLOY_OTHER_DATABASES);
@@ -129,6 +135,15 @@ public void execute(CommandContext context) {
129135
@Override
130136
public void undo(CommandContext context) {
131137
String payload = buildPayload(context);
138+
139+
if (databasesToNotUndeploy != null) {
140+
final String dbName = new PayloadParser().getPayloadFieldValue(payload, "database-name", false);
141+
if (dbName != null && databasesToNotUndeploy.contains(dbName)) {
142+
logger.info(format("Not undeploying database %s because it is in the list of database names to not undeploy.", dbName));
143+
return;
144+
}
145+
}
146+
132147
if (payload != null) {
133148
DatabaseManager dbMgr = newDatabaseManageForDeleting(context);
134149
// if this has subdatabases, detach/delete them first
@@ -422,4 +437,12 @@ public String getSuperDatabaseName() {
422437
public void setDatabaseFile(File databaseFile) {
423438
this.databaseFile = databaseFile;
424439
}
440+
441+
public Set<String> getDatabasesToNotUndeploy() {
442+
return databasesToNotUndeploy;
443+
}
444+
445+
public void setDatabasesToNotUndeploy(Set<String> databasesToNotUndeploy) {
446+
this.databasesToNotUndeploy = databasesToNotUndeploy;
447+
}
425448
}

src/main/java/com/marklogic/appdeployer/command/databases/DeployOtherDatabasesCommand.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public class DeployOtherDatabasesCommand extends AbstractUndoableCommand {
3131
private String forestFilename;
3232
private boolean createForestsOnEachHost = true;
3333

34+
/**
35+
* Defines database names that, by default, this command will never undeploy.
36+
*/
37+
private Set<String> defaultDatabasesToNotUndeploy = new HashSet<>();
38+
3439
public DeployOtherDatabasesCommand() {
3540
this(1);
3641
}
@@ -39,8 +44,23 @@ public DeployOtherDatabasesCommand(int forestsPerHost) {
3944
setForestsPerHost(forestsPerHost);
4045
setExecuteSortOrder(SortOrderConstants.DEPLOY_OTHER_DATABASES);
4146
setUndoSortOrder(SortOrderConstants.DELETE_OTHER_DATABASES);
47+
initializeDefaultDatabasesToNotUndeploy();
4248
}
4349

50+
protected void initializeDefaultDatabasesToNotUndeploy() {
51+
defaultDatabasesToNotUndeploy = new HashSet<>();
52+
defaultDatabasesToNotUndeploy.add("App-Services");
53+
defaultDatabasesToNotUndeploy.add("Documents");
54+
defaultDatabasesToNotUndeploy.add("Extensions");
55+
defaultDatabasesToNotUndeploy.add("Fab");
56+
defaultDatabasesToNotUndeploy.add("Last-Login");
57+
defaultDatabasesToNotUndeploy.add("Meters");
58+
defaultDatabasesToNotUndeploy.add("Modules");
59+
defaultDatabasesToNotUndeploy.add("Schemas");
60+
defaultDatabasesToNotUndeploy.add("Security");
61+
defaultDatabasesToNotUndeploy.add("Triggers");
62+
}
63+
4464
@Override
4565
public void execute(CommandContext context) {
4666
List<DeployDatabaseCommand> list = buildDatabaseCommands(context);
@@ -68,6 +88,7 @@ public void undo(CommandContext context) {
6888
}
6989
}
7090

91+
7192
protected void sortCommandsBeforeUndo(List<DeployDatabaseCommand> list, CommandContext context) {
7293
Collections.sort(list, new DeployDatabaseCommandComparator(context, true));
7394
}
@@ -97,6 +118,7 @@ protected DeployDatabaseCommand buildDeployDatabaseCommand(File file) {
97118
c.setCheckForCustomForests(isCheckForCustomForests());
98119
c.setForestFilename(getForestFilename());
99120
c.setCreateForestsOnEachHost(isCreateForestsOnEachHost());
121+
c.setDatabasesToNotUndeploy(this.getDefaultDatabasesToNotUndeploy());
100122
return c;
101123
}
102124

@@ -151,4 +173,12 @@ public boolean isCreateForestsOnEachHost() {
151173
public void setCreateForestsOnEachHost(boolean createForestsOnEachHost) {
152174
this.createForestsOnEachHost = createForestsOnEachHost;
153175
}
176+
177+
public Set<String> getDefaultDatabasesToNotUndeploy() {
178+
return defaultDatabasesToNotUndeploy;
179+
}
180+
181+
public void setDefaultDatabasesToNotUndeploy(Set<String> defaultDatabasesToNotUndeploy) {
182+
this.defaultDatabasesToNotUndeploy = defaultDatabasesToNotUndeploy;
183+
}
154184
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.marklogic.appdeployer.command.databases;
2+
3+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
4+
import com.marklogic.appdeployer.ConfigDir;
5+
import com.marklogic.mgmt.resource.databases.DatabaseManager;
6+
import org.junit.Test;
7+
8+
import java.io.File;
9+
10+
public class DontUndeployDefaultDatabasesTest extends AbstractAppDeployerTest {
11+
12+
@Test
13+
public void test() {
14+
DatabaseManager mgr = new DatabaseManager(manageClient);
15+
16+
appConfig.setConfigDir(new ConfigDir(new File("src/test/resources/sample-app/default-databases")));
17+
initializeAppDeployer(new DeployContentDatabasesCommand(1), new DeployOtherDatabasesCommand());
18+
19+
deploySampleApp();
20+
21+
assertTrue(mgr.exists(appConfig.getContentDatabaseName()));
22+
assertTrue(mgr.exists("Fab"));
23+
24+
undeploySampleApp();
25+
26+
assertFalse(mgr.exists(appConfig.getContentDatabaseName()));
27+
assertTrue(mgr.exists("Fab"));
28+
}
29+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"database-name": "%%DATABASE%%"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"database-name": "Fab"
3+
}

0 commit comments

Comments
 (0)