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

Commit 8f34412

Browse files
committed
#236 Can now customize the database that ES models are loaded into
1 parent d880712 commit 8f34412

File tree

5 files changed

+58
-21
lines changed

5 files changed

+58
-21
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ public class AppConfig {
199199
private boolean generateDatabaseProperties = true;
200200
private boolean generateExtractionTemplate = true;
201201
private boolean generateSearchOptions = true;
202+
private String modelsDatabase;
202203

203204
private String[] resourceFilenamesToIgnore;
204205
private Pattern resourceFilenamesExcludePattern;
@@ -1195,4 +1196,12 @@ public Integer getModulesLoaderBatchSize() {
11951196
public void setModulesLoaderBatchSize(Integer modulesLoaderBatchSize) {
11961197
this.modulesLoaderBatchSize = modulesLoaderBatchSize;
11971198
}
1199+
1200+
public String getModelsDatabase() {
1201+
return modelsDatabase;
1202+
}
1203+
1204+
public void setModelsDatabase(String modelsDatabase) {
1205+
this.modelsDatabase = modelsDatabase;
1206+
}
11981207
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,12 @@ public AppConfig newAppConfig() {
597597
/**
598598
* The following properties are all for generating Entity Services artifacts.
599599
*/
600+
prop = getProperty("mlModelsDatabase");
601+
if (prop != null) {
602+
logger.info("Entity Services models database: " + prop);
603+
c.setModelsDatabase(prop);
604+
}
605+
600606
prop = getProperty("mlModelsPath");
601607
if (prop != null) {
602608
logger.info("Entity Services models path: " + prop);

src/main/java/com/marklogic/appdeployer/command/es/GenerateModelArtifactsCommand.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.marklogic.client.ext.es.EntityServicesManager;
1111
import com.marklogic.client.ext.es.GeneratedCode;
1212
import org.springframework.util.FileCopyUtils;
13+
import org.springframework.util.StringUtils;
1314

1415
import java.io.File;
1516
import java.io.IOException;
@@ -39,23 +40,38 @@ public void execute(CommandContext context) {
3940
String modelsPath = appConfig.getModelsPath();
4041
File modelsDir = new File(modelsPath);
4142
if (modelsDir.exists()) {
42-
DatabaseClient client = appConfig.newDatabaseClient();
43-
EntityServicesManager mgr = new EntityServicesManager(client);
44-
for (File f : modelsDir.listFiles()) {
45-
GeneratedCode code = loadModelDefinition(appConfig, f, mgr);
43+
DatabaseClient client = buildDatabaseClient(appConfig);
44+
try {
45+
EntityServicesManager mgr = new EntityServicesManager(client);
46+
for (File f : modelsDir.listFiles()) {
47+
GeneratedCode code = loadModelDefinition(appConfig, f, mgr);
4648

47-
File modulesDir = selectModulesDir(appConfig);
48-
modulesDir.mkdirs();
49+
File modulesDir = selectModulesDir(appConfig);
50+
modulesDir.mkdirs();
4951

50-
generateInstanceConverter(appConfig, code, modulesDir);
51-
generateSearchOptions(code, modulesDir);
52-
generateDatabaseProperties(appConfig, code);
53-
generateSchema(appConfig, code);
54-
generateExtractionTemplate(appConfig, code);
52+
generateInstanceConverter(appConfig, code, modulesDir);
53+
generateSearchOptions(code, modulesDir);
54+
generateDatabaseProperties(appConfig, code);
55+
generateSchema(appConfig, code);
56+
generateExtractionTemplate(appConfig, code);
57+
}
58+
} finally {
59+
client.release();
5560
}
5661
}
5762
}
5863

64+
protected DatabaseClient buildDatabaseClient(AppConfig appConfig) {
65+
String db = appConfig.getModelsDatabase();
66+
if (StringUtils.isEmpty(db)) {
67+
db = appConfig.getContentDatabaseName();
68+
}
69+
if (logger.isInfoEnabled()) {
70+
logger.info("Will load Entity Services models into database: " + db);
71+
}
72+
return appConfig.newAppServicesDatabaseClient(db);
73+
}
74+
5975
protected GeneratedCode loadModelDefinition(AppConfig appConfig, File f, EntityServicesManager mgr) {
6076
return loadModelDefinition(buildCodeGenerationRequest(appConfig),f,mgr);
6177
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public void allProperties() {
141141
p.setProperty("mlModulesLoaderBatchSize", "79");
142142

143143
p.setProperty("mlModelsPath", "ml/models");
144+
p.setProperty("mlModelsDatabase", "my-models-database");
144145
p.setProperty("mlInstanceConverterPath", "ext/my/path");
145146
p.setProperty("mlGenerateInstanceConverter", "false");
146147
p.setProperty("mlGenerateDatabaseProperties", "false");
@@ -243,6 +244,7 @@ public void allProperties() {
243244
assertEquals(new Integer(79), config.getModulesLoaderBatchSize());
244245

245246
assertEquals("ml/models", config.getModelsPath());
247+
assertEquals("my-models-database", config.getModelsDatabase());
246248
assertEquals("ext/my/path", config.getInstanceConverterPath());
247249
assertFalse(config.isGenerateDatabaseProperties());
248250
assertFalse(config.isGenerateExtractionTemplate());

src/test/java/com/marklogic/appdeployer/command/es/GenerateModelArtifactsTest.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import com.marklogic.appdeployer.AbstractAppDeployerTest;
44
import com.marklogic.appdeployer.ConfigDir;
55
import com.marklogic.appdeployer.command.databases.DeployContentDatabasesCommand;
6+
import com.marklogic.appdeployer.command.databases.DeployOtherDatabasesCommand;
67
import com.marklogic.appdeployer.command.databases.DeploySchemasDatabaseCommand;
7-
import com.marklogic.appdeployer.command.modules.LoadModulesCommand;
88
import com.marklogic.appdeployer.command.restapis.DeployRestApiServersCommand;
9-
import com.marklogic.appdeployer.command.schemas.LoadSchemasCommand;
9+
import com.marklogic.client.DatabaseClient;
10+
import com.marklogic.client.io.StringHandle;
1011
import org.junit.After;
11-
import org.junit.Ignore;
1212
import org.junit.Test;
1313

1414
import java.io.File;
@@ -17,8 +17,6 @@ public class GenerateModelArtifactsTest extends AbstractAppDeployerTest {
1717

1818
@After
1919
public void tearDown() {
20-
initializeAppDeployer(new DeployContentDatabasesCommand(), new DeploySchemasDatabaseCommand(),
21-
new DeployRestApiServersCommand());
2220
undeploySampleApp();
2321
}
2422

@@ -34,8 +32,10 @@ public void test() {
3432
appConfig.getModulePaths().clear();
3533
appConfig.getModulePaths().add(projectPath + "/src/main/ml-modules");
3634
appConfig.setSchemasPath(projectPath + "/src/main/ml-schemas");
35+
appConfig.setModelsDatabase(appConfig.getContentDatabaseName() + "-other");
3736

3837
initializeAppDeployer(new DeployContentDatabasesCommand(1), new DeploySchemasDatabaseCommand(),
38+
new DeployOtherDatabasesCommand(1),
3939
new DeployRestApiServersCommand(), new GenerateModelArtifactsCommand());
4040
deploySampleApp();
4141

@@ -47,6 +47,15 @@ public void test() {
4747
assertTrue("A schemas db file needs to be created since the ES content-database.json file refers to one",
4848
new File(projectPath, "src/main/ml-config/databases/schemas-database.json").exists());
4949

50+
// Verify the model was loaded into the "other" database
51+
DatabaseClient modelsClient = appConfig.newAppServicesDatabaseClient(appConfig.getModelsDatabase());
52+
try {
53+
String raceModel = modelsClient.newDocumentManager().read("/marklogic.com/entity-services/models/race.json").nextContent(new StringHandle()).get();
54+
assertTrue("Simple smoke test to make sure the race model came back", raceModel.contains("This schema represents a Runner"));
55+
} finally {
56+
modelsClient.release();
57+
}
58+
5059
deploySampleApp();
5160

5261
// These shouldn't exist because the content is the same
@@ -55,10 +64,5 @@ public void test() {
5564
assertFalse(new File(projectPath, "src/main/ml-config/databases/content-database-GENERATED.json").exists());
5665
assertFalse(new File(projectPath, "src/main/ml-schemas/Race-0.0.1-GENERATED.xsd").exists());
5766
assertFalse(new File(projectPath, "src/main/ml-schemas/Race-0.0.1-GENERATED.tdex").exists());
58-
59-
// Make sure none of these files break when they're deployed
60-
initializeAppDeployer(new DeployContentDatabasesCommand(), new DeploySchemasDatabaseCommand(),
61-
new LoadSchemasCommand(), new LoadModulesCommand());
62-
//deploySampleApp();
6367
}
6468
}

0 commit comments

Comments
 (0)