@@ -103,10 +103,8 @@ impl PostgresConnection {
103
103
. is_none ( )
104
104
{
105
105
// User does not exist, ensure we create it.
106
- let create_role_sql = format ! ( "CREATE ROLE {} NOCREATEDB NOCREATEROLE NOINHERIT LOGIN NOREPLICATION NOBYPASSRLS CONNECTION LIMIT 8 PASSWORD $1;" , role_name) ;
107
- self . client
108
- . execute ( & create_role_sql, & [ & role_name, & role_password] )
109
- . await ?;
106
+ let create_role_sql = format ! ( "CREATE ROLE {} NOCREATEDB NOCREATEROLE NOINHERIT LOGIN NOREPLICATION NOBYPASSRLS CONNECTION LIMIT 8 PASSWORD '{}'" , role_name, single_quote_escape( role_password) ) ;
107
+ self . client . execute ( & create_role_sql, & [ ] ) . await ?;
110
108
} ;
111
109
112
110
// Same for database, only create if not existent yet.
@@ -149,6 +147,10 @@ fn assert_valid_ident(input: &str) -> Result<(), Error> {
149
147
}
150
148
}
151
149
150
+ fn single_quote_escape ( input : & str ) -> String {
151
+ input. replace ( '\'' , "''" )
152
+ }
153
+
152
154
#[ cfg( test) ]
153
155
mod tests {
154
156
use super :: PostgresDb ;
@@ -180,12 +182,23 @@ mod tests {
180
182
su. create_database ( "unittest" , "unittest" )
181
183
. expect ( "should be able to create db" ) ;
182
184
183
- let db = dbg ! ( PostgresDb :: new(
184
- pg. as_user( "unittest" , "unittest" ) . uri( "postgres" )
185
- ) ) ;
185
+ let db = PostgresDb :: new ( pg. as_user ( "unittest" , "unittest" ) . uri ( "postgres" ) ) ;
186
186
let con = db. connect ( ) . await . expect ( "failed to connect" ) ;
187
187
con. run_self_check ( )
188
188
. await
189
189
. expect_err ( "self-check should fail" ) ;
190
190
}
191
+
192
+ #[ tokio:: test]
193
+ async fn can_create_db_for_instance ( ) {
194
+ let pg = mk_pg ( ) ;
195
+ let su = pg. as_superuser ( ) ;
196
+
197
+ let db = PostgresDb :: new ( su. uri ( "postgres" ) ) ;
198
+
199
+ let con = db. connect ( ) . await . expect ( "failed to connect" ) ;
200
+ con. create_instance ( "myrole" , "mypw" , "mydb" )
201
+ . await
202
+ . expect ( "failed to create instance" ) ;
203
+ }
191
204
}
0 commit comments