New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
qe: use one db per test on cockroachdb #3597
Changes from all commits
f292f1b
32ee6b9
3d37b56
d32ae2e
396397e
5c1862f
64f2e85
01ec1fb
e0c8b93
487d5e3
c58e944
2917d45
0417e15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use migration_core::migration_connector::{ConnectorError, ConnectorResult}; | ||
use quaint::{prelude::*, single::Quaint}; | ||
use url::Url; | ||
|
||
pub(crate) async fn cockroach_setup(url: String, prisma_schema: &str) -> ConnectorResult<()> { | ||
let mut url = Url::parse(&url).map_err(ConnectorError::url_parse_error)?; | ||
let quaint_url = quaint::connector::PostgresUrl::new(url.clone()).unwrap(); | ||
let db_name = quaint_url.dbname(); | ||
let conn = create_admin_conn(&mut url).await?; | ||
|
||
let query = format!( | ||
r#" | ||
DROP DATABASE IF EXISTS "{db_name}"; | ||
CREATE DATABASE "{db_name}"; | ||
"# | ||
); | ||
|
||
conn.raw_cmd(&query).await.unwrap(); | ||
crate::diff_and_apply(prisma_schema).await; | ||
|
||
drop_db_when_thread_exits(url, db_name); | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn create_admin_conn(url: &mut Url) -> ConnectorResult<Quaint> { | ||
url.set_path("/postgres"); | ||
Ok(Quaint::new(url.as_ref()).await.unwrap()) | ||
} | ||
|
||
fn drop_db_when_thread_exits(admin_url: Url, db_name: &str) { | ||
use crossbeam_channel::*; | ||
use once_cell::sync::OnceCell; | ||
use std::{cell::RefCell, thread}; | ||
use test_setup::runtime::run_with_thread_local_runtime as tok; | ||
|
||
// === Dramatis Personæ === | ||
|
||
// DB_DROP_THREAD: A thread that drops databases. | ||
static DB_DROP_THREAD: OnceCell<Sender<String>> = OnceCell::new(); | ||
|
||
let sender = DB_DROP_THREAD.get_or_init(|| { | ||
let (sender, receiver) = unbounded::<String>(); | ||
|
||
thread::spawn(move || { | ||
let mut admin_url = admin_url; | ||
let conn = tok(create_admin_conn(&mut admin_url)).unwrap(); | ||
loop { | ||
// Receive new databases to drop. | ||
for msg in receiver.iter() { | ||
tok(conn.raw_cmd(&msg)).unwrap(); | ||
} | ||
} | ||
}); | ||
|
||
sender | ||
}); | ||
|
||
// NOTIFIER: a thread local that notifies DB_DROP_THREAD when dropped. | ||
struct Notifier(String, Sender<String>); | ||
|
||
impl Drop for Notifier { | ||
fn drop(&mut self) { | ||
self.1.send(std::mem::take(&mut self.0)).unwrap() | ||
} | ||
} | ||
|
||
thread_local! { | ||
static NOTIFIER: RefCell<Option<Notifier>> = RefCell::new(None); | ||
} | ||
|
||
NOTIFIER.with(move |cell| { | ||
*cell.borrow_mut() = Some(Notifier(format!("DROP DATABASE \"{db_name}\""), sender.clone())); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ mod metrics { | |
Sqlite => assert_eq!(total_queries, 9), | ||
SqlServer(_) => assert_eq!(total_queries, 15), | ||
MongoDb(_) => assert_eq!(total_queries, 5), | ||
ConnectorVersion::CockroachDb => assert_eq!(total_queries, 10), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't dug to the bottom of this, but it would make sense for it to be from DROP DATABASE + CREATE DATABASE on test initialization becoming 1 query (instead of 2). |
||
_ => assert_eq!(total_queries, 11), | ||
} | ||
assert_eq!(total_operations, 2); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is an interesting addition - avoiding running the QE tests on IE only changes I assume?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, not reflected but it seems like a small win.