db-pool 0.6.0

A thread-safe database pool for running database-tied integration tests in parallel
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
use std::{
    borrow::Cow,
    fmt::Debug,
    marker::PhantomData,
    ops::{Deref, DerefMut},
};

use async_trait::async_trait;
use uuid::Uuid;

use crate::{common::statement::mysql, util::get_db_name};

use super::super::error::Error as BackendError;

#[async_trait]
pub(super) trait MySQLBackend<'pool>: Send + Sync + 'static {
    type Connection;
    type PooledConnection: DerefMut<Target = Self::Connection>;
    type Pool;

    type BuildError: Into<
            BackendError<
                Self::BuildError,
                Self::PoolError,
                Self::ConnectionError,
                Self::QueryError,
            >,
        > + Debug;
    type PoolError: Into<
            BackendError<
                Self::BuildError,
                Self::PoolError,
                Self::ConnectionError,
                Self::QueryError,
            >,
        > + Debug;
    type ConnectionError: Into<
            BackendError<
                Self::BuildError,
                Self::PoolError,
                Self::ConnectionError,
                Self::QueryError,
            >,
        > + Debug;
    type QueryError: Into<
            BackendError<
                Self::BuildError,
                Self::PoolError,
                Self::ConnectionError,
                Self::QueryError,
            >,
        > + Debug;

    async fn get_connection(&'pool self) -> Result<Self::PooledConnection, Self::PoolError>;

    async fn execute_query(
        &self,
        query: &str,
        conn: &mut Self::Connection,
    ) -> Result<(), Self::QueryError>;
    async fn batch_execute_query<'a>(
        &self,
        query: impl IntoIterator<Item = Cow<'a, str>> + Send,
        conn: &mut Self::Connection,
    ) -> Result<(), Self::QueryError>;

    fn get_host(&self) -> &str;

    async fn get_previous_database_names(
        &self,
        conn: &mut Self::Connection,
    ) -> Result<Vec<String>, Self::QueryError>;
    async fn create_entities(&self, db_name: &str) -> Result<(), Self::ConnectionError>;
    async fn create_connection_pool(&self, db_id: Uuid) -> Result<Self::Pool, Self::BuildError>;

    async fn get_table_names(
        &self,
        db_name: &str,
        conn: &mut Self::Connection,
    ) -> Result<Vec<String>, Self::QueryError>;

    fn get_drop_previous_databases(&self) -> bool;
}

pub(super) struct MySQLBackendWrapper<'backend, 'pool, B: MySQLBackend<'pool>> {
    inner: &'backend B,
    _marker: &'pool PhantomData<()>,
}

impl<'backend, 'pool, B: MySQLBackend<'pool>> MySQLBackendWrapper<'backend, 'pool, B> {
    pub(super) fn new(backend: &'backend B) -> Self {
        Self {
            inner: backend,
            _marker: &PhantomData,
        }
    }
}

impl<'pool, B: MySQLBackend<'pool>> Deref for MySQLBackendWrapper<'_, 'pool, B> {
    type Target = B;

    fn deref(&self) -> &Self::Target {
        self.inner
    }
}

