Skip to content

Commit 0b611a6

Browse files
committed
Add migration parameters
Issue launchbadge#3178 Added migration parameters in a similar fashion to goose's env var migration parameters. This still needs some tests and documentation. I wanted to get a proposal for a solution up for folks to take a look at.
1 parent e474be6 commit 0b611a6

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

Cargo.lock

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

sqlx-cli/src/migration.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::{bail, Context};
2+
use regex::Regex;
23
use console::style;
34
use std::fs::{self, File};
45
use std::io::{Read, Write};

sqlx-core/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ log = { version = "0.4.18", default-features = false }
6767
memchr = { version = "2.4.1", default-features = false }
6868
once_cell = "1.9.0"
6969
percent-encoding = "2.1.0"
70-
regex = { version = "1.5.5", optional = true }
70+
regex = { version = "1.5.5"}
7171
serde = { version = "1.0.132", features = ["derive", "rc"], optional = true }
7272
serde_json = { version = "1.0.73", features = ["raw_value"], optional = true }
7373
sha2 = { version = "0.10.0", default-features = false, optional = true }
@@ -82,6 +82,7 @@ hashlink = "0.10.0"
8282
indexmap = "2.0"
8383
event-listener = "5.2.0"
8484
hashbrown = "0.15.0"
85+
subst = "0.3.7"
8586

8687
[dev-dependencies]
8788
sqlx = { workspace = true, features = ["postgres", "sqlite", "mysql", "migrate", "macros", "time", "uuid"] }

sqlx-core/src/migrate/migration.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
use regex::{Captures, Regex};
12
use std::borrow::Cow;
3+
use std::sync::OnceLock;
24

35
use sha2::{Digest, Sha384};
46

57
use super::MigrationType;
68

9+
static ENV_SUB_REGEX: OnceLock<Regex> = OnceLock::new();
10+
711
#[derive(Debug, Clone)]
812
pub struct Migration {
913
pub version: i64,
@@ -23,12 +27,22 @@ impl Migration {
2327
no_tx: bool,
2428
) -> Self {
2529
let checksum = Cow::Owned(Vec::from(Sha384::digest(sql.as_bytes()).as_slice()));
26-
30+
let re = ENV_SUB_REGEX.get_or_init(|| {
31+
Regex::new(r"--\s?sqlx envsub on((.|\n|\r)*?)--\s?sqlx envsub off").unwrap()
32+
});
33+
let envsub_sql = re
34+
.replace_all(&sql, |cap: &Captures<'_>| {
35+
format!(
36+
"-- sqlx envsub on{}-- sqlx envsub off",
37+
subst::substitute(&cap[1], &subst::Env).unwrap_or(cap[1].to_owned())
38+
)
39+
})
40+
.into_owned();
2741
Migration {
2842
version,
2943
description,
3044
migration_type,
31-
sql,
45+
sql: Cow::Owned(envsub_sql),
3246
checksum,
3347
no_tx,
3448
}

0 commit comments

Comments
 (0)