Skip to content

Use minijinja instead of handlebars #3951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ flate2 = "1.0"
futures-channel = { version = "0.3.1", default-features = false }
futures-util = "0.3"
git2 = "0.13.0"
handlebars = "4.1.3"
hex = "0.4"
http = "0.2"
hyper = { version = "0.14", features = ["client", "http1"] }
indexmap = { version = "1.0.2", features = ["serde-1"] }
jemallocator = { version = "0.3", features = ['unprefixed_malloc_on_supported_platforms', 'profiling'] }
lettre = { version = "0.10.0-beta.3", default-features = false, features = ["file-transport", "smtp-transport", "native-tls", "hostname", "builder"] }
license-exprs = "1.6"
minijinja = "0.6.0"
oauth2 = { version = "4.0.0", default-features = false, features = ["reqwest"] }
parking_lot = "0.11"
prometheus = { version = "0.13.0", default-features = false }
Expand Down
9 changes: 0 additions & 9 deletions src/tasks/dump_db/dump-export.sql.hbs

This file was deleted.

9 changes: 9 additions & 0 deletions src/tasks/dump_db/dump-export.sql.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BEGIN ISOLATION LEVEL REPEATABLE READ, READ ONLY;
{% for table in tables %}
{% if table.filter %}
\copy (SELECT {{table.columns}} FROM "{{table.name}}" WHERE {{table.filter}}) TO 'data/{{table.name}}.csv' WITH CSV HEADER
{% else %}
\copy "{{table.name}}" ({{table.columns}}) TO 'data/{{table.name}}.csv' WITH CSV HEADER
{% endif %}
{% endfor %}
COMMIT;
38 changes: 0 additions & 38 deletions src/tasks/dump_db/dump-import.sql.hbs

This file was deleted.

38 changes: 38 additions & 0 deletions src/tasks/dump_db/dump-import.sql.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
BEGIN;
-- Disable triggers on each table.
{% for table in tables %}
ALTER TABLE "{{table.name}}" DISABLE TRIGGER ALL;
{% endfor %}

-- Set defaults for non-nullable columns not included in the dump.
{% for table in tables %}
{% for cd in table.column_defaults %}
ALTER TABLE "{{table.name}}" ALTER COLUMN "{{cd.column}}" SET DEFAULT {{cd.value}};
{% endfor %}
{% endfor %}

-- Truncate all tables.
{% for table in tables %}
TRUNCATE "{{table.name}}" RESTART IDENTITY CASCADE;
{% endfor %}

-- Enable this trigger so that `crates.textsearchable_index_col` can be excluded from the export
ALTER TABLE "crates" ENABLE TRIGGER "trigger_crates_tsvector_update";

-- Import the CSV data.
{% for table in tables %}
\copy "{{table.name}}" ({{table.columns}}) FROM 'data/{{table.name}}.csv' WITH CSV HEADER
{% endfor %}

-- Drop the defaults again.
{% for table in tables %}
{% for cd in table.column_defaults %}
ALTER TABLE "{{table.name}}" ALTER COLUMN "{{cd.column}}" DROP DEFAULT;
{% endfor %}
{% endfor %}

-- Reenable triggers on each table.
{% for table in tables %}
ALTER TABLE "{{table.name}}" ENABLE TRIGGER ALL;
{% endfor %}
COMMIT;
65 changes: 43 additions & 22 deletions src/tasks/dump_db/gen_scripts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, fs::File, path::Path};
use std::{fs::File, path::Path};

use crate::tasks::dump_db::configuration::{ColumnVisibility, TableConfig, VisibilityConfig};
use swirl::PerformError;
Expand All @@ -16,11 +16,17 @@ struct HandlebarsTableContext<'a> {
name: &'a str,
filter: Option<String>,
columns: String,
column_defaults: BTreeMap<&'a str, &'a str>,
column_defaults: Vec<ColumnDefault<'a>>,
}

#[derive(Debug, Serialize)]
struct ColumnDefault<'a> {
column: &'a str,
value: &'a str,
}

impl TableConfig {
fn handlebars_context<'a>(&'a self, name: &'a str) -> Option<HandlebarsTableContext<'a>> {
fn template_context<'a>(&'a self, name: &'a str) -> Option<HandlebarsTableContext<'a>> {
let columns = self
.columns
.iter()
Expand All @@ -35,7 +41,10 @@ impl TableConfig {
let column_defaults = self
.column_defaults
.iter()
.map(|(k, v)| (k.as_str(), v.as_str()))
.map(|(k, v)| ColumnDefault {
column: k.as_str(),
value: v.as_str(),
})
.collect();
Some(HandlebarsTableContext {
name,
Expand All @@ -49,41 +58,53 @@ impl TableConfig {

/// Subset of the configuration data to be passed on to the Handlbars template.
#[derive(Debug, Serialize)]
struct HandlebarsContext<'a> {
struct TemplateContext<'a> {
tables: Vec<HandlebarsTableContext<'a>>,
}

impl VisibilityConfig {
fn handlebars_context(&self) -> HandlebarsContext<'_> {
fn template_context(&self) -> TemplateContext<'_> {
let tables = self
.topological_sort()
.into_iter()
.filter_map(|table| self.0[table].handlebars_context(table))
.filter_map(|table| self.0[table].template_context(table))
.collect();
HandlebarsContext { tables }
TemplateContext { tables }
}

fn gen_psql_scripts<W>(&self, export_sql: W, import_sql: W) -> Result<(), PerformError>
fn gen_psql_scripts<W>(
&self,
mut export_writer: W,
mut import_writer: W,
) -> Result<(), PerformError>
where
W: std::io::Write,
{
let context = self.handlebars_context();
let mut handlebars = handlebars::Handlebars::new();
handlebars.register_escape_fn(handlebars::no_escape);
use minijinja::Environment;

let mut env = Environment::new();
env.add_template("dump-export.sql", include_str!("dump-export.sql.j2"))?;
env.add_template("dump-import.sql", include_str!("dump-import.sql.j2"))?;

let context = self.template_context();

debug!("Rendering dump-export.sql file…");
let export_sql = env
.get_template("dump-export.sql")
.unwrap()
.render(&context)?;

debug!("Rendering dump-import.sql file…");
let import_sql = env
.get_template("dump-import.sql")
.unwrap()
.render(&context)?;

debug!("Writing dump-export.sql file…");
handlebars.render_template_to_write(
include_str!("dump-export.sql.hbs"),
&context,
export_sql,
)?;
export_writer.write_all(export_sql.as_bytes())?;

debug!("Writing dump-import.sql file…");
handlebars.render_template_to_write(
include_str!("dump-import.sql.hbs"),
&context,
import_sql,
)?;
import_writer.write_all(import_sql.as_bytes())?;

Ok(())
}
Expand Down