feat: db utils

This commit is contained in:
Aravinth Manivannan 2024-05-13 21:27:51 +05:30
parent ef50407f3f
commit df4c07344d
Signed by: realaravinth
GPG key ID: F8F50389936984FF
9 changed files with 1856 additions and 0 deletions

View file

@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
#[async_trait::async_trait]
pub trait CreateDatabase: Send + Sync {
async fn create_database(&self, url: &url::Url);
}

View file

@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
#[async_trait::async_trait]
pub trait DeleteDatabase: Send + Sync {
async fn delete_database(&self, name: &url::Url);
}

8
src/db/migrate.rs Normal file
View file

@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
#[async_trait::async_trait]
pub trait RunMigrations: Send + Sync {
async fn migrate(&self);
}

8
src/db/mod.rs Normal file
View file

@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
pub mod create_database;
pub mod delete_database;
pub mod migrate;
pub mod sqlx_postgres;

53
src/db/sqlx_postgres.rs Normal file
View file

@ -0,0 +1,53 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// 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();
}
}

1
utils/db-migrations/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

1737
utils/db-migrations/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,10 @@
[package]
name = "db-migrations"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-rt = "2.9.0"
sqlx = { version = "0.7.4", features = ["runtime-tokio-rustls", "postgres", "time"] }

View file

@ -0,0 +1,23 @@
use std::env;
use sqlx::postgres::PgPoolOptions;
#[actix_rt::main]
async fn main() {
//TODO featuregate sqlite and postgres
postgres_migrate().await;
}
async fn postgres_migrate() {
let db_url = env::var("DATABASE_URL").expect("set DATABASE_URL env var");
let db = PgPoolOptions::new()
.max_connections(2)
.connect(&db_url)
.await
.expect("Unable to form database pool");
sqlx::migrate!("../../migrations/")
.run(&db)
.await
.unwrap();
}