Skip to content

Commit 8943f05

Browse files
committed
Update role creation code to not use bind params
1 parent 1eaa467 commit 8943f05

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/postgres.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,8 @@ impl PostgresConnection {
103103
.is_none()
104104
{
105105
// 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?;
110108
};
111109

112110
// Same for database, only create if not existent yet.
@@ -149,6 +147,10 @@ fn assert_valid_ident(input: &str) -> Result<(), Error> {
149147
}
150148
}
151149

150+
fn single_quote_escape(input: &str) -> String {
151+
input.replace('\'', "''")
152+
}
153+
152154
#[cfg(test)]
153155
mod tests {
154156
use super::PostgresDb;
@@ -180,12 +182,23 @@ mod tests {
180182
su.create_database("unittest", "unittest")
181183
.expect("should be able to create db");
182184

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"));
186186
let con = db.connect().await.expect("failed to connect");
187187
con.run_self_check()
188188
.await
189189
.expect_err("self-check should fail");
190190
}
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+
}
191204
}

0 commit comments

Comments
 (0)