Skip to content

Commit bbdccd1

Browse files
committed
Use minijinja instead of handlebars
`minijinja` is explicitly built to be a light-weight templating system with minimal dependencies. Since we don't use a lot of the complexity that a full templating system like `handlebars` brings, we can reduce our number of transitive dependencies by replacing `handlebars` with `minijinja`. Note that it currently does not reduce the number of dependencies significantly, because other dependencies are still depending on these other transitive dependencies... 🙈
1 parent f4af5f5 commit bbdccd1

File tree

7 files changed

+89
-88
lines changed

7 files changed

+89
-88
lines changed

Cargo.lock

Lines changed: 10 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ flate2 = "1.0"
5858
futures-channel = { version = "0.3.1", default-features = false }
5959
futures-util = "0.3"
6060
git2 = "0.13.0"
61-
handlebars = "4.1.3"
6261
hex = "0.4"
6362
http = "0.2"
6463
hyper = { version = "0.14", features = ["client", "http1"] }
6564
indexmap = { version = "1.0.2", features = ["serde-1"] }
6665
jemallocator = { version = "0.3", features = ['unprefixed_malloc_on_supported_platforms', 'profiling'] }
6766
lettre = { version = "0.10.0-beta.3", default-features = false, features = ["file-transport", "smtp-transport", "native-tls", "hostname", "builder"] }
6867
license-exprs = "1.6"
68+
minijinja = "0.6.0"
6969
oauth2 = { version = "4.0.0", default-features = false, features = ["reqwest"] }
7070
parking_lot = "0.11"
7171
prometheus = { version = "0.13.0", default-features = false }

src/tasks/dump_db/dump-export.sql.hbs

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

src/tasks/dump_db/dump-export.sql.j2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
BEGIN ISOLATION LEVEL REPEATABLE READ, READ ONLY;
2+
{% for table in tables %}
3+
{% if table.filter %}
4+
\copy (SELECT {{table.columns}} FROM "{{table.name}}" WHERE {{table.filter}}) TO 'data/{{table.name}}.csv' WITH CSV HEADER
5+
{% else %}
6+
\copy "{{table.name}}" ({{table.columns}}) TO 'data/{{table.name}}.csv' WITH CSV HEADER
7+
{% endif %}
8+
{% endfor %}
9+
COMMIT;

src/tasks/dump_db/dump-import.sql.hbs

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

src/tasks/dump_db/dump-import.sql.j2

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
BEGIN;
2+
-- Disable triggers on each table.
3+
{% for table in tables %}
4+
ALTER TABLE "{{table.name}}" DISABLE TRIGGER ALL;
5+
{% endfor %}
6+
7+
-- Set defaults for non-nullable columns not included in the dump.
8+
{% for table in tables %}
9+
{% for cd in table.column_defaults %}
10+
ALTER TABLE "{{table.name}}" ALTER COLUMN "{{cd.column}}" SET DEFAULT {{cd.value}};
11+
{% endfor %}
12+
{% endfor %}
13+
14+
-- Truncate all tables.
15+
{% for table in tables %}
16+
TRUNCATE "{{table.name}}" RESTART IDENTITY CASCADE;
17+
{% endfor %}
18+
19+
-- Enable this trigger so that `crates.textsearchable_index_col` can be excluded from the export
20+
ALTER TABLE "crates" ENABLE TRIGGER "trigger_crates_tsvector_update";
21+
22+
-- Import the CSV data.
23+
{% for table in tables %}
24+
\copy "{{table.name}}" ({{table.columns}}) FROM 'data/{{table.name}}.csv' WITH CSV HEADER
25+
{% endfor %}
26+
27+
-- Drop the defaults again.
28+
{% for table in tables %}
29+
{% for cd in table.column_defaults %}
30+
ALTER TABLE "{{table.name}}" ALTER COLUMN "{{cd.column}}" DROP DEFAULT;
31+
{% endfor %}
32+
{% endfor %}
33+
34+
-- Reenable triggers on each table.
35+
{% for table in tables %}
36+
ALTER TABLE "{{table.name}}" ENABLE TRIGGER ALL;
37+
{% endfor %}
38+
COMMIT;

src/tasks/dump_db/gen_scripts.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct ColumnDefault<'a> {
2626
}
2727

2828
impl TableConfig {
29-
fn handlebars_context<'a>(&'a self, name: &'a str) -> Option<HandlebarsTableContext<'a>> {
29+
fn template_context<'a>(&'a self, name: &'a str) -> Option<HandlebarsTableContext<'a>> {
3030
let columns = self
3131
.columns
3232
.iter()
@@ -58,41 +58,53 @@ impl TableConfig {
5858

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

6565
impl VisibilityConfig {
66-
fn handlebars_context(&self) -> HandlebarsContext<'_> {
66+
fn template_context(&self) -> TemplateContext<'_> {
6767
let tables = self
6868
.topological_sort()
6969
.into_iter()
70-
.filter_map(|table| self.0[table].handlebars_context(table))
70+
.filter_map(|table| self.0[table].template_context(table))
7171
.collect();
72-
HandlebarsContext { tables }
72+
TemplateContext { tables }
7373
}
7474

75-
fn gen_psql_scripts<W>(&self, export_sql: W, import_sql: W) -> Result<(), PerformError>
75+
fn gen_psql_scripts<W>(
76+
&self,
77+
mut export_writer: W,
78+
mut import_writer: W,
79+
) -> Result<(), PerformError>
7680
where
7781
W: std::io::Write,
7882
{
79-
let context = self.handlebars_context();
80-
let mut handlebars = handlebars::Handlebars::new();
81-
handlebars.register_escape_fn(handlebars::no_escape);
83+
use minijinja::Environment;
84+
85+
let mut env = Environment::new();
86+
env.add_template("dump-export.sql", include_str!("dump-export.sql.j2"))?;
87+
env.add_template("dump-import.sql", include_str!("dump-import.sql.j2"))?;
88+
89+
let context = self.template_context();
90+
91+
debug!("Rendering dump-export.sql file…");
92+
let export_sql = env
93+
.get_template("dump-export.sql")
94+
.unwrap()
95+
.render(&context)?;
96+
97+
debug!("Rendering dump-import.sql file…");
98+
let import_sql = env
99+
.get_template("dump-import.sql")
100+
.unwrap()
101+
.render(&context)?;
82102

83103
debug!("Writing dump-export.sql file…");
84-
handlebars.render_template_to_write(
85-
include_str!("dump-export.sql.hbs"),
86-
&context,
87-
export_sql,
88-
)?;
104+
export_writer.write_all(export_sql.as_bytes())?;
89105

90106
debug!("Writing dump-import.sql file…");
91-
handlebars.render_template_to_write(
92-
include_str!("dump-import.sql.hbs"),
93-
&context,
94-
import_sql,
95-
)?;
107+
import_writer.write_all(import_sql.as_bytes())?;
96108

97109
Ok(())
98110
}

0 commit comments

Comments
 (0)