| 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-postgres 0.8.3 crate/::connectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/::Connectionrust-analyzer cargo sqlx-core 0.8.3 connection/Connection#; |
| 12 | |
| 13 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::errorrust-analyzer cargo sqlx-postgres 0.8.3 error/::Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#; |
| 14 | use craterust-analyzer cargo sqlx-postgres 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-postgres 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-postgres 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-postgres 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().; |
| 18 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::{PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#, PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#}; |
| 19 | |
| 20 | pub(craterust-analyzer cargo sqlx-postgres 0.8.3 crate/) use sqlx_corerust-analyzer cargo sqlx-core 0.8.3 crate/::testingrust-analyzer cargo sqlx-core 0.8.3 testing/::*; |
| 21 | |
| 22 | // Using a blocking `OnceCell` here because the critical sections are short. |
| 23 | static MASTER_POOLrust-analyzer cargo sqlx-postgres 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#<Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#>> = OnceCellrust-analyzer cargo once_cell 1.21.4 sync/OnceCell#::newrust-analyzer cargo once_cell 1.21.4 sync/impl#[`OnceCell<T>`]new().(); |
| 24 | // Automatically delete any databases created before the start of the test binary. |
| 25 | static DO_CLEANUPrust-analyzer cargo sqlx-postgres 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); |
| 26 | |
| 27 | impl TestSupportrust-analyzer cargo sqlx-core 0.8.3 testing/TestSupport# for Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres# { |
| 28 | fn test_contextrust-analyzer cargo sqlx-postgres 0.8.3 testing/impl#[Postgres][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-postgres 0.8.3 testing/impl#[Postgres][TestSupport]>, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>> { |
| 29 | 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-postgres 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(). }) |
| 30 | } |
| 31 | |
| 32 | fn cleanup_testrust-analyzer cargo sqlx-postgres 0.8.3 testing/impl#[Postgres][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#>> { |
| 33 | 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 { |
| 34 | let mut conn = MASTER_POOLrust-analyzer cargo sqlx-postgres 0.8.3 testing/MASTER_POOL. |
| 35 | .getrust-analyzer cargo once_cell 1.21.4 sync/impl#[`OnceCell<T>`]get().() |
| 36 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("cleanup_test() invoked outside `#[sqlx::test]") |
| 37 | .acquirerust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]acquire().() |
| 38 | .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().; |
| 39 | |
| 40 | 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().) |
| 41 | .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().; |
| 42 | |
| 43 | queryrust-analyzer cargo sqlx-core 0.8.3 query/query().("delete from _sqlx_test.databases where db_name = $1") |
| 44 | .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(db_name) |
| 45 | .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) |
| 46 | .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().; |
| 47 | |
| 48 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | fn cleanup_test_dbsrust-analyzer cargo sqlx-postgres 0.8.3 testing/impl#[Postgres][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#>> { |
| 53 | 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 { |
| 54 | 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"); |
| 55 | |
| 56 | let mut conn = PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#::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().; |
| 57 | |
| 58 | 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().() |
| 59 | .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.) |
| 60 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 61 | |
| 62 | let num_deleted = do_cleanuprust-analyzer cargo sqlx-postgres 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().; |
| 63 | let _ = conn.closerust-analyzer cargo sqlx-postgres 0.8.3 connection/impl#[PgConnection][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().; |
| 64 | 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)) |
| 65 | }) |
| 66 | } |
| 67 | |
| 68 | fn snapshotrust-analyzer cargo sqlx-postgres 0.8.3 testing/impl#[Postgres][TestSupport]snapshot().( |
| 69 | _conn: &mut Selfrust-analyzer cargo sqlx-postgres 0.8.3 testing/impl#[Postgres][TestSupport]::Connectionrust-analyzer cargo sqlx-core 0.8.3 database/Database#Connection#, |
| 70 | ) -> 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-postgres 0.8.3 testing/impl#[Postgres][TestSupport]>, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>> { |
| 71 | // TODO: I want to get the testing feature out the door so this will have to wait, |
| 72 | // but I'm keeping the code around for now because I plan to come back to it. |
| 73 | todorust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/todo!!() |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | async fn test_contextrust-analyzer cargo sqlx-postgres 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#<Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#>, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> { |
| 78 | 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"); |
| 79 | |
| 80 | let master_opts = PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#::from_strrust-analyzer cargo sqlx-postgres 0.8.3 options/parse/impl#[PgConnectOptions][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"); |
| 81 | |
| 82 | 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().() |
| 83 | // Postgres' normal connection limit is 100 plus 3 superuser connections |
| 84 | // We don't want to use the whole cap and there may be fuzziness here due to |
| 85 | // concurrently running tests anyway. |
| 86 | .max_connectionsrust-analyzer cargo sqlx-core 0.8.3 pool/options/impl#[`PoolOptions<DB>`]max_connections().(20) |
| 87 | // Immediately close master connections. Tokio's I/O streams don't like hopping runtimes. |
| 88 | .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) })) |
| 89 | .connect_lazy_withrust-analyzer cargo sqlx-core 0.8.3 pool/options/impl#[`PoolOptions<DB>`]connect_lazy_with().(master_opts); |
| 90 | |
| 91 | let master_pool = match MASTER_POOLrust-analyzer cargo sqlx-postgres 0.8.3 testing/MASTER_POOL..try_insertrust-analyzer cargo once_cell 1.21.4 sync/impl#[`OnceCell<T>`]try_insert().(pool) { |
| 92 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(inserted) => inserted, |
| 93 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#((existing, pool)) => { |
| 94 | // Sanity checks. |
| 95 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 96 | existing.connect_optionsrust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]connect_options().().hostrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#host., |
| 97 | pool.connect_optionsrust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]connect_options().().hostrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#host., |
| 98 | "DATABASE_URL changed at runtime, host differs" |
| 99 | ); |
| 100 | |
| 101 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 102 | existing.connect_optionsrust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]connect_options().().databaserust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#database., |
| 103 | pool.connect_optionsrust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]connect_options().().databaserust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#database., |
| 104 | "DATABASE_URL changed at runtime, database differs" |
| 105 | ); |
| 106 | |
| 107 | existing |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | 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().; |
| 112 | |
| 113 | // language=PostgreSQL |
| 114 | conn.executerust-analyzer cargo sqlx-core 0.8.3 executor/Executor#execute().( |
| 115 | // Explicit lock avoids this latent bug: https://stackoverflow.com/a/29908840 |
| 116 | // I couldn't find a bug on the mailing list for `CREATE SCHEMA` specifically, |
| 117 | // but a clearly related bug with `CREATE TABLE` has been known since 2007: |
| 118 | // https://www.postgresql.org/message-id/200710222037.l9MKbCJZ098744%40wwwmaster.postgresql.org |
| 119 | r#" |
| 120 | lock table pg_catalog.pg_namespace in share row exclusive mode; |
| 121 | |
| 122 | create schema if not exists _sqlx_test; |
| 123 | |
| 124 | create table if not exists _sqlx_test.databases ( |
| 125 | db_name text primary key, |
| 126 | test_path text not null, |
| 127 | created_at timestamptz not null default now() |
| 128 | ); |
| 129 | |
| 130 | create index if not exists databases_created_at |
| 131 | on _sqlx_test.databases(created_at); |
| 132 | |
| 133 | create sequence if not exists _sqlx_test.database_ids; |
| 134 | "#, |
| 135 | ) |
| 136 | .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().; |
| 137 | |
| 138 | // Record the current time _before_ we acquire the `DO_CLEANUP` permit. This |
| 139 | // prevents the first test thread from accidentally deleting new test dbs |
| 140 | // created by other test threads if we're a bit slow. |
| 141 | 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().() |
| 142 | .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.) |
| 143 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 144 | |
| 145 | // Only run cleanup if the test binary just started. |
| 146 | if DO_CLEANUPrust-analyzer cargo sqlx-postgres 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#) { |
| 147 | do_cleanuprust-analyzer cargo sqlx-postgres 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().; |
| 148 | } |
| 149 | |
| 150 | let new_db_name: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# = query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().( |
| 151 | r#" |
| 152 | insert into _sqlx_test.databases(db_name, test_path) |
| 153 | select '_sqlx_test_' || nextval('_sqlx_test.database_ids'), $1 |
| 154 | returning db_name |
| 155 | "#, |
| 156 | ) |
| 157 | .bindrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>`]bind().(args.test_pathrust-analyzer cargo sqlx-core 0.8.3 testing/TestArgs#test_path.) |
| 158 | .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) |
| 159 | .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().; |
| 160 | |
| 161 | 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().) |
| 162 | .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().; |
| 163 | |
| 164 | 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# { |
| 165 | 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().() |
| 166 | // Don't allow a single test to take all the connections. |
| 167 | // Most tests shouldn't require more than 5 connections concurrently, |
| 168 | // or else they're likely doing too much in one test. |
| 169 | .max_connectionsrust-analyzer cargo sqlx-core 0.8.3 pool/options/impl#[`PoolOptions<DB>`]max_connections().(5) |
| 170 | // Close connections ASAP if left in the idle queue. |
| 171 | .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))) |
| 172 | .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().()), |
| 173 | connect_optsrust-analyzer cargo sqlx-core 0.8.3 testing/TestContext#connect_opts.: master_pool |
| 174 | .connect_optionsrust-analyzer cargo sqlx-core 0.8.3 pool/impl#[`Pool<DB>`]connect_options().() |
| 175 | .derefrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Deref]deref().() |
| 176 | .clonerust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions][Clone]clone().() |
| 177 | .databaserust-analyzer cargo sqlx-postgres 0.8.3 options/impl#[PgConnectOptions]database().(&new_db_name), |
| 178 | db_namerust-analyzer cargo sqlx-core 0.8.3 testing/TestContext#db_name.: new_db_name, |
| 179 | }) |
| 180 | } |
| 181 | |
| 182 | async fn do_cleanuprust-analyzer cargo sqlx-postgres 0.8.3 testing/do_cleanup().(conn: &mut PgConnectionrust-analyzer cargo sqlx-postgres 0.8.3 connection/PgConnection#, 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#> { |
| 183 | // since SystemTime is not monotonic we added a little margin here to avoid race conditions with other threads |
| 184 | let created_before = i64::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[i64][`TryFrom<u64>`]try_from().(created_before.as_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_secs().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Sub<Self>`]sub().2; |
| 185 | |
| 186 | let delete_db_names: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> = query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().( |
| 187 | "select db_name from _sqlx_test.databases \ |
| 188 | where created_at < (to_timestamp($1) at time zone 'UTC')", |
| 189 | ) |
| 190 | .bindrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>`]bind().(created_before) |
| 191 | .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) |
| 192 | .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().; |
| 193 | |
| 194 | if delete_db_names.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 195 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0); |
| 196 | } |
| 197 | |
| 198 | let mut deleted_db_names = 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_names.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 199 | let delete_db_names = delete_db_names.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().(); |
| 200 | |
| 201 | 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().(); |
| 202 | |
| 203 | for db_name in delete_db_names { |
| 204 | command.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]clear().(); |
| 205 | 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().(); |
| 206 | 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(). { |
| 207 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(_deleted) => { |
| 208 | deleted_db_names.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(db_name); |
| 209 | } |
| 210 | // Assume a database error just means the DB is still in use. |
| 211 | 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)) => { |
| 212 | eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("could not clean test database {db_name:?}: {dbe}") |
| 213 | } |
| 214 | // Bubble up other errors |
| 215 | 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), |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | queryrust-analyzer cargo sqlx-core 0.8.3 query/query().("delete from _sqlx_test.databases where db_name = any($1::text[])") |
| 220 | .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(&deleted_db_names) |
| 221 | .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) |
| 222 | .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().; |
| 223 | |
| 224 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(deleted_db_names.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()) |
| 225 | } |