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

Commit 0df75cb

Browse files
committed
#404 Content database not deleted on undeploy if filename is ignored
This is specific to the narrow scope of this ticket, which is that the "default content database filename" is ignored
1 parent 4ee356d commit 0df75cb

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import com.fasterxml.jackson.databind.ObjectReader;
44
import com.fasterxml.jackson.databind.node.ObjectNode;
5+
import com.marklogic.appdeployer.AppConfig;
56
import com.marklogic.appdeployer.ConfigDir;
67
import com.marklogic.appdeployer.command.AbstractUndoableCommand;
78
import com.marklogic.appdeployer.command.CommandContext;
9+
import com.marklogic.appdeployer.command.ResourceFilenameFilter;
810
import com.marklogic.appdeployer.command.SortOrderConstants;
911
import com.marklogic.appdeployer.command.forests.DeployForestsCommand;
1012
import com.marklogic.mgmt.PayloadParser;
@@ -21,6 +23,7 @@
2123
import com.marklogic.rest.util.JsonNodeUtil;
2224

2325
import java.io.File;
26+
import java.io.FilenameFilter;
2427
import java.io.IOException;
2528
import java.util.*;
2629

@@ -134,8 +137,39 @@ public void undo(CommandContext context) {
134137
// If no databases were found, may still need to delete the content database in case no file exists for it.
135138
// That's because the command for creating a REST API server will not delete the content database by default,
136139
// though it will delete the test database by default
137-
DatabaseManager dbMgr = new DeployDatabaseCommand().newDatabaseManageForDeleting(context);
138-
dbMgr.deleteByName(context.getAppConfig().getContentDatabaseName());
140+
if (deleteContentDatabaseOnUndo(databasePlans, context.getAppConfig())) {
141+
DatabaseManager dbMgr = new DeployDatabaseCommand().newDatabaseManageForDeleting(context);
142+
dbMgr.deleteByName(context.getAppConfig().getContentDatabaseName());
143+
}
144+
}
145+
146+
/**
147+
* If no database files are found, may still need to delete the content database in case no file exists for it.
148+
* That's because the command for creating a REST API server will not delete the content database by default.
149+
*
150+
* Per ticket #404, this will now do a check to see if the default content database filename is ignored. If so,
151+
* and there are no database files found, then the content database will not be deleted.
152+
*
153+
* @param databasePlans
154+
* @param appConfig
155+
* @return
156+
*/
157+
protected boolean deleteContentDatabaseOnUndo(List<DatabasePlan> databasePlans, AppConfig appConfig) {
158+
if (databasePlans == null || databasePlans.isEmpty()) {
159+
FilenameFilter filter = getResourceFilenameFilter();
160+
if (filter != null && filter instanceof ResourceFilenameFilter) {
161+
Set<String> filenamesToIgnore = ((ResourceFilenameFilter) filter).getFilenamesToIgnore();
162+
if (filenamesToIgnore != null && !filenamesToIgnore.isEmpty() && appConfig.getConfigDirs() != null) {
163+
for (ConfigDir configDir : appConfig.getConfigDirs()) {
164+
if (filenamesToIgnore.contains(configDir.getDefaultContentDatabaseFilename())) {
165+
return false;
166+
}
167+
}
168+
}
169+
}
170+
return true;
171+
}
172+
return false;
139173
}
140174

141175
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.marklogic.appdeployer.command.databases;
2+
3+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
4+
import com.marklogic.mgmt.resource.databases.DatabaseManager;
5+
import org.junit.Test;
6+
7+
import java.io.File;
8+
9+
public class UndeployDatabasesWithIgnoredContentDatabaseTest extends AbstractAppDeployerTest {
10+
11+
@Test
12+
public void test() {
13+
initializeAppDeployer(new DeployOtherDatabasesCommand(1));
14+
15+
// Deploy the database
16+
appConfig.getFirstConfigDir().setBaseDir(new File("src/test/resources/sample-app/db-only-config"));
17+
deploySampleApp();
18+
19+
// Ignore the file
20+
appConfig.setResourceFilenamesToIgnore("content-database.json");
21+
undeploySampleApp();
22+
23+
DatabaseManager mgr = new DatabaseManager(manageClient);
24+
String dbName = appConfig.getContentDatabaseName();
25+
try {
26+
// Verify db is still there
27+
assertTrue(
28+
"The database should still exist since the database file was ignored",
29+
mgr.exists(dbName)
30+
);
31+
} finally {
32+
mgr.deleteByName(appConfig.getContentDatabaseName());
33+
assertFalse("Verifying the database was deleted", mgr.exists(dbName));
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)