Skip to content

Commit 3a452e4

Browse files
committed
Stop generating whole profile, just create key schema files
1 parent e5f2d1a commit 3a452e4

File tree

6 files changed

+68
-10
lines changed

6 files changed

+68
-10
lines changed

sqrl-tools/sqrl-packager/src/main/java/com/datasqrl/packager/Packager.java

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
import freemarker.template.Configuration;
3131
import freemarker.template.DefaultMapAdapter;
3232
import freemarker.template.Template;
33+
import freemarker.template.TemplateException;
3334
import freemarker.template.TemplateExceptionHandler;
3435
import freemarker.template.TemplateMethodModelEx;
3536
import freemarker.template.TemplateModelException;
3637
import java.io.StringWriter;
3738
import java.io.Writer;
39+
import java.nio.charset.StandardCharsets;
3840
import java.nio.file.FileVisitResult;
3941
import java.nio.file.SimpleFileVisitor;
4042
import java.nio.file.attribute.BasicFileAttributes;
@@ -43,8 +45,11 @@
4345
import lombok.SneakyThrows;
4446
import org.apache.commons.lang3.StringUtils;
4547

48+
import static java.nio.charset.StandardCharsets.UTF_8;
49+
4650
import java.io.File;
4751
import java.io.IOException;
52+
import java.io.OutputStreamWriter;
4853
import java.nio.file.Files;
4954
import java.nio.file.Path;
5055
import java.nio.file.StandardCopyOption;
@@ -323,17 +328,64 @@ public void postprocess(PackageJson sqrlConfig, Path rootDir, Path targetDir, Ph
323328
? rootDir.resolve(profile)
324329
: namepath2Path(buildDir.getBuildDir(), NamePath.parse(profile));
325330

326-
copyToDeploy(targetDir, profilePath, plan, testPlan, sqrlConfig, plans);
331+
// copyToDeploy(targetDir, profilePath, plan, testPlan, sqrlConfig, plans);
327332
}
328333

329334
copyDataFiles(buildDir.getBuildDir());
330335
moveFolder(targetDir, DATA_DIR);
331336
copyJarFiles(buildDir.getBuildDir());
332337
moveFolder(targetDir, LIB_DIR);
333338
copyCompiledPlan(buildDir.getBuildDir(), targetDir);
339+
340+
// copy deployment files
341+
Map<String, Object> config = collectConfiguration(sqrlConfig, plans);
342+
writePostgresSchema(targetDir, config);
343+
}
344+
345+
private void writePostgresSchema(Path targetDir, Map<String, Object> config) {
346+
if(config.containsKey("postgres") || config.containsKey("postgres_log")) {
347+
//postgres
348+
copyTemplate(targetDir, config, "templates/database-schema.sql.ftl", "postgres/database-schema.sql");
349+
copyTemplate(targetDir, config, "templates/database-schema.sql.ftl", "files/postgres-schema.sql");
350+
}
351+
352+
if(config.containsKey("flink")) {
353+
//flink
354+
copyTemplate(targetDir, config, "templates/flink.sql.ftl", "flink/src/main/resources/flink.sql");
355+
copyTemplate(targetDir, config, "templates/flink.sql.ftl", "files/flink.sql");
356+
}
357+
358+
if(config.containsKey("vertx")) {
359+
//vertx server-config
360+
copyTemplate(targetDir, config, "templates/server-config.json.ftl", "vertx/server-config.json");
361+
copyTemplate(targetDir, config, "templates/server-config.json.ftl", "files/vertx-config.json");
362+
363+
//vertx server-config
364+
copyTemplate(targetDir, config, "templates/server-model.json.ftl", "vertx/server-model.json");
365+
copyTemplate(targetDir, config, "templates/server-model.json.ftl", "files/vertx-model.json");
366+
}
334367
}
335368