impl<'backend, 'pool, B> MySQLBackendWrapper<'backend, 'pool, B>
where
    'backend: 'pool,
    B: MySQLBackend<'pool>,
{
    pub(super) async fn init(
        &'backend self,
    ) -> Result<(), BackendError<B::BuildError, B::PoolError, B::ConnectionError, B::QueryError>>
    {
        // Drop previous databases if needed
        if self.get_drop_previous_databases() {
            // Get privileged connection
            let conn = &mut self.get_connection().await.map_err(Into::into)?;

            // Get previous database names
            self.execute_query(mysql::USE_DEFAULT_DATABASE, conn)
                .await
                .map_err(Into::into)?;
            let mut db_names = self
                .get_previous_database_names(conn)
                .await
                .map_err(Into::into)?;

            // Drop databases
            let futures = db_names
                .drain(..)
                .map(|db_name| async move {
                    let conn = &mut self.get_connection().await.map_err(Into::into)?;
                    self.execute_query(mysql::drop_database(db_name.as_str()).as_str(), conn)
                        .await
                        .map_err(Into::into)?;
                    Ok::<
                        _,
                        BackendError<
                            B::BuildError,
                            B::PoolError,
                            B::ConnectionError,
                            B::QueryError,
                        >,
                    >(())
                })
                .collect::<Vec<_>>();
            futures::future::try_join_all(futures).await?;
        }

        Ok(())
    }

    pub(super) async fn create(
        &'backend self,
        db_id: uuid::Uuid,
        restrict_privileges: bool,
    ) -> Result<B::Pool, BackendError<B::BuildError, B::PoolError, B::ConnectionError, B::QueryError>>
    {
        // Get database name based on UUID
        let db_name = get_db_name(db_id);
        let db_name = db_name.as_str();

        let host = self.get_host();

        // Get privileged connection
        let conn = &mut self.get_connection().await.map_err(Into::into)?;

        // Create database
        self.execute_query(mysql::create_database(db_name).as_str(), conn)
            .await
            .map_err(Into::into)?;

        // Create user
        self.execute_query(mysql::create_user(db_name, host).as_str(), conn)
            .await
            .map_err(Into::into)?;

        // Create entities
        self.execute_query(mysql::use_database(db_name).as_str(), conn)
            .await
            .map_err(Into::into)?;
        self.create_entities(db_name).await.map_err(Into::into)?;
        self.execute_query(mysql::USE_DEFAULT_DATABASE, conn)
            .await
            .map_err(Into::into)?;

        if restrict_privileges {
            // Grant privileges to restricted user
            self.execute_query(
                mysql::grant_restricted_privileges(db_name, host).as_str(),
                conn,
            )
            .await
            .map_err(Into::into)?;
        } else {
            // Grant all privileges to database-unrestricted user
            self.execute_query(mysql::grant_all_privileges(db_name, host).as_str(), conn)
                .await
                .map_err(Into::into)?;
        }

        // Create connection pool with attached user
        let pool = self
            .create_connection_pool(db_id)
            .await
            .map_err(Into::into)?;

        Ok(pool)
    }

    pub(super) async fn clean(
        &'backend self,
        db_id: uuid::Uuid,
    ) -> Result<(), BackendError<B::BuildError, B::PoolError, B::ConnectionError, B::QueryError>>
    {
        // Get database name based on UUID
        let db_name = get_db_name(db_id);
        let db_name = db_name.as_str();

        // Get privileged connection
        let conn = &mut self.get_connection().await.map_err(Into::into)?;

        // Get table names
        let table_names = self
            .get_table_names(db_name, conn)
            .await
            .map_err(Into::into)?;

        // Generate truncate statements
        let stmts = table_names
            .iter()
            .map(|table_name| mysql::truncate_table(table_name.as_str(), db_name).into());

        // Turn off foreign key checks
        self.execute_query(mysql::TURN_OFF_FOREIGN_KEY_CHECKS, conn)
            .await
            .map_err(Into::into)?;

        // Truncate tables
        self.batch_execute_query(stmts, conn)
            .await
            .map_err(Into::into)?;

        // Turn on foreign key checks
        self.execute_query(mysql::TURN_ON_FOREIGN_KEY_CHECKS, conn)
            .await
            .map_err(Into::into)?;

        Ok(())
    }

    pub(super) async fn drop(
        &'backend self,
        db_id: uuid::Uuid,
    ) -> Result<(), BackendError<B::BuildError, B::PoolError, B::ConnectionError, B::QueryError>>
    {
        // Get database name based on UUID
        let db_name = get_db_name(db_id);
        let db_name = db_name.as_str();

        let host = self.get_host();

        // Get privileged connection
        let conn = &mut self.get_connection().await.map_err(Into::into)?;

        // Drop database
        self.execute_query(mysql::drop_database(db_name).as_str(), conn)
            .await
            .map_err(Into::into)?;

        // Drop attached user
        self.execute_query(mysql::drop_user(db_name, host).as_str(), conn)
            .await
            .map_err(Into::into)?;

        Ok(())
    }
}

