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

Commit 47d6e02

Browse files
committed
#177 New command for deleting test modules
1 parent 4d3aacf commit 47d6e02

File tree

8 files changed

+257
-118
lines changed

8 files changed

+257
-118
lines changed

src/main/java/com/marklogic/appdeployer/AppConfig.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public class AppConfig {
9595
private boolean staticCheckLibraryAssets = false;
9696
private boolean bulkLoadAssets = true;
9797
private String moduleTimestampsPath;
98+
private boolean deleteTestModules = false;
99+
private String deleteTestModulesPattern = "/test/**";
98100

99101
private String schemasPath;
100102
private ConfigDir configDir;
@@ -811,4 +813,20 @@ public boolean isSortRolesByDependencies() {
811813
public void setSortRolesByDependencies(boolean sortRolesByDependencies) {
812814
this.sortRolesByDependencies = sortRolesByDependencies;
813815
}
816+
817+
public boolean isDeleteTestModules() {
818+
return deleteTestModules;
819+
}
820+
821+
public void setDeleteTestModules(boolean deleteTestModules) {
822+
this.deleteTestModules = deleteTestModules;
823+
}
824+
825+
public String getDeleteTestModulesPattern() {
826+
return deleteTestModulesPattern;
827+
}
828+
829+
public void setDeleteTestModulesPattern(String deleteTestModulesPattern) {
830+
this.deleteTestModulesPattern = deleteTestModulesPattern;
831+
}
814832
}

src/main/java/com/marklogic/appdeployer/DefaultAppConfigFactory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,18 @@ public AppConfig newAppConfig() {
365365
c.setStaticCheckLibraryAssets(Boolean.parseBoolean(prop));
366366
}
367367

368+
prop = getProperty("mlDeleteTestModules");
369+
if (prop != null) {
370+
logger.info("Delete test modules: " + prop);
371+
c.setDeleteTestModules(Boolean.parseBoolean(prop));
372+
}
373+
374+
prop = getProperty("mlDeleteTestModulesPattern");
375+
if (prop != null) {
376+
logger.info("Delete test modules pattern: " + prop);
377+
c.setDeleteTestModulesPattern(prop);
378+
}
379+
368380
/**
369381
* The following properties are all for generating Entity Services artifacts.
370382
*/

src/main/java/com/marklogic/appdeployer/command/CommandMapBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.marklogic.appdeployer.command.forests.DeployCustomForestsCommand;
2121
import com.marklogic.appdeployer.command.groups.DeployGroupsCommand;
2222
import com.marklogic.appdeployer.command.mimetypes.DeployMimetypesCommand;
23+
import com.marklogic.appdeployer.command.modules.DeleteTestModulesCommand;
2324
import com.marklogic.appdeployer.command.modules.LoadModulesCommand;
2425
import com.marklogic.appdeployer.command.restapis.DeployRestApiServersCommand;
2526
import com.marklogic.appdeployer.command.schemas.LoadSchemasCommand;
@@ -93,6 +94,7 @@ public Map<String, List<Command>> buildCommandMap() {
9394
// Modules
9495
List<Command> moduleCommands = new ArrayList<>();
9596
moduleCommands.add(new LoadModulesCommand());
97+
moduleCommands.add(new DeleteTestModulesCommand());
9698
map.put("mlModuleCommands", moduleCommands);
9799

98100
// Alerting

src/main/java/com/marklogic/appdeployer/command/SortOrderConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public abstract class SortOrderConstants {
3232
// Modules have to be loaded after the REST API server has been updated, for if the deployer is expecting to load
3333
// modules via SSL, then the REST API server must already be configured with a certificate template
3434
public static Integer LOAD_MODULES = 400;
35+
public static Integer DELETE_TEST_MODULES = 410;
3536

3637
// The modules database must exist before we deploy amps
3738
public static Integer DEPLOY_AMPS = 450;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.marklogic.appdeployer.command.modules;
2+
3+
import com.marklogic.appdeployer.AppConfig;
4+
import com.marklogic.appdeployer.command.AbstractCommand;
5+
import com.marklogic.appdeployer.command.CommandContext;
6+
import com.marklogic.client.DatabaseClient;
7+
import com.marklogic.client.DatabaseClientFactory;
8+
9+
public class DeleteModulesCommand extends AbstractCommand {
10+
11+
private String pattern;
12+
private String databaseName;
13+
14+
public DeleteModulesCommand() {
15+
super();
16+
}
17+
18+
public DeleteModulesCommand(String pattern) {
19+
this();
20+
this.pattern = pattern;
21+
}
22+
23+
@Override
24+
public void execute(CommandContext context) {
25+
if (pattern == null || pattern.trim().length() == 0) {
26+
logger.warn("No pattern was specified, so not deleting any modules");
27+
}
28+
29+
AppConfig appConfig = context.getAppConfig();
30+
31+
String dbName = databaseName != null ? databaseName : appConfig.getModulesDatabaseName();
32+
if (logger.isInfoEnabled()) {
33+
logger.info(format("Deleting modules in database '%s' with URIs matching pattern '%s'", dbName, pattern));
34+
}
35+
36+
DatabaseClient client = DatabaseClientFactory.newClient(appConfig.getHost(), appConfig.getAppServicesPort(), dbName,
37+
appConfig.getRestAdminUsername(), appConfig.getRestAdminPassword(), appConfig.getRestAuthentication(),
38+
appConfig.getRestSslContext(), appConfig.getRestSslHostnameVerifier());
39+
40+
String xquery = "for $uri in cts:uri-match('%s') where fn:doc-available($uri) return xdmp:document-delete($uri)";
41+
try {
42+
client.newServerEval().xquery(format(xquery, pattern)).evalAs(String.class);
43+
if (logger.isInfoEnabled()) {
44+
logger.info("Finished deleting modules");
45+
}
46+
} finally {
47+
client.release();
48+
}
49+
}
50+
51+
public void setPattern(String pattern) {
52+
this.pattern = pattern;
53+
}
54+
55+
public void setDatabaseName(String databaseName) {
56+
this.databaseName = databaseName;
57+
}
58+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.marklogic.appdeployer.command.modules;
2+
3+
import com.marklogic.appdeployer.command.CommandContext;
4+
import com.marklogic.appdeployer.command.SortOrderConstants;
5+
6+
/**
7+
* Subclass that is primarily intended for current Roxy users that are accustomed to having test-only modules deployed
8+
* under the "/test" path.
9+
*/
10+
public class DeleteTestModulesCommand extends DeleteModulesCommand {
11+
12+
public final static String DEFAULT_TEST_MODULES_PATTERN = "/test/**";
13+
14+
public DeleteTestModulesCommand() {
15+
this(DEFAULT_TEST_MODULES_PATTERN);
16+
}
17+
18+
public DeleteTestModulesCommand(String pattern) {
19+
super(pattern);
20+
setExecuteSortOrder(SortOrderConstants.DELETE_TEST_MODULES);
21+
}
22+
23+
@Override
24+
public void execute(CommandContext context) {
25+
if (context.getAppConfig().isDeleteTestModules()) {
26+
String pattern = context.getAppConfig().getDeleteTestModulesPattern();
27+
if (pattern != null) {
28+
setPattern(pattern);
29+
}
30+
super.execute(context);
31+
}
32+
}
33+
}

src/test/java/com/marklogic/appdeployer/DefaultAppConfigFactoryTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public void allProperties() {
103103
p.setProperty("mlUseRoxyTokenPrefix", "false");
104104
p.setProperty("mlModulePaths", "path1,path2,path3");
105105
p.setProperty("mlModuleTimestampsPath", "custom/timestamps/path.properties");
106+
p.setProperty("mlDeleteTestModules", "true");
107+
p.setProperty("mlDeleteTestModulesPattern", "/some/pattern");
106108

107109
p.setProperty("mlModelsPath", "ml/models");
108110
p.setProperty("mlInstanceConverterPath", "ext/my/path");
@@ -149,6 +151,8 @@ public void allProperties() {
149151
assertEquals((Integer) 8123, config.getAppServicesPort());
150152
assertFalse(config.isReplaceTokensInModules());
151153
assertFalse(config.isUseRoxyTokenPrefix());
154+
assertTrue(config.isDeleteTestModules());
155+
assertEquals("/some/pattern", config.getDeleteTestModulesPattern());
152156

153157
assertEquals("ml/models", config.getModelsPath());
154158
assertEquals("ext/my/path", config.getInstanceConverterPath());

0 commit comments

Comments
 (0)