336369
@SneakyThrows
370+
private void copyTemplate(Path targetDir, Map<String, Object> config, String source, String destination) {
371+
// Set up the FreeMarker configuration to load templates from the classpath.
372+
Configuration cfg = new Configuration(Configuration.VERSION_2_3_32);
373+
cfg.setDefaultEncoding("UTF-8");
374+
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
375+
cfg.setNumberFormat("computer");
376+
cfg.setSharedVariable("jsonEncode", new JsonEncoderMethod()); // Set the base directory for templates to the root of the classpath.
377+
cfg.setClassLoaderForTemplateLoading(getClass().getClassLoader(), "/");
378+
379+
Template template = cfg.getTemplate(source);
380+
381+
Path postgresSchemaFile = targetDir.resolve(destination);
382+
Files.createDirectories(postgresSchemaFile.getParent());
383+
try (Writer writer = new OutputStreamWriter(Files.newOutputStream(postgresSchemaFile), StandardCharsets.UTF_8)) {
384+
template.process(config, writer);
385+
}
386+
}
387+
388+
@SneakyThrows
337389
private void copyCompiledPlan(Path buildDir, Path targetDir) {
338390
if (Files.exists(buildDir.resolve(COMPILED_PLAN_JSON))) {
339391
Path destFolder = targetDir.resolve("flink");
@@ -422,10 +474,7 @@ private void copyToDeploy(Path targetDir, Path profile, PhysicalPlan plan, TestP
422474
Files.createDirectories(targetDir);
423475
}
424476

425-
Map<String, Object> templateConfig = new HashMap<>();
426-
templateConfig.put("config", sqrlConfig.toMap()); //Add SQRL config
427-
templateConfig.put("environment", System.getenv()); //Add environmental variables
428-
templateConfig.putAll(plans);
477+
Map<String, Object> templateConfig = collectConfiguration(sqrlConfig, plans);
429478
// Copy each file and directory from the profile path to the target directory
430479
if (!Files.isDirectory(profile)) {
431480
throw new RuntimeException("Could not find profile: " + profile);
@@ -455,6 +504,14 @@ private void copyToDeploy(Path targetDir, Path profile, PhysicalPlan plan, TestP
455504
}
456505
}
457506

507+
private Map<String, Object> collectConfiguration(PackageJson sqrlConfig, Map<String, Object> plans) {
508+
Map<String, Object> templateConfig = new HashMap<>();
509+
templateConfig.put("config", sqrlConfig.toMap()); //Add SQRL config
510+
templateConfig.put("environment", System.getenv()); //Add environmental variables
511+
templateConfig.putAll(plans);
512+
return templateConfig;
513+
}
514+
458515
private Path trimFtl(Path destinationPath) {
459516
return destinationPath.getFileName().toString().endsWith(".ftl") ?
460517
destinationPath.getParent().resolve(destinationPath.getFileName().toString().substring(0,destinationPath.getFileName().toString().length()-4 ))
@@ -490,18 +547,17 @@ public void processTemplate(Path path, Path destination, Map config) throws Exce
490547
return;
491548
}
492549

550+
// extract the template filename
551+
String templateName = path.getFileName().toString();
552+
493553
// configure Freemarker
494554
Configuration cfg = new Configuration(Configuration.VERSION_2_3_32);
495555
cfg.setDirectoryForTemplateLoading(path.getParent().toFile());
496556
cfg.setDefaultEncoding("UTF-8");
497557
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
498558
cfg.setNumberFormat("computer");
499-
500559
cfg.setSharedVariable("jsonEncode", new JsonEncoderMethod());
501560

502-
// extract the template filename
503-
String templateName = path.getFileName().toString();
504-
505561
// load and process the template
506562
Template template = cfg.getTemplate(templateName);
507563
Writer out = new StringWriter();

profiles/default/vertx/server-config.json renamed to sqrl-tools/sqrl-packager/src/main/resources/templates/server-config.json.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,13 @@
126126
"reconnectAttempts": 0,
127127
"reconnectInterval": 1000,
128128
"hostnameVerificationAlgorithm": "",
129+
<#noparse>
129130
"host": "${PGHOST}",
130131
"port": 5432,
131132
"user": "${PGUSER}",
132133
"password": "${PGPASSWORD}",
133134
"database": "${PGDATABASE}",
135+
</#noparse>
134136
"cachePreparedStatements": false,
135137
"preparedStatementCacheMaxSize": 256,
136138
"preparedStatementCacheSqlFilter": {},

sqrl-tools/sqrl-run/src/main/java/com/datasqrl/DatasqrlRun.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public CompiledPlan compileFlink() {
209209

210210
Path flinkPath = path.resolve("flink.json");
211211
if (!flinkPath.toFile().exists()) {
212-
throw new RuntimeException("Could not find flink plan.");
212+
throw new RuntimeException("Could not find flink plan: " + flinkPath);
213213
}
214214

215215
Map map = objectMapper.readValue(path.resolve("flink.json").toFile(), Map.class);

0 commit comments

Comments
 (0)