• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

polyphony-chat / sonata / 15957510251

29 Jun 2025 05:09PM UTC coverage: 68.553% (-10.3%) from 78.899%
15957510251

push

github

bitfl0wer
fix: disable mcdc coverage to prevent nightly rust crash

0 of 8 branches covered (0.0%)

Branch coverage included in aggregate %.

218 of 310 relevant lines covered (70.32%)

598.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/src/database/mod.rs
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4

5
use sqlx::PgPool;
6
use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
7

8
use crate::StdResult;
9
use crate::config::DatabaseConfig;
10

11
pub(crate) mod api_keys;
12
pub(crate) mod models;
13
pub(crate) mod serial_number;
14

15
pub(crate) use models::*;
16

17
#[derive(Debug, Clone)]
18
/// Main Database struct. Wrapper around [PgPool].
19
pub(crate) struct Database {
20
    /// The underlying `sqlx` [PgPool].
21
    pub pool: PgPool,
22
}
23

24
impl Database {
25
    /// Connect to the PostgreSQL Database using configuration options provided through [DatabaseConfig],
26
    /// which is most commonly derived by parsing a [SonataConfiguration].
27
    #[cfg_attr(coverage_nightly, coverage(off))]
28
    pub async fn connect_with_config(config: &DatabaseConfig) -> StdResult<Self> {
29
        let connect_options = PgConnectOptions::new()
30
            .host(&config.host)
31
            .database(&config.database)
32
            .application_name("sonata")
33
            .password(&config.password)
34
            .port(config.port)
35
            .ssl_mode(match config.tls {
36
                crate::config::TlsConfig::Disable => sqlx::postgres::PgSslMode::Disable,
37
                crate::config::TlsConfig::Allow => sqlx::postgres::PgSslMode::Allow,
38
                crate::config::TlsConfig::Prefer => sqlx::postgres::PgSslMode::Prefer,
39
                crate::config::TlsConfig::Require => sqlx::postgres::PgSslMode::Require,
40
                crate::config::TlsConfig::VerifyCa => sqlx::postgres::PgSslMode::VerifyCa,
41
                crate::config::TlsConfig::VerifyFull => sqlx::postgres::PgSslMode::VerifyFull,
42
            })
43
            .username(&config.username);
44
        let pool = PgPoolOptions::new()
45
            .max_connections(config.max_connections)
46
            .connect_with(connect_options)
47
            .await?;
48
        Ok(Self { pool })
49
    }
50

51
    /// Applies the migrations.
52
    pub(super) async fn run_migrations(&self) -> StdResult<()> {
×
53
        sqlx::migrate!().run(&self.pool).await.map_err(|e| e.into())
×
54
    }
×
55
}
56

57
#[cfg(test)]
58
mod tests {
59
    use super::*;
60
    use crate::config::TlsConfig;
61

62
    #[test]
63
    fn test_database_debug() {
1✔
64
        // We can't easily test the actual Database struct without a real connection,
65
        // but we can test that it implements Debug
66
        // This is a compile-time test to ensure Debug is implemented
67
        fn assert_debug<T: std::fmt::Debug>() {}
1✔
68
        assert_debug::<Database>();
1✔
69
    }
1✔
70

71
    #[test]
72
    fn test_database_clone() {
1✔
73
        // This is a compile-time test to ensure Clone is implemented
74
        fn assert_clone<T: Clone>() {}
1✔
75
        assert_clone::<Database>();
1✔
76
    }
1✔
77

78
    #[tokio::test]
79
    async fn test_connect_with_config_invalid() {
1✔
80
        let config = DatabaseConfig {
1✔
81
            max_connections: 10,
1✔
82
            database: "nonexistent".to_owned(),
1✔
83
            username: "invalid".to_owned(),
1✔
84
            password: "invalid".to_owned(),
1✔
85
            port: 5432,
1✔
86
            host: "invalid_host".to_owned(),
1✔
87
            tls: TlsConfig::Disable,
1✔
88
        };
1✔
89

90
        // This should fail to connect
91
        let result = Database::connect_with_config(&config).await;
1✔
92
        assert!(result.is_err());
1✔
93
    }
1✔
94

95
    // Note: Testing actual database connections and migrations would require
96
    // either a test database or mocking, which is typically done in integration tests
97
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc