Skip to content

Commit 7072305

Browse files
committed
Auto merge of #2144 - jtgeibel:prod/fix-db-dump-fallback, r=smarnach
Allow database dumps to be run against the read-only replica Edit: The description has been updated, with the original message included below. This PR updates the database dump script so that it can be run against the read-only replica database. I've tested locally, but we can manually schedule a database dump against the read-only replica and then update the existing daily scheduled task. ## Original PR Description In production, the scheduler was configured to run: ``` ./target/release/enqueue-job dump_db $READ_ONLY_REPLICA ``` instead of: ``` ./target/release/enqueue-job dump_db $READ_ONLY_REPLICA_URL ``` Because of the typo in the variable name, the dump_db job has been running against the primary database. This patch modifies the fallback to ensure the primary database is only used if explicitly provided.
2 parents 8e7ca34 + 4ae90e6 commit 7072305

File tree

3 files changed

+5
-17
lines changed

3 files changed

+5
-17
lines changed

src/bin/enqueue-job.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn main() -> Result<(), Error> {
1313
match &*job {
1414
"update_downloads" => Ok(tasks::update_downloads().enqueue(&conn)?),
1515
"dump_db" => {
16-
let database_url = args.next().unwrap_or_else(|| env("DATABASE_URL"));
16+
let database_url = args.next().unwrap_or_else(|| env("READ_ONLY_REPLICA_URL"));
1717
let target_name = args
1818
.next()
1919
.unwrap_or_else(|| String::from("db-dump.tar.gz"));

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
BEGIN;
1+
BEGIN ISOLATION LEVEL REPEATABLE READ, READ ONLY;
22
{{~#each tables}}
33
{{~#if this.filter}}
4-
CREATE TEMPORARY VIEW "dump_db_{{this.name}}" AS (
5-
SELECT {{this.columns}}
6-
FROM "{{this.name}}"
7-
WHERE {{this.filter}}
8-
);
9-
{{~/if}}
10-
{{~/each}}
11-
COMMIT;
12-
13-
BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE;
14-
{{~#each tables}}
15-
{{~#if this.filter}}
16-
\copy (SELECT * FROM "dump_db_{{this.name}}") TO 'data/{{this.name}}.csv' WITH CSV HEADER
4+
\copy (SELECT {{this.columns}} FROM "{{this.name}}" WHERE {{this.filter}}) TO 'data/{{this.name}}.csv' WITH CSV HEADER
175
{{~else}}
186
\copy "{{this.name}}" ({{this.columns}}) TO 'data/{{this.name}}.csv' WITH CSV HEADER
197
{{~/if}}

src/tasks/dump_db/gen_scripts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct TableConfig {
4242
#[derive(Debug, Serialize)]
4343
struct HandlebarsTableContext<'a> {
4444
name: &'a str,
45-
filter: Option<&'a str>,
45+
filter: Option<String>,
4646
columns: String,
4747
column_defaults: BTreeMap<&'a str, &'a str>,
4848
}
@@ -59,7 +59,7 @@ impl TableConfig {
5959
if columns.is_empty() {
6060
None
6161
} else {
62-
let filter = self.filter.as_ref().map(String::as_str);
62+
let filter = self.filter.as_ref().map(|s| s.replace('\n', " "));
6363
let column_defaults = self
6464
.column_defaults
6565
.iter()

0 commit comments

Comments
 (0)