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

Commit 31477e9

Browse files
committed
#308 Can now load schemas into specific databases
1 parent a98fc26 commit 31477e9

File tree

8 files changed

+82
-22
lines changed

8 files changed

+82
-22
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,10 @@ public File getDatabasesDir() {
4848
public List<File> getDatabaseResourceDirectories() {
4949
File dbDir = getDatabasesDir();
5050
if (dbDir != null && dbDir.exists()) {
51-
File[] dirs = dbDir.listFiles(new FileFilter() {
52-
@Override
53-
public boolean accept(File pathname) {
54-
return pathname.isDirectory();
55-
}
56-
});
51+
File[] dirs = dbDir.listFiles(pathname -> pathname.isDirectory());
5752
return Arrays.asList(dirs);
5853
}
59-
return new ArrayList<File>();
54+
return new ArrayList<>();
6055
}
6156

6257
protected void initializeContentDatabaseFiles() {

src/main/java/com/marklogic/appdeployer/command/schemas/LoadSchemasCommand.java

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.marklogic.client.ext.schemasloader.SchemasLoader;
1010
import com.marklogic.client.ext.schemasloader.impl.DefaultSchemasLoader;
1111

12+
import java.io.File;
1213
import java.io.FileFilter;
1314

1415
public class LoadSchemasCommand extends AbstractCommand {
@@ -19,35 +20,65 @@ public LoadSchemasCommand() {
1920

2021
@Override
2122
public void execute(CommandContext context) {
22-
loadSchemasIntoSchemasDatabase(context);
23+
loadSchemasFromSchemasPath(context);
24+
loadSchemasFromDatabaseSpecificPaths(context);
2325
}
2426

25-
protected void loadSchemasIntoSchemasDatabase(CommandContext context) {
27+
/**
28+
* This expects a single path to be defined in the AppConfig object. If set, then any files at that path are loaded
29+
* into the schemas database defined by the AppConfig object.
30+
*
31+
* @param context
32+
*/
33+
protected void loadSchemasFromSchemasPath(CommandContext context) {
2634
AppConfig config = context.getAppConfig();
27-
DatabaseClient client = config.newSchemasDatabaseClient();
28-
SchemasLoader schemasLoader = buildSchemasLoader(context, client);
35+
final String schemasPath = config.getSchemasPath();
36+
if (schemasPath != null && schemasPath.trim().length() > 0) {
37+
loadSchemas(config.getSchemasPath(), config.getSchemasDatabaseName(), context);
38+
} else {
39+
logger.info("Schemas path is empty, so not attempting to load any schemas");
40+
}
41+
}
42+
43+
/**
44+
* This loads schemas from every "databases/(name of schemas database)/schemas" path found in each configuration
45+
* directory in the AppConfig object.
46+
*
47+
* @param context
48+
*/
49+
protected void loadSchemasFromDatabaseSpecificPaths(CommandContext context) {
50+
context.getAppConfig().getConfigDirs().forEach(configDir -> {
51+
configDir.getDatabaseResourceDirectories().forEach(dir -> {
52+
File schemasDir = new File(dir, "schemas");
53+
if (schemasDir.exists()) {
54+
loadSchemas(schemasDir.getAbsolutePath(), dir.getName(), context);
55+
}
56+
});
57+
});
58+
}
59+
60+
protected void loadSchemas(String schemasPath, String schemasDatabaseName, CommandContext context) {
61+
logger.info(format("Loading schemas into database %s from: %s", schemasDatabaseName, schemasPath));
62+
DatabaseClient client = buildDatabaseClient(schemasDatabaseName, context);
2963
try {
30-
String schemasPath = config.getSchemasPath();
31-
if (schemasPath != null && schemasPath.trim().length() > 0) {
32-
logger.info("Loading schemas from path: " + schemasPath);
33-
schemasLoader.loadSchemas(schemasPath);
34-
logger.info("Finished loading schemas from: " + schemasPath);
35-
}
36-
else {
37-
logger.info("Schemas path is empty, so not attempting to load any schemas");
38-
}
64+
SchemasLoader schemasLoader = buildSchemasLoader(context, client);
65+
schemasLoader.loadSchemas(schemasPath);
66+
logger.info("Finished loading schemas from: " + schemasPath);
3967
} catch (FailedRequestException fre) {
4068
if (fre.getMessage().contains("NOSUCHDB")) {
4169
logger.warn("Unable to load schemas because no schemas database exists; cause: " + fre.getMessage());
42-
}
43-
else {
70+
} else {
4471
throw fre;
4572
}
4673
} finally {
4774
client.release();
4875
}
4976
}
5077

78+
protected DatabaseClient buildDatabaseClient(String schemasDatabaseName, CommandContext context) {
79+
return context.getAppConfig().newAppServicesDatabaseClient(schemasDatabaseName);
80+
}
81+
5182
/**
5283
* Will utilize schemasFileFilter in AppConfig if it's been set.
5384
*

src/test/java/com/marklogic/appdeployer/command/schemas/LoadSchemasTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@
1515

1616
public class LoadSchemasTest extends AbstractAppDeployerTest {
1717

18+
@Test
19+
public void databaseSpecificPaths() {
20+
initializeAppDeployer(new DeployOtherDatabasesCommand(), newCommand());
21+
22+
appConfig.getFirstConfigDir().setBaseDir(new File("src/test/resources/sample-app/multiple-schema-databases/ml-config"));
23+
appConfig.setSchemasDatabaseName("sample-app-schemas1");
24+
appConfig.setSchemasPath("src/test/resources/sample-app/multiple-schema-databases/ml-schemas");
25+
26+
deploySampleApp();
27+
28+
DatabaseClient client = appConfig.newSchemasDatabaseClient();
29+
GenericDocumentManager mgr = client.newDocumentManager();
30+
assertNotNull(mgr.exists("/default-schema.xsd"));
31+
assertNotNull(mgr.exists("/schema1.xsd"));
32+
assertNull(mgr.exists("/schema2.xsd"));
33+
client.release();
34+
35+
client = appConfig.newAppServicesDatabaseClient("sample-app-schemas2");
36+
mgr = client.newDocumentManager();
37+
assertNull(mgr.exists("/default-schema.xsd"));
38+
assertNull(mgr.exists("/schema1.xsd"));
39+
assertNotNull(mgr.exists("/schema2.xsd"));
40+
client.release();
41+
}
42+
1843
@Test
1944
public void testSchemaLoading() {
2045
initializeAppDeployer(new DeployOtherDatabasesCommand(),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"database-name": "sample-app-schemas1"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<schema1/>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"database-name": "sample-app-schemas2"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<schema2/>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<default-schema/>

0 commit comments

Comments
 (0)