Debian Codegraph @

src/testing/mod.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#;
2 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#;
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#;
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::{AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#};
5 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::{Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#, SystemTimerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/SystemTime#};
6
7 use futures_corerust-analyzer cargo futures-core 0.3.34 crate/::futurerust-analyzer cargo futures-core 0.3.34 future/::BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#;
8
9 use once_cellrust-analyzer cargo once_cell 1.21.4 crate/::syncrust-analyzer cargo once_cell 1.21.4 sync/::OnceCellrust-analyzer cargo once_cell 1.21.4 sync/OnceCell#;
10
11 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::connectionrust-analyzer cargo sqlx-mysql 0.8.3 connection/::Connectionrust-analyzer cargo sqlx-core 0.8.3 connection/Connection#;
12
13 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::errorrust-analyzer cargo sqlx-mysql 0.8.3 error/::Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#;
14 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::executorrust-analyzer cargo sqlx-core 0.8.3 executor/::Executorrust-analyzer cargo sqlx-core 0.8.3 executor/Executor#;
15 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::poolrust-analyzer cargo sqlx-core 0.8.3 pool/::{Poolrust-analyzer cargo sqlx-core 0.8.3 pool/Pool#, PoolOptionsrust-analyzer cargo sqlx-core 0.8.3 pool/options/PoolOptions#};
16 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::queryrust-analyzer cargo sqlx-core 0.8.3 query/::queryrust-analyzer cargo sqlx-core 0.8.3 query/query().;
17 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::query_builderrust-analyzer cargo sqlx-core 0.8.3 query_builder/::QueryBuilderrust-analyzer cargo sqlx-core 0.8.3 query_builder/QueryBuilder#;
18 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/::query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().;
19 use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::{MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#, MySqlConnectOptionsrust-analyzer cargo sqlx-mysql 0.8.3 options/MySqlConnectOptions#, MySqlConnectionrust-analyzer cargo sqlx-mysql 0.8.3 connection/MySqlConnection#};
20
21 pub(craterust-analyzer cargo sqlx-mysql 0.8.3 crate/) use sqlx_corerust-analyzer cargo sqlx-core 0.8.3 crate/::testingrust-analyzer cargo sqlx-core 0.8.3 testing/::*;
22
23 // Using a blocking `OnceCell` here because the critical sections are short.
24 static MASTER_POOLrust-analyzer cargo sqlx-mysql 0.8.3 testing/MASTER_POOL.: OnceCellrust-analyzer cargo once_cell 1.21.4 sync/OnceCell#<Poolrust-analyzer cargo sqlx-core 0.8.3 pool/Pool#<MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#>> = OnceCellrust-analyzer cargo once_cell 1.21.4 sync/OnceCell#::newrust-analyzer cargo once_cell 1.21.4 sync/impl#[`OnceCell<T>`]new().();
25 // Automatically delete any databases created before the start of the test binary.
26 static DO_CLEANUPrust-analyzer cargo sqlx-mysql 0.8.3 testing/DO_CLEANUP.: AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool# = AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]new().(true);
27
28 impl TestSupportrust-analyzer cargo sqlx-core 0.8.3 testing/TestSupport# for MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql# {
29 fn test_contextrust-analyzer cargo sqlx-mysql 0.8.3 testing/impl#[MySql][TestSupport]test_context().(args: &TestArgsrust-analyzer cargo sqlx-core 0.8.3 testing/TestArgs#) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'_, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<TestContextrust-analyzer cargo sqlx-core 0.8.3 testing/TestContext#<Selfrust-analyzer cargo sqlx-mysql 0.8.3 testing/impl#[MySql][TestSupport]>, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>> {
30 Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::pinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]pin().(async move { test_contextrust-analyzer cargo sqlx-mysql 0.8.3 testing/test_context().(args).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll(). })
31 }
32
33 fn cleanup_testrust-analyzer cargo sqlx-mysql 0.8.3 testing/impl#[MySql][TestSupport]cleanup_test().(db_name: &str) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'_, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>> {
34 Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::pinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]pin().(async move {
35 let mut conn = MASTER_POOLrust-analyzer cargo sqlx-mysql 0.8.3 testing/MASTER_POOL.
36 .getrust-analyzer cargo once_cell 1.21.4 sync/impl#[`OnceCell<T>`]get().()
37 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("cleanup_test() invoked outside `#[sqlx::test]")
38 .acquirerust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]acquire().()
39 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
40
41 let db_id = db_idrust-analyzer cargo sqlx-mysql 0.8.3 testing/db_id().(db_name);
42
43 conn.executerust-analyzer cargo sqlx-core 0.8.3 executor/Executor#execute().(&formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("drop database if exists {db_name};")[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().)
44 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
45
46 queryrust-analyzer cargo sqlx-core 0.8.3 query/query().("delete from _sqlx_test_databases where db_id = ?")
47 .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(db_id)
48 .executerust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`]execute().(&mut *rust-analyzer cargo sqlx-core 0.8.3 pool/connection/impl#[`PoolConnection<DB>`][DerefMut]deref_mut().conn)
49 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
50
51 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
52 })
53 }
54
55 fn cleanup_test_dbsrust-analyzer cargo sqlx-mysql 0.8.3 testing/impl#[MySql][TestSupport]cleanup_test_dbs().() -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'static, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>> {
56 Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::pinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]pin().(async move {
57 let url = dotenvyrust-analyzer cargo dotenvy 0.15.7 crate/::varrust-analyzer cargo dotenvy 0.15.7 var().("DATABASE_URL").expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("DATABASE_URL must be set");
58
59 let mut conn = MySqlConnectionrust-analyzer cargo sqlx-mysql 0.8.3 connection/MySqlConnection#::connectrust-analyzer cargo sqlx-core 0.8.3 connection/Connection#connect().(&url).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
60
61 let now = SystemTimerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/SystemTime#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[SystemTime]now().()
62 .duration_sincerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[SystemTime]duration_since().(SystemTimerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/SystemTime#::UNIX_EPOCHrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[SystemTime]UNIX_EPOCH.)
63 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
64
65 let num_deleted = do_cleanuprust-analyzer cargo sqlx-mysql 0.8.3 testing/do_cleanup().(&mut conn, now).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
66 let _ = conn.closerust-analyzer cargo sqlx-mysql 0.8.3 connection/impl#[MySqlConnection][Connection]close().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().;
67 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(num_deleted))
68 })
69 }
70
71 fn snapshotrust-analyzer cargo sqlx-mysql 0.8.3 testing/impl#[MySql][TestSupport]snapshot().(
72 _conn: &mut Selfrust-analyzer cargo sqlx-mysql 0.8.3 testing/impl#[MySql][TestSupport]::Connectionrust-analyzer cargo sqlx-core 0.8.3 database/Database#Connection#,
73 ) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'_, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<FixtureSnapshotrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#<Selfrust-analyzer cargo sqlx-mysql 0.8.3 testing/impl#[MySql][TestSupport]>, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>> {
74 // TODO: I want to get the testing feature out the door so this will have to wait,
75 // but I'm keeping the code around for now because I plan to come back to it.
76 todorust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/todo!!()
77 }
78 }
79
80 async fn test_contextrust-analyzer cargo sqlx-mysql 0.8.3 testing/test_context().(args: &TestArgsrust-analyzer cargo sqlx-core 0.8.3 testing/TestArgs#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<TestContextrust-analyzer cargo sqlx-core 0.8.3 testing/TestContext#<MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#>, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> {
81 let url = dotenvyrust-analyzer cargo dotenvy 0.15.7 crate/::varrust-analyzer cargo dotenvy 0.15.7 var().("DATABASE_URL").expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("DATABASE_URL must be set");
82
83 let master_opts = MySqlConnectOptionsrust-analyzer cargo sqlx-mysql 0.8.3 options/MySqlConnectOptions#::from_strrust-analyzer cargo sqlx-mysql 0.8.3 options/parse/impl#[MySqlConnectOptions][FromStr]from_str().(&url).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to parse DATABASE_URL");
84
85 let pool = PoolOptionsrust-analyzer cargo sqlx-core 0.8.3 pool/options/PoolOptions#::newrust-analyzer cargo sqlx-core 0.8.3 pool/options/impl#[`PoolOptions<DB>`]new().()
86 // MySql's normal connection limit is 150 plus 1 superuser connection
87 // We don't want to use the whole cap and there may be fuzziness here due to
88 // concurrently running tests anyway.
89 .max_connectionsrust-analyzer cargo sqlx-core 0.8.3 pool/options/impl#[`PoolOptions<DB>`]max_connections().(20)
90 // Immediately close master connections. Tokio's I/O streams don't like hopping runtimes.
91 .after_releaserust-analyzer cargo sqlx-core 0.8.3 pool/options/impl#[`PoolOptions<DB>`]after_release().(|_conn, _| Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::pinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]pin().(async move { Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(false) }))
92 .connect_lazy_withrust-analyzer cargo sqlx-core 0.8.3 pool/options/impl#[`PoolOptions<DB>`]connect_lazy_with().(master_opts);
93
94 let master_pool = match MASTER_POOLrust-analyzer cargo sqlx-mysql 0.8.3 testing/MASTER_POOL..try_insertrust-analyzer cargo once_cell 1.21.4 sync/impl#[`OnceCell<T>`]try_insert().(pool) {
95 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(inserted) => inserted,
96 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#((existing, pool)) => {
97 // Sanity checks.
98 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
99 existing.connect_optionsrust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]connect_options().().hostrust-analyzer cargo sqlx-mysql 0.8.3 options/MySqlConnectOptions#host.,
100 pool.connect_optionsrust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]connect_options().().hostrust-analyzer cargo sqlx-mysql 0.8.3 options/MySqlConnectOptions#host.,
101 "DATABASE_URL changed at runtime, host differs"
102 );
103
104 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
105 existing.connect_optionsrust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]connect_options().().databaserust-analyzer cargo sqlx-mysql 0.8.3 options/MySqlConnectOptions#database.,
106 pool.connect_optionsrust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]connect_options().().databaserust-analyzer cargo sqlx-mysql 0.8.3 options/MySqlConnectOptions#database.,
107 "DATABASE_URL changed at runtime, database differs"
108 );
109
110 existing
111 }
112 };
113
114 let mut conn = master_pool.acquirerust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]acquire().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
115
116 // language=MySQL
117 conn.executerust-analyzer cargo sqlx-core 0.8.3 executor/Executor#execute().(
118 r#"
119 create table if not exists _sqlx_test_databases (
120 db_id bigint unsigned primary key auto_increment,
121 test_path text not null,
122 created_at timestamp not null default current_timestamp
123 );
124 "#,
125 )
126 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
127
128 // Record the current time _before_ we acquire the `DO_CLEANUP` permit. This
129 // prevents the first test thread from accidentally deleting new test dbs
130 // created by other test threads if we're a bit slow.
131 let now = SystemTimerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/SystemTime#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[SystemTime]now().()
132 .duration_sincerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[SystemTime]duration_since().(SystemTimerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/SystemTime#::UNIX_EPOCHrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[SystemTime]UNIX_EPOCH.)
133 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
134
135 // Only run cleanup if the test binary just started.
136 if DO_CLEANUPrust-analyzer cargo sqlx-mysql 0.8.3 testing/DO_CLEANUP..swaprust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]swap().(false, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#) {
137 do_cleanuprust-analyzer cargo sqlx-mysql 0.8.3 testing/do_cleanup().(&mut conn, now).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
138 }
139
140 queryrust-analyzer cargo sqlx-core 0.8.3 query/query().("insert into _sqlx_test_databases(test_path) values (?)")
141 .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(args.test_pathrust-analyzer cargo sqlx-core 0.8.3 testing/TestArgs#test_path.)
142 .executerust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`]execute().(&mut *rust-analyzer cargo sqlx-core 0.8.3 pool/connection/impl#[`PoolConnection<DB>`][DerefMut]deref_mut().conn)
143 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
144
145 // MySQL doesn't have `INSERT ... RETURNING`
146 let new_db_id: u64 = query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().("select last_insert_id()")
147 .fetch_onerust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch_one().(&mut *rust-analyzer cargo sqlx-core 0.8.3 pool/connection/impl#[`PoolConnection<DB>`][DerefMut]deref_mut().conn)
148 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
149
150 let new_db_name = db_namerust-analyzer cargo sqlx-mysql 0.8.3 testing/db_name().(new_db_id);
151
152 conn.executerust-analyzer cargo sqlx-core 0.8.3 executor/Executor#execute().(&formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("create database {new_db_name}")[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().)
153 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
154
155 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("created database {new_db_name}");
156
157 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(TestContextrust-analyzer cargo sqlx-core 0.8.3 testing/TestContext# {
158 pool_optsrust-analyzer cargo sqlx-core 0.8.3 testing/TestContext#pool_opts.: PoolOptionsrust-analyzer cargo sqlx-core 0.8.3 pool/options/PoolOptions#::newrust-analyzer cargo sqlx-core 0.8.3 pool/options/impl#[`PoolOptions<DB>`]new().()
159 // Don't allow a single test to take all the connections.
160 // Most tests shouldn't require more than 5 connections concurrently,
161 // or else they're likely doing too much in one test.
162 .max_connectionsrust-analyzer cargo sqlx-core 0.8.3 pool/options/impl#[`PoolOptions<DB>`]max_connections().(5)
163 // Close connections ASAP if left in the idle queue.
164 .idle_timeoutrust-analyzer cargo sqlx-core 0.8.3 pool/options/impl#[`PoolOptions<DB>`]idle_timeout().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(1)))
165 .parentrust-analyzer cargo sqlx-core 0.8.3 pool/options/impl#[`PoolOptions<DB>`]parent().(master_pool.clonerust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`][Clone]clone().()),
166 connect_optsrust-analyzer cargo sqlx-core 0.8.3 testing/TestContext#connect_opts.: master_pool
167 .connect_optionsrust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]connect_options().()
168 .derefrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Deref]deref().()
169 .clonerust-analyzer cargo sqlx-mysql 0.8.3 options/impl#[MySqlConnectOptions][Clone]clone().()
170 .databaserust-analyzer cargo sqlx-mysql 0.8.3 options/impl#[MySqlConnectOptions]database().(&new_db_name),
171 db_namerust-analyzer cargo sqlx-core 0.8.3 testing/TestContext#db_name.: new_db_name,
172 })
173 }
174
175 async fn do_cleanuprust-analyzer cargo sqlx-mysql 0.8.3 testing/do_cleanup().(conn: &mut MySqlConnectionrust-analyzer cargo sqlx-mysql 0.8.3 connection/MySqlConnection#, created_before: Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> {
176 // since SystemTime is not monotonic we added a little margin here to avoid race conditions with other threads
177 let created_before_as_secs = created_before.as_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_secs().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().2;
178 let delete_db_ids: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u64> = query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().(
179 "select db_id from _sqlx_test_databases \
180 where created_at < from_unixtime(?)",
181 )
182 .bindrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>`]bind().(created_before_as_secs)
183 .fetch_allrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch_all().(&mut *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().conn)
184 .rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
185
186 if delete_db_ids.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
187 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0);
188 }
189
190 let mut deleted_db_ids = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(delete_db_ids.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().());
191
192 let mut command = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
193
194 for db_id in delete_db_ids {
195 command.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]clear().();
196
197 let db_name = db_namerust-analyzer cargo sqlx-mysql 0.8.3 testing/db_name().(db_id);
198
199 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(command, "drop database if exists {db_name}").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().();
200 match conn.executerust-analyzer cargo sqlx-core 0.8.3 executor/Executor#execute().(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Deref]deref().command).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll(). {
201 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(_deleted) => {
202 deleted_db_ids.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(db_id);
203 }
204 // Assume a database error just means the DB is still in use.
205 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#::Databaserust-analyzer cargo sqlx-core 0.8.3 error/Error#Database#(dbe)) => {
206 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("could not clean test database {db_id:?}: {dbe}")
207 }
208 // Bubble up other errors
209 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e),
210 }
211 }
212
213 let mut query = QueryBuilderrust-analyzer cargo sqlx-core 0.8.3 query_builder/QueryBuilder#::newrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`QueryBuilder<'args, DB>`]new().("delete from _sqlx_test_databases where db_id in (");
214
215 let mut separated = query.separatedrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`QueryBuilder<'args, DB>`]separated().(",");
216
217 for db_id in &deleted_db_ids {
218 separated.push_bindrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`Separated<'qb, 'args, DB, Sep>`]push_bind().(db_id);
219 }
220
221 query.pushrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`QueryBuilder<'args, DB>`]push().(")").buildrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`QueryBuilder<'args, DB>`]build().().executerust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`]execute().(&mut *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().conn).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
222
223 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(deleted_db_ids.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().())
224 }
225
226 fn db_namerust-analyzer cargo sqlx-mysql 0.8.3 testing/db_name().(id: u64) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
227 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("_sqlx_test_database_{id}")
228 }
229
230 fn db_idrust-analyzer cargo sqlx-mysql 0.8.3 testing/db_id().(name: &str) -> u64 {
231 name.trim_start_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start_matches().("_sqlx_test_database_")
232 .parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()
233 .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|_1| panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("failed to parse ID from database name {name:?}"))
234 }
235
236 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
237 fn test_db_name_idrust-analyzer cargo sqlx-mysql 0.8.3 testing/test_db_name_id().() {
238 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(db_namerust-analyzer cargo sqlx-mysql 0.8.3 testing/db_name().(12345), "_sqlx_test_database_12345");
239 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(db_idrust-analyzer cargo sqlx-mysql 0.8.3 testing/db_id().("_sqlx_test_database_12345"), 12345);
240 }