about summary refs log tree commit diff
path: root/src/database/postgres
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2024-08-01 19:48:37 +0300
committerVika <vika@fireburn.ru>2024-08-01 20:40:00 +0300
commit7e8e688e2e58f9c944b941e768ab7b034a348a1f (patch)
tree1068469c6b9b97bac407038276fd8971b2101e48 /src/database/postgres
parent57a9c3c7e520714928904fc7e2ff3d62ac2b2467 (diff)
downloadkittybox-7e8e688e2e58f9c944b941e768ab7b034a348a1f.tar.zst
treewide: create a common method for state initialization
Now the database objects can be uniformly created from a URI. They can
also optionally do sanity checks and one-time initialization.
Diffstat (limited to 'src/database/postgres')
-rw-r--r--src/database/postgres/mod.rs30
1 files changed, 15 insertions, 15 deletions
diff --git a/src/database/postgres/mod.rs b/src/database/postgres/mod.rs
index 7813045..0ebaffb 100644
--- a/src/database/postgres/mod.rs
+++ b/src/database/postgres/mod.rs
@@ -2,7 +2,7 @@ use std::borrow::Cow;
 use std::str::FromStr;
 
 use kittybox_util::{MicropubChannel, MentionType};
-use sqlx::{PgPool, Executor};
+use sqlx::{ConnectOptions, Executor, PgPool};
 use crate::micropub::{MicropubUpdate, MicropubPropertyDeletion};
 
 use super::settings::Setting;
@@ -36,6 +36,17 @@ pub struct PostgresStorage {
 }
 
 impl PostgresStorage {
+    /// Construct a [`PostgresStorage`] from a [`sqlx::PgPool`],
+    /// running appropriate migrations.
+    pub async fn from_pool(db: sqlx::PgPool) -> Result<Self> {
+        db.execute(sqlx::query("CREATE SCHEMA IF NOT EXISTS kittybox")).await?;
+        MIGRATOR.run(&db).await?;
+        Ok(Self { db })
+    }
+}
+
+#[async_trait::async_trait]
+impl Storage for PostgresStorage {
     /// Construct a new [`PostgresStorage`] from an URI string and run
     /// migrations on the database.
     ///
@@ -43,9 +54,9 @@ impl PostgresStorage {
     /// password from the file at the specified path. If, instead,
     /// the `PGPASS` environment variable is present, read the
     /// password from it.
-    pub async fn new(uri: &str) -> Result<Self> {
-        tracing::debug!("Postgres URL: {uri}");
-        let mut options = sqlx::postgres::PgConnectOptions::from_str(uri)?
+    async fn new(url: &'_ url::Url) -> Result<Self> {
+        tracing::debug!("Postgres URL: {url}");
+        let mut options = sqlx::postgres::PgConnectOptions::from_url(url)?
             .options([("search_path", "kittybox")]);
         if let Ok(password_file) = std::env::var("PGPASS_FILE") {
             let password = tokio::fs::read_to_string(password_file).await.unwrap();
@@ -62,17 +73,6 @@ impl PostgresStorage {
 
     }
 
-    /// Construct a [`PostgresStorage`] from a [`sqlx::PgPool`],
-    /// running appropriate migrations.
-    pub async fn from_pool(db: sqlx::PgPool) -> Result<Self> {
-        db.execute(sqlx::query("CREATE SCHEMA IF NOT EXISTS kittybox")).await?;
-        MIGRATOR.run(&db).await?;
-        Ok(Self { db })
-    }
-}
-
-#[async_trait::async_trait]
-impl Storage for PostgresStorage {
     #[tracing::instrument(skip(self))]
     async fn categories(&self, url: &str) -> Result<Vec<String>> {
         sqlx::query_scalar::<_, String>("