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

Commit e7d7228

Browse files
committed
#128 Stubbing out schemas database file when generating model artifacts
1 parent 286dd2e commit e7d7228

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,4 @@ src/test/resources/user.properties
1010
setenv.bat
1111
src/test/resources/scaffold-test/
1212
.DS_Store
13-
src/test/resources/entity-services-project/src/main/ml-modules
14-
src/test/resources/entity-services-project/src/main/ml-schemas
15-
src/test/resources/entity-services-project/src/main/ml-config/databases/content-database-GENERATED.json
16-
13+
src/test/resources/entity-services-project/src

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

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import com.marklogic.appdeployer.command.AbstractCommand;
55
import com.marklogic.appdeployer.command.CommandContext;
66
import com.marklogic.appdeployer.command.SortOrderConstants;
7+
import com.marklogic.appdeployer.command.databases.DeploySchemasDatabaseCommand;
78
import com.marklogic.client.DatabaseClient;
89
import com.marklogic.client.es.CodeGenerationRequest;
910
import com.marklogic.client.es.EntityServicesManager;
1011
import com.marklogic.client.es.GeneratedCode;
11-
import com.sun.org.apache.bcel.internal.classfile.Code;
1212
import org.springframework.util.FileCopyUtils;
1313

1414
import java.io.File;
@@ -97,13 +97,15 @@ protected void generateInstanceConverter(AppConfig appConfig, GeneratedCode code
9797
File esDir = new File(modulesDir, appConfig.getInstanceConverterPath());
9898
esDir.mkdirs();
9999
File out = new File(esDir, code.getTitle() + "-" + code.getVersion() + ".xqy");
100+
String logMessage = "Wrote instance converter to: ";
100101
if (out.exists()) {
101102
out = new File(esDir, code.getTitle() + "-" + code.getVersion() + "-GENERATED.xqy");
103+
logMessage = "Instance converter file already exists; writing new generated copy to: ";
102104
}
103105
try {
104106
FileCopyUtils.copy(instanceConverter.getBytes(), out);
105107
if (logger.isInfoEnabled()) {
106-
logger.info("Wrote instance converter to: " + out.getAbsolutePath());
108+
logger.info(logMessage + out.getAbsolutePath());
107109
}
108110
} catch (IOException e) {
109111
throw new RuntimeException("Unable to write instance converter to: " + out.getAbsolutePath(), e);
@@ -117,33 +119,55 @@ protected void generateSearchOptions(GeneratedCode code, File modulesDir) {
117119
File optionsDir = new File(modulesDir, optionsPath);
118120
optionsDir.mkdirs();
119121
File out = new File(optionsDir, code.getTitle() + ".xml");
122+
String logMessage = "Wrote search options to: ";
120123
if (out.exists()) {
121124
out = new File(optionsDir, code.getTitle() + "-GENERATED.xml");
125+
logMessage = "Search options file already exists; writing new generated copy to: ";
122126
}
123127
try {
124128
FileCopyUtils.copy(searchOptions.getBytes(), out);
125129
if (logger.isInfoEnabled()) {
126-
logger.info("Wrote search options to: " + out.getAbsolutePath());
130+
logger.info(logMessage + out.getAbsolutePath());
127131
}
128132
} catch (IOException e) {
129133
throw new RuntimeException("Unable to write search options to file: " + out.getAbsolutePath(), e);
130134
}
131135
}
132136
}
133137

138+
/**
139+
* The content-database.json file generated by Entity Services has a reference to a schemas database. So if a config
140+
* file for a schemas database doesn't exist, one is generated so that the next deployment succeeds.
141+
*
142+
* @param appConfig
143+
* @param code
144+
*/
134145
protected void generateDatabaseProperties(AppConfig appConfig, GeneratedCode code) {
135146
String props = code.getDatabaseProperties();
136147
if (props != null) {
137-
File dir = appConfig.getConfigDir().getDatabasesDir();
138-
dir.mkdirs();
139-
File out = new File(dir, "content-database.json");
148+
File dbDir = appConfig.getConfigDir().getDatabasesDir();
149+
dbDir.mkdirs();
150+
File out = new File(dbDir, "content-database.json");
151+
String logMessage = "Wrote database properties to: ";
140152
if (out.exists()) {
141-
out = new File(dir, "content-database-GENERATED.json");
153+
out = new File(dbDir, "content-database-GENERATED.json");
154+
logMessage = "Database properties file already exists; writing new generated copy to: ";
142155
}
143156
try {
144157
FileCopyUtils.copy(props.getBytes(), out);
145158
if (logger.isInfoEnabled()) {
146-
logger.info("Wrote database properties to: " + out.getAbsolutePath());
159+
logger.info(logMessage + out.getAbsolutePath());
160+
}
161+
162+
// Makes some assumptions about the schemas file
163+
File schemasFile = new File(dbDir, DeploySchemasDatabaseCommand.DATABASE_FILENAME);
164+
if (!schemasFile.exists()) {
165+
String payload = "{\"database-name\": \"%%SCHEMAS_DATABASE%%\"}";
166+
try {
167+
FileCopyUtils.copy(payload.getBytes(), schemasFile);
168+
} catch (IOException ex) {
169+
logger.warn("Unable to write schemas database payload to file: " + schemasFile.getAbsolutePath(), ex);
170+
}
147171
}
148172
} catch (IOException e) {
149173
throw new RuntimeException("Unable to write database properties to file: " + out.getAbsolutePath(), e);
@@ -157,13 +181,15 @@ protected void generateSchema(AppConfig appConfig, GeneratedCode code) {
157181
File dir = new File(appConfig.getSchemasPath());
158182
dir.mkdirs();
159183
File out = new File(dir, code.getTitle() + "-" + code.getVersion() + ".xsd");
184+
String logMessage = "Wrote schema to: ";
160185
if (out.exists()) {
161186
out = new File(dir, code.getTitle() + "-" + code.getVersion() + "-GENERATED.xsd");
187+
logMessage = "Schema file already exists; writing new generated copy to: ";
162188
}
163189
try {
164190
FileCopyUtils.copy(schema.getBytes(), out);
165191
if (logger.isInfoEnabled()) {
166-
logger.info("Wrote schema to: " + out.getAbsolutePath());
192+
logger.info(logMessage + out.getAbsolutePath());
167193
}
168194
} catch (IOException e) {
169195
throw new RuntimeException("Unable to write schema to file: " + out.getAbsolutePath(), e);
@@ -177,13 +203,15 @@ protected void generateExtractionTemplate(AppConfig appConfig, GeneratedCode cod
177203
File dir = new File(appConfig.getSchemasPath());
178204
dir.mkdirs();
179205
File out = new File(dir, code.getTitle() + "-" + code.getVersion() + ".tdex");
206+
String logMessage = "Wrote extraction template to: ";
180207
if (out.exists()) {
181208
out = new File(dir, code.getTitle() + "-" + code.getVersion() + "-GENERATED.tdex");
209+
logMessage = "Extraction template already exists; writing new generated copy to: ";
182210
}
183211
try {
184212
FileCopyUtils.copy(template.getBytes(), out);
185213
if (logger.isInfoEnabled()) {
186-
logger.info("Wrote extraction template to: " + out.getAbsolutePath());
214+
logger.info(logMessage + out.getAbsolutePath());
187215
}
188216
} catch (IOException e) {
189217
throw new RuntimeException("Unable to write extraction template to file: " + out.getAbsolutePath(), e);

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public void tearDown() {
2424
@Test
2525
public void test() {
2626
String projectPath = "src/test/resources/entity-services-project";
27+
File srcDir = new File(projectPath, "src");
28+
if (srcDir.exists()) {
29+
srcDir.delete();
30+
}
2731
appConfig.setConfigDir(new ConfigDir(new File(projectPath + "/src/main/ml-config")));
2832
appConfig.setModelsPath(projectPath + "/data/entity-services");
2933
appConfig.getModulePaths().clear();
@@ -38,8 +42,9 @@ public void test() {
3842
assertTrue(new File(projectPath, "src/main/ml-modules/options/Race.xml").exists());
3943
assertTrue(new File(projectPath, "src/main/ml-schemas/Race-0.0.1.xsd").exists());
4044
assertTrue(new File(projectPath, "src/main/ml-schemas/Race-0.0.1.tdex").exists());
41-
// We already have a content-database.json file
42-
assertTrue(new File(projectPath, "src/main/ml-config/databases/content-database-GENERATED.json").exists());
45+
assertTrue(new File(projectPath, "src/main/ml-config/databases/content-database.json").exists());
46+
assertTrue("A schemas db file needs to be created since the ES content-database.json file refers to one",
47+
new File(projectPath, "src/main/ml-config/databases/schemas-database.json").exists());
4348

4449
deploySampleApp();
4550

@@ -50,7 +55,8 @@ public void test() {
5055
assertTrue(new File(projectPath, "src/main/ml-schemas/Race-0.0.1-GENERATED.tdex").exists());
5156

5257
// Make sure none of these files break when they're deployed
53-
initializeAppDeployer(new DeployContentDatabasesCommand(), new LoadSchemasCommand(), new LoadModulesCommand());
58+
initializeAppDeployer(new DeployContentDatabasesCommand(), new DeploySchemasDatabaseCommand(),
59+
new LoadSchemasCommand(), new LoadModulesCommand());
5460
deploySampleApp();
5561
}
5662
}

src/test/resources/entity-services-project/src/main/ml-config/databases/content-database.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/test/resources/entity-services-project/src/main/ml-config/databases/schemas-database.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)