#[cfg(test)]
pub(super) mod tests {
    #![allow(clippy::unwrap_used)]

    use bb8::Pool as Bb8Pool;
    use diesel::{dsl::exists, insert_into, prelude::*, select, sql_query, table};
    use diesel_async::{
        pooled_connection::AsyncDieselConnectionManager, AsyncMysqlConnection, RunQueryDsl,
    };
    use futures::{future::join_all, Future};
    use tokio::sync::OnceCell;
    use uuid::Uuid;

    use crate::{
        common::statement::mysql::tests::{DDL_STATEMENTS, DML_STATEMENTS},
        r#async::{backend::r#trait::Backend, db_pool::DatabasePoolBuilder},
        tests::{get_privileged_mysql_config, MYSQL_DROP_LOCK},
        util::get_db_name,
    };

    pub type Pool = Bb8Pool<AsyncDieselConnectionManager<AsyncMysqlConnection>>;

    table! {
        schemata (schema_name) {
            schema_name -> Text
        }
    }

    #[allow(unused_variables)]
    pub trait MySQLDropLock<T>
    where
        Self: Future<Output = T> + Sized,
    {
        async fn lock_drop(self) -> T {
            let guard = MYSQL_DROP_LOCK.write().await;
            self.await
        }

        async fn lock_read(self) -> T {
            let guard = MYSQL_DROP_LOCK.read().await;
            self.await
        }
    }

    impl<T, F> MySQLDropLock<T> for F where F: Future<Output = T> + Sized {}

    async fn get_privileged_connection_pool<'a>() -> &'a Pool {
        static POOL: OnceCell<Pool> = OnceCell::const_new();
        POOL.get_or_init(|| async {
            let config = get_privileged_mysql_config();
            let database_url = config.default_connection_url();
            let manager = AsyncDieselConnectionManager::new(database_url);
            Bb8Pool::builder().build(manager).await.unwrap()
        })
        .await
    }

    async fn create_restricted_connection_pool(db_name: &str) -> Pool {
        let config = get_privileged_mysql_config();
        let database_url =
            config.restricted_database_connection_url(db_name, Some(db_name), db_name);
        let manager = AsyncDieselConnectionManager::new(database_url);
        Bb8Pool::builder().build(manager).await.unwrap()
    }

    async fn create_database(conn: &mut AsyncMysqlConnection) -> String {
        let db_id = Uuid::new_v4();
        let db_name = get_db_name(db_id);
        sql_query(format!("CREATE DATABASE {db_name}"))
            .execute(conn)
            .await
            .unwrap();
        db_name
    }

    async fn create_databases(count: i64, pool: &Pool) -> Vec<String> {
        let futures = (0..count)
            .map(|_| async {
                let conn = &mut pool.get().await.unwrap();
                create_database(conn).await
            })
            .collect::<Vec<_>>();
        join_all(futures).await
    }

    async fn use_database(db_name: &str, conn: &mut AsyncMysqlConnection) {
        sql_query(format!("USE {db_name}"))
            .execute(conn)
            .await
            .unwrap();
    }

    async fn use_information_schema(conn: &mut AsyncMysqlConnection) {
        use_database("information_schema", conn).await;
    }

    async fn count_databases(db_names: &Vec<String>, conn: &mut AsyncMysqlConnection) -> i64 {
        use_information_schema(conn).await;

        schemata::table
            .filter(schemata::schema_name.eq_any(db_names))
            .count()
            .get_result(conn)
            .await
            .unwrap()
    }

    async fn count_all_databases(conn: &mut AsyncMysqlConnection) -> i64 {
        use_information_schema(conn).await;

        schemata::table
            .filter(schemata::schema_name.like("db_pool_%"))
            .count()
            .get_result(conn)
            .await
            .unwrap()
    }

    async fn database_exists(db_name: &str, conn: &mut AsyncMysqlConnection) -> bool {
        use_information_schema(conn).await;

        select(exists(
            schemata::table.filter(schemata::schema_name.eq(db_name)),
        ))
        .get_result(conn)
        .await
        .unwrap()
    }

