Skip to content

Commit

Permalink
DatabaseConnection directly from PgPool (#2348)
Browse files Browse the repository at this point in the history
* feat(postgres): create `DatabaseConnection` directly from `PgPool`

This allows sqlx-based connection pools that were already on-hand to be
easily converted into SeaORM types.

* Move `From` impls into driver

* impls for MySQL and SQLite

* CHANGELOG

* fmt

---------

Co-authored-by: Billy Chan <[email protected]>
  • Loading branch information
fosskers and billy1624 authored Oct 4, 2024
1 parent fb44229 commit 8339dbb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Enhancements

* [sea-orm-macros] Call `EnumIter::get` using fully qualified syntax https://github.com/SeaQL/sea-orm/pull/2321
* Construct `DatabaseConnection` directly from `sqlx::PgPool`, `sqlx::SqlitePool` and `sqlx::MySqlPool` https://github.com/SeaQL/sea-orm/pull/2348

### Upgrades

Expand Down
15 changes: 15 additions & 0 deletions src/driver/sqlx_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ impl std::fmt::Debug for SqlxMySqlPoolConnection {
}
}

impl From<MySqlPool> for SqlxMySqlPoolConnection {
fn from(pool: MySqlPool) -> Self {
SqlxMySqlPoolConnection {
pool,
metric_callback: None,
}
}
}

impl From<MySqlPool> for DatabaseConnection {
fn from(pool: MySqlPool) -> Self {
DatabaseConnection::SqlxMySqlPoolConnection(pool.into())
}
}

impl SqlxMySqlConnector {
/// Check if the URI provided corresponds to `mysql://` for a MySQL database
pub fn accepts(string: &str) -> bool {
Expand Down
15 changes: 15 additions & 0 deletions src/driver/sqlx_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ impl std::fmt::Debug for SqlxPostgresPoolConnection {
}
}

impl From<PgPool> for SqlxPostgresPoolConnection {
fn from(pool: PgPool) -> Self {
SqlxPostgresPoolConnection {
pool,
metric_callback: None,
}
}
}

impl From<PgPool> for DatabaseConnection {
fn from(pool: PgPool) -> Self {
DatabaseConnection::SqlxPostgresPoolConnection(pool.into())
}
}

impl SqlxPostgresConnector {
/// Check if the URI provided corresponds to `postgres://` for a PostgreSQL database
pub fn accepts(string: &str) -> bool {
Expand Down
15 changes: 15 additions & 0 deletions src/driver/sqlx_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ impl std::fmt::Debug for SqlxSqlitePoolConnection {
}
}

impl From<SqlitePool> for SqlxSqlitePoolConnection {
fn from(pool: SqlitePool) -> Self {
SqlxSqlitePoolConnection {
pool,
metric_callback: None,
}
}
}

impl From<SqlitePool> for DatabaseConnection {
fn from(pool: SqlitePool) -> Self {
DatabaseConnection::SqlxSqlitePoolConnection(pool.into())
}
}

impl SqlxSqliteConnector {
/// Check if the URI provided corresponds to `sqlite:` for a SQLite database
pub fn accepts(string: &str) -> bool {
Expand Down

0 comments on commit 8339dbb

Please sign in to comment.