feat: db utils
This commit is contained in:
parent
ef50407f3f
commit
df4c07344d
9 changed files with 1856 additions and 0 deletions
8
src/db/create_database.rs
Normal file
8
src/db/create_database.rs
Normal 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);
|
||||
}
|
8
src/db/delete_database.rs
Normal file
8
src/db/delete_database.rs
Normal 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
8
src/db/migrate.rs
Normal 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
8
src/db/mod.rs
Normal 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
53
src/db/sqlx_postgres.rs
Normal 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
1
utils/db-migrations/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
1737
utils/db-migrations/Cargo.lock
generated
Normal file
1737
utils/db-migrations/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
10
utils/db-migrations/Cargo.toml
Normal file
10
utils/db-migrations/Cargo.toml
Normal 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"] }
|
23
utils/db-migrations/src/main.rs
Normal file
23
utils/db-migrations/src/main.rs
Normal 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();
|
||||
}
|
Loading…
Reference in a new issue