add postgress boilerplate
This commit is contained in:
parent
5548e37234
commit
f5e7e0a36e
1 changed files with 24 additions and 2 deletions
|
@ -1,10 +1,11 @@
|
||||||
|
#![allow(unused)]
|
||||||
use log::{info, warn};
|
use log::{info, warn};
|
||||||
use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool};
|
use sqlx::{migrate::MigrateDatabase, PgPool, Postgres, Sqlite, SqlitePool};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum DataBase {
|
pub enum DataBase {
|
||||||
Sqlite(sqlx::Pool<Sqlite>),
|
Sqlite(sqlx::Pool<Sqlite>),
|
||||||
//Postgres(sqlx::Pool<Postgres>),
|
Postgres(sqlx::Pool<Postgres>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DataBase {
|
impl DataBase {
|
||||||
|
@ -28,4 +29,25 @@ impl DataBase {
|
||||||
|
|
||||||
Self::Sqlite(pool)
|
Self::Sqlite(pool)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn postgres(url: &str) -> Self {
|
||||||
|
if !sqlx::Postgres::database_exists(url)
|
||||||
|
.await
|
||||||
|
.expect("failed to connect to db")
|
||||||
|
{
|
||||||
|
warn!("No Postgres database found, if this is the first time you are starting Gunnhildr, you can safely ignore this.");
|
||||||
|
sqlx::Postgres::create_database(url)
|
||||||
|
.await
|
||||||
|
.expect("failed to create Postgres Database!");
|
||||||
|
info!("Created new Postgres Database");
|
||||||
|
}
|
||||||
|
|
||||||
|
let pool = PgPool::connect("url").await.unwrap();
|
||||||
|
sqlx::migrate!("migrations/postgres")
|
||||||
|
.run(&pool)
|
||||||
|
.await
|
||||||
|
.expect("Failed to apply migration!");
|
||||||
|
|
||||||
|
Self::Postgres(pool)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue