Move SQL files to resources from java (#1879)

This is similar to where we store the SQL files for beam pipelines, and
frankly makes more sense. Also streamlined the use of the API to read
SQL files from a jar.
This commit is contained in:
Lai Jiang 2022-12-12 16:32:27 -05:00 committed by GitHub
parent 0b8e7d1120
commit 1242e3d50c
19 changed files with 8 additions and 24 deletions

View file

@ -18,9 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.io.Resources;
import google.registry.util.Clock; import google.registry.util.Clock;
import google.registry.util.ResourceUtils;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.avro.generic.GenericRecord; import org.apache.avro.generic.GenericRecord;
import org.apache.beam.sdk.io.gcp.bigquery.SchemaAndRecord; import org.apache.beam.sdk.io.gcp.bigquery.SchemaAndRecord;
@ -57,14 +55,6 @@ public class BeamUtils {
} }
} }
/**
* Returns the {@link String} contents for a file in the {@code sql/} directory relative to a
* class.
*/
public static String getQueryFromFile(Class<?> clazz, String filename) {
return ResourceUtils.readResourceUtf8(Resources.getResource(clazz, "sql/" + filename));
}
/** Creates a beam job name and validates that it conforms to the requirements. */ /** Creates a beam job name and validates that it conforms to the requirements. */
public static String createJobName(String prefix, Clock clock) { public static String createJobName(String prefix, Clock clock) {
// Flex template job name must be unique and consists of only characters [-a-z0-9], starting // Flex template job name must be unique and consists of only characters [-a-z0-9], starting

View file

@ -15,7 +15,6 @@
package google.registry.beam.invoicing; package google.registry.beam.invoicing;
import static com.google.common.collect.ImmutableSet.toImmutableSet; import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.beam.BeamUtils.getQueryFromFile;
import static org.apache.beam.sdk.values.TypeDescriptors.strings; import static org.apache.beam.sdk.values.TypeDescriptors.strings;
import com.google.common.flogger.FluentLogger; import com.google.common.flogger.FluentLogger;
@ -29,6 +28,7 @@ import google.registry.model.registrar.Registrar;
import google.registry.persistence.PersistenceModule.TransactionIsolationLevel; import google.registry.persistence.PersistenceModule.TransactionIsolationLevel;
import google.registry.reporting.billing.BillingModule; import google.registry.reporting.billing.BillingModule;
import google.registry.util.DomainNameUtils; import google.registry.util.DomainNameUtils;
import google.registry.util.ResourceUtils;
import google.registry.util.SqlTemplate; import google.registry.util.SqlTemplate;
import java.io.Serializable; import java.io.Serializable;
import java.time.YearMonth; import java.time.YearMonth;
@ -209,7 +209,8 @@ public class InvoicingPipeline implements Serializable {
YearMonth endMonth = YearMonth.parse(yearMonth).plusMonths(1); YearMonth endMonth = YearMonth.parse(yearMonth).plusMonths(1);
String queryWithComments = String queryWithComments =
SqlTemplate.create( SqlTemplate.create(
getQueryFromFile(InvoicingPipeline.class, "cloud_sql_billing_events.sql")) ResourceUtils.readResourceUtf8(
InvoicingPipeline.class, "sql/cloud_sql_billing_events.sql"))
.put("FIRST_TIMESTAMP_OF_MONTH", yearMonth + "-01") .put("FIRST_TIMESTAMP_OF_MONTH", yearMonth + "-01")
.put( .put(
"LAST_TIMESTAMP_OF_MONTH", "LAST_TIMESTAMP_OF_MONTH",

View file

@ -14,7 +14,6 @@
package google.registry.reporting.icann; package google.registry.reporting.icann;
import com.google.common.io.Resources;
import google.registry.util.ResourceUtils; import google.registry.util.ResourceUtils;
import google.registry.util.SqlTemplate; import google.registry.util.SqlTemplate;
import org.joda.time.YearMonth; import org.joda.time.YearMonth;
@ -28,9 +27,7 @@ public class BasicDnsCountQueryCoordinator implements DnsCountQueryCoordinator {
@Override @Override
public String createQuery(YearMonth yearMonth) { public String createQuery(YearMonth yearMonth) {
return SqlTemplate.create( return SqlTemplate.create(ResourceUtils.readResourceUtf8(this.getClass(), "sql/dns_counts.sql"))
ResourceUtils.readResourceUtf8(
Resources.getResource(this.getClass(), "sql/" + "dns_counts.sql")))
.build(); .build();
} }

View file

@ -142,7 +142,7 @@ public class GenerateSqlErDiagramCommand implements Command {
// Add pan and zoom support for the embedded SVG in the HTML. // Add pan and zoom support for the embedded SVG in the HTML.
StringBuilder svgPanZoomLib = StringBuilder svgPanZoomLib =
new StringBuilder("<script>") new StringBuilder("<script>")
.append(ResourceUtils.readResourceUtf8(Resources.getResource(SVG_PAN_ZOOM_LIB))) .append(ResourceUtils.readResourceUtf8(SVG_PAN_ZOOM_LIB))
.append("</script>"); .append("</script>");
doc.select("head").first().append(svgPanZoomLib.toString()); doc.select("head").first().append(svgPanZoomLib.toString());
doc.select("svg") doc.select("svg")
@ -207,8 +207,7 @@ public class GenerateSqlErDiagramCommand implements Command {
private static void initDb(Connection connection) { private static void initDb(Connection connection) {
try (Statement statement = connection.createStatement()) { try (Statement statement = connection.createStatement()) {
statement.execute( statement.execute(ResourceUtils.readResourceUtf8(NOMULUS_GOLDEN_SCHEMA));
ResourceUtils.readResourceUtf8(Resources.getResource(NOMULUS_GOLDEN_SCHEMA)));
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View file

@ -20,7 +20,6 @@ import static google.registry.tools.GenerateSqlErDiagramCommand.FLYWAY_FILE_ELEM
import static google.registry.tools.GenerateSqlErDiagramCommand.getLastFlywayFileName; import static google.registry.tools.GenerateSqlErDiagramCommand.getLastFlywayFileName;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.io.Resources;
import google.registry.util.ResourceUtils; import google.registry.util.ResourceUtils;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Path; import java.nio.file.Path;
@ -61,16 +60,14 @@ class GenerateSqlErDiagramCommandTest extends CommandTestCase<GenerateSqlErDiagr
void validateErDiagramIsUpToDate() { void validateErDiagramIsUpToDate() {
String goldenFullDiagram = String goldenFullDiagram =
ResourceUtils.readResourceUtf8( ResourceUtils.readResourceUtf8(
Resources.getResource( Paths.get(GOLDEN_DIAGRAM_FOLDER).resolve("full_er_diagram.html").toString());
Paths.get(GOLDEN_DIAGRAM_FOLDER).resolve("full_er_diagram.html").toString()));
assertWithMessage(UPDATE_INSTRUCTIONS) assertWithMessage(UPDATE_INSTRUCTIONS)
.that(Jsoup.parse(goldenFullDiagram).getElementById(FLYWAY_FILE_ELEMENT_ID).text()) .that(Jsoup.parse(goldenFullDiagram).getElementById(FLYWAY_FILE_ELEMENT_ID).text())
.isEqualTo(getLastFlywayFileName()); .isEqualTo(getLastFlywayFileName());
String briefFullDiagram = String briefFullDiagram =
ResourceUtils.readResourceUtf8( ResourceUtils.readResourceUtf8(
Resources.getResource( Paths.get(GOLDEN_DIAGRAM_FOLDER).resolve("brief_er_diagram.html").toString());
Paths.get(GOLDEN_DIAGRAM_FOLDER).resolve("brief_er_diagram.html").toString()));
assertWithMessage(UPDATE_INSTRUCTIONS) assertWithMessage(UPDATE_INSTRUCTIONS)
.that(Jsoup.parse(briefFullDiagram).getElementById(FLYWAY_FILE_ELEMENT_ID).text()) .that(Jsoup.parse(briefFullDiagram).getElementById(FLYWAY_FILE_ELEMENT_ID).text())
.isEqualTo(getLastFlywayFileName()); .isEqualTo(getLastFlywayFileName());