Skip to content

Commit 1ef4485

Browse files
committed
generate random separators
1 parent 0144c7f commit 1ef4485

3 files changed

Lines changed: 58 additions & 15 deletions

File tree

cli/Cargo.lock

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

cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ clap = { version = "4.5.42", features = ["derive"] }
1212
dirs = "6.0.0"
1313
futures = "0.3"
1414
postgrest = "1.5.0"
15+
rand = "0.9.2"
1516
regex = "1.11"
1617
reqwest = { version = "0.12.9", features = ["json", "native-tls-vendored"] }
1718
rpassword = "7.4.0"

cli/src/commands/add.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{
44
path::Path,
55
};
66

7+
use rand::{distr::Alphanumeric, Rng};
78
use sqlx::{types::chrono::Utc, PgConnection};
89
use tempfile::tempdir;
910
use tokio::fs;
@@ -94,6 +95,14 @@ async fn write_upgrade_files(
9495
Ok(())
9596
}
9697

98+
fn generate_random_ascii_string(length: usize) -> String {
99+
let rng = rand::rng();
100+
rng.sample_iter(Alphanumeric)
101+
.take(length)
102+
.map(char::from)
103+
.collect()
104+
}
105+
97106
pub async fn add(
98107
payload: &Payload,
99108
output_path: &Path,
@@ -151,6 +160,9 @@ pub async fn add(
151160
migration_content.push('\n');
152161
}
153162

163+
let random_string = generate_random_ascii_string(16); // 16 bytes = 128 bits entropy
164+
let sql_separator = format!("$sql_{random_string}$");
165+
let comment_separator = format!("$comment_{random_string}$");
154166
for install_file in &payload.install_files {
155167
if !existing_versions.contains(&install_file.version) {
156168
if installed_extension_once {
@@ -163,9 +175,9 @@ pub async fn add(
163175
migration_content.push_str(&extension_name);
164176
migration_content.push_str("', '");
165177
migration_content.push_str(&install_file.version);
166-
migration_content.push_str("', $sql$");
178+
migration_content.push_str(&format!("', {sql_separator}"));
167179
migration_content.push_str(&install_file.body);
168-
migration_content.push_str("$sql$);\n\n");
180+
migration_content.push_str(&format!("{sql_separator});\n\n"));
169181

170182
versions_installed_now.insert(install_file.version.clone());
171183
} else {
@@ -186,11 +198,11 @@ pub async fn add(
186198
migration_content.push_str(&extension_name);
187199
migration_content.push_str("', '");
188200
migration_content.push_str(&install_file.version);
189-
migration_content.push_str("', $comment$");
201+
migration_content.push_str(&format!("', {comment_separator}"));
190202
migration_content.push_str(payload.metadata.comment.as_deref().unwrap_or(""));
191-
migration_content.push_str("$comment$, $sql$");
203+
migration_content.push_str(&format!("{comment_separator}, {sql_separator}"));
192204
migration_content.push_str(&install_file.body);
193-
migration_content.push_str("$sql$, array[");
205+
migration_content.push_str(&format!("{sql_separator}, array["));
194206
migration_content.push_str(&requirements);
195207
migration_content.push_str("]::text[] );\n\n");
196208

@@ -217,9 +229,9 @@ pub async fn add(
217229
migration_content.push_str(&upgrade_file.from_version);
218230
migration_content.push_str("', '");
219231
migration_content.push_str(&upgrade_file.to_version);
220-
migration_content.push_str("', $sql$");
232+
migration_content.push_str(&format!("', {sql_separator}"));
221233
migration_content.push_str(&upgrade_file.body);
222-
migration_content.push_str("$sql$);\n\n");
234+
migration_content.push_str(&format!("{sql_separator});\n\n"));
223235
}
224236
}
225237

0 commit comments

Comments
 (0)