Hi there. I'm using sqlx
with Postgres as my database and want to implement a test wrapper around my database struct. The purpose of this wrapper is to create a temporary database, let user work with it, and then drop the database on the drop of the wrapper.
Part of the code:
// Create temporary database (make sure database user has
// enough priviliges).
let uuid = Uuid::new_v4().simple();
let name = format!("test_{}", uuid);
database
.pool
.execute(format!("CREATE DATABASE {};", &name).as_str())
.await?;
database
.pool
.execute(format!(r#"\connect {}"#, &name).as_str())
.await?;
sqlx::migrate!().run(&database.pool).await?;
The problem is, I can't execute \connect
using pool.execute
, it gives me this error:
Error: error returned from database: syntax error at or near "\"
Caused by:
syntax error at or near "\"
So I wonder, is it possible? If not, how else can I implement this functionality (besides connecting to the main database, creating new test database, disconnect from main database and connect to the new database)?