    pub async fn test_backend_drops_previous_databases<B: Backend>(
        default: B,
        enabled: B,
        disabled: B,
    ) {
        const NUM_DBS: i64 = 3;

        let conn_pool = get_privileged_connection_pool().await;
        let conn = &mut conn_pool.get().await.unwrap();

        async {
            for (backend, cleans) in [(default, true), (enabled, true), (disabled, false)] {
                let db_names = create_databases(NUM_DBS, conn_pool).await;
                assert_eq!(count_databases(&db_names, conn).await, NUM_DBS);
                backend.init().await.unwrap();
                assert_eq!(
                    count_databases(&db_names, conn).await,
                    if cleans { 0 } else { NUM_DBS }
                );
            }
        }
        .lock_drop()
        .await;
    }

    pub async fn test_backend_creates_database_with_restricted_privileges(backend: impl Backend) {
        let db_id = Uuid::new_v4();
        let db_name = get_db_name(db_id);
        let db_name = db_name.as_str();

        async {
            // privileged operations
            {
                let conn_pool = get_privileged_connection_pool().await;
                let conn = &mut conn_pool.get().await.unwrap();

                // database must not exist
                assert!(!database_exists(db_name, conn).await);

                // database must exist after creating through backend
                backend.init().await.unwrap();
                backend.create(db_id, true).await.unwrap();
                assert!(database_exists(db_name, conn).await);
            }

            // restricted operations
            {
                let conn_pool = create_restricted_connection_pool(db_name).await;
                let conn = &mut conn_pool.get().await.unwrap();

                // // DDL statements must fail
                for stmt in DDL_STATEMENTS {
                    assert!(sql_query(stmt).execute(conn).await.is_err());
                }

                // DML statements must succeed
                for stmt in DML_STATEMENTS {
                    assert!(sql_query(stmt).execute(conn).await.is_ok());
                }
            }
        }
        .lock_read()
        .await;
    }

    pub async fn test_backend_creates_database_with_unrestricted_privileges(backend: impl Backend) {
        async {
            {
                let db_id = Uuid::new_v4();
                let db_name = get_db_name(db_id);
                let db_name = db_name.as_str();

                // privileged operations
                {
                    let conn_pool = get_privileged_connection_pool().await;
                    let conn = &mut conn_pool.get().await.unwrap();

                    // database must not exist
                    assert!(!database_exists(db_name, conn).await);

                    // database must exist after creating through backend
                    backend.init().await.unwrap();
                    backend.create(db_id, false).await.unwrap();
                    assert!(database_exists(db_name, conn).await);
                }

                // DML statements must succeed
                {
                    let conn_pool = create_restricted_connection_pool(db_name).await;
                    let conn = &mut conn_pool.get().await.unwrap();
                    for stmt in DML_STATEMENTS {
                        assert!(sql_query(stmt).execute(conn).await.is_ok());
                    }
                }
            }

            // DDL statements must succeed
            for stmt in DDL_STATEMENTS {
                let db_id = Uuid::new_v4();
                let db_name = get_db_name(db_id);
                let db_name = db_name.as_str();

                backend.create(db_id, false).await.unwrap();
                let conn_pool = create_restricted_connection_pool(db_name).await;
                let conn = &mut conn_pool.get().await.unwrap();

                assert!(sql_query(stmt).execute(conn).await.is_ok());
            }
        }
        .lock_read()
        .await;
    }

    pub async fn test_backend_cleans_database_with_tables(backend: impl Backend) {
        const NUM_BOOKS: i64 = 3;

        let db_id = Uuid::new_v4();
        let db_name = get_db_name(db_id);
        let db_name = db_name.as_str();

        async {
            backend.init().await.unwrap();
            backend.create(db_id, true).await.unwrap();

            table! {
                book (id) {
                    id -> Int4,
                    title -> Text
                }
            }

            #[derive(Insertable)]
            #[diesel(table_name = book)]
            struct NewBook {
                title: String,
            }

            let conn_pool = create_restricted_connection_pool(db_name).await;
            let conn = &mut conn_pool.get().await.unwrap();

            let new_books = (0..NUM_BOOKS)
                .map(|i| NewBook {
                    title: format!("Title {}", i + 1),
                })
                .collect::<Vec<_>>();
            insert_into(book::table)
                .values(&new_books)
                .execute(conn)
                .await
                .unwrap();

            // there must be books
            assert_eq!(
                book::table.count().get_result::<i64>(conn).await.unwrap(),
                NUM_BOOKS
            );

            backend.clean(db_id).await.unwrap();

            // there must be no books
            assert_eq!(
                book::table.count().get_result::<i64>(conn).await.unwrap(),
                0
            );
        }
        .lock_read()
        .await;
    }

