// SPDX-FileCopyrightText: 2024 Aravinth Manivannan // // SPDX-License-Identifier: AGPL-3.0-or-later use sqlx::migrate::MigrateDatabase; use sqlx::postgres::PgPool; use super::migrate::RunMigrations; #[derive(Clone)] pub struct Postgres { pub pool: PgPool, } impl Postgres { pub fn new(pool: PgPool) -> Self { Self { pool } } pub async fn init(database_url: &str) -> Self { let pool = PgPool::connect(database_url).await.unwrap(); Self::new(pool) } } #[async_trait::async_trait] impl RunMigrations for Postgres { async fn migrate(&self) { sqlx::migrate!("./migrations/") .run(&self.pool) .await .unwrap(); } } pub struct PostgresDatabase; #[async_trait::async_trait] impl super::create_database::CreateDatabase for PostgresDatabase { async fn create_database(&self, url: &url::Url) { sqlx::Postgres::create_database(url.as_str()).await.unwrap(); } } #[async_trait::async_trait] impl super::delete_database::DeleteDatabase for PostgresDatabase { async fn delete_database(&self, url: &url::Url) { sqlx::Postgres::force_drop_database(url.as_str()) .await .unwrap(); } }