Skip to main content

toasty_driver_mysql/
mariadb.rs

1use super::MySQL;
2use async_trait::async_trait;
3use std::borrow::Cow;
4use toasty_core::{
5    Result,
6    driver::{Capability, ConnectContext, Connection, Driver},
7    schema::{db::Migration, diff},
8};
9
10/// A MariaDB 11.8+ [`Driver`] that connects through SQLx.
11///
12/// Uses MariaDB's native `UUID` type and `INSERT ... RETURNING`.
13///
14/// # Examples
15///
16/// ```
17/// use toasty_driver_mysql::MariaDB;
18///
19/// let driver = MariaDB::new("mariadb://localhost/mydb").unwrap();
20/// ```
21#[derive(Debug)]
22pub struct MariaDB {
23    inner: MySQL,
24}
25
26impl MariaDB {
27    /// Creates a MariaDB driver without connecting to the server.
28    ///
29    /// The URL must use the `mariadb` scheme and include a database path,
30    /// such as `mariadb://user:pass@host:3306/dbname`. The server must run
31    /// MariaDB 11.8 or later.
32    ///
33    /// # Errors
34    ///
35    /// Returns an error if the URL is malformed or uses another scheme.
36    pub fn new(url: impl Into<String>) -> Result<Self> {
37        Ok(Self {
38            inner: MySQL::with_capability(url, "mariadb", &Capability::MARIADB)?,
39        })
40    }
41}
42
43#[async_trait]
44impl Driver for MariaDB {
45    fn url(&self) -> Cow<'_, str> {
46        self.inner.url()
47    }
48
49    fn capability(&self) -> &'static Capability {
50        &Capability::MARIADB
51    }
52
53    async fn connect(&self, cx: &ConnectContext) -> Result<Box<dyn Connection>> {
54        self.inner.connect(cx).await
55    }
56
57    fn generate_migration(&self, schema_diff: &diff::Schema<'_>) -> Migration {
58        self.inner.generate_migration(schema_diff)
59    }
60
61    async fn reset_db(&self) -> Result<()> {
62        self.inner.reset_db().await
63    }
64}