    pub async fn test_backend_cleans_database_without_tables(backend: impl Backend) {
        let db_id = Uuid::new_v4();

        async {
            backend.init().await.unwrap();
            backend.create(db_id, true).await.unwrap();
            backend.clean(db_id).await.unwrap();
        }
        .lock_read()
        .await;
    }

    pub async fn test_backend_drops_database(backend: impl Backend, restricted: bool) {
        let db_id = Uuid::new_v4();
        let db_name = get_db_name(db_id);
        let db_name = db_name.as_str();

        async {
            let conn_pool = get_privileged_connection_pool().await;
            let conn = &mut conn_pool.get().await.unwrap();

            // database must exist
            backend.init().await.unwrap();
            backend.create(db_id, restricted).await.unwrap();
            assert!(database_exists(db_name, conn).await);

            // database must not exist
            backend.drop(db_id, true).await.unwrap();
            assert!(!database_exists(db_name, conn).await);
        }
        .lock_read()
        .await;
    }

    pub async fn test_pool_drops_previous_databases<B: Backend>(
        default: B,
        enabled: B,
        disabled: B,
    ) {
        const NUM_DBS: i64 = 3;

        async {
            let conn_pool = get_privileged_connection_pool().await;
            let conn = &mut conn_pool.get().await.unwrap();

            for (backend, cleans) in [(default, true), (enabled, true), (disabled, false)] {
                let db_names = create_databases(NUM_DBS, conn_pool).await;
                assert_eq!(count_databases(&db_names, conn).await, NUM_DBS);
                backend.create_database_pool().await.unwrap();
                assert_eq!(
                    count_databases(&db_names, conn).await,
                    if cleans { 0 } else { NUM_DBS }
                );
            }
        }
        .lock_drop()
        .await;
    }

    pub async fn test_pool_drops_created_restricted_databases(backend: impl Backend) {
        const NUM_DBS: i64 = 3;

        let conn_pool = get_privileged_connection_pool().await;
        let conn = &mut conn_pool.get().await.unwrap();

        async {
            let db_pool = backend.create_database_pool().await.unwrap();

            // there must be no databases
            assert_eq!(count_all_databases(conn).await, 0);

            // fetch connection pools
            let conn_pools = join_all((0..NUM_DBS).map(|_| db_pool.pull_immutable())).await;

            // there must be databases
            assert_eq!(count_all_databases(conn).await, NUM_DBS);

            // must release databases back to pool
            drop(conn_pools);

            // there must be databases
            assert_eq!(count_all_databases(conn).await, NUM_DBS);

            // must drop databases
            drop(db_pool);

            // there must be no databases
            assert_eq!(count_all_databases(conn).await, 0);
        }
        .lock_drop()
        .await;
    }

    pub async fn test_pool_drops_created_unrestricted_database(backend: impl Backend) {
        let conn_pool = get_privileged_connection_pool().await;
        let conn = &mut conn_pool.get().await.unwrap();

        async {
            let db_pool = backend.create_database_pool().await.unwrap();

            // there must be no databases
            assert_eq!(count_all_databases(conn).await, 0);

            // fetch connection pool
            let conn_pool = db_pool.create_mutable().await.unwrap();

            // there must be a database
            assert_eq!(count_all_databases(conn).await, 1);

            // must drop database
            drop(conn_pool);

            // there must be no databases
            assert_eq!(count_all_databases(conn).await, 0);

            drop(db_pool);

            // there must be no databases
            assert_eq!(count_all_databases(conn).await, 0);
        }
        .lock_drop()
        .await;
    }
}