@@ -4,6 +4,7 @@ use std::{
44 path:: Path ,
55} ;
66
7+ use rand:: { distr:: Alphanumeric , Rng } ;
78use sqlx:: { types:: chrono:: Utc , PgConnection } ;
89use tempfile:: tempdir;
910use 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+
97106pub 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