about summary refs log tree commit diff stats
path: root/crates/yt/src/app.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/yt/src/app.rs')
-rw-r--r--crates/yt/src/app.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/crates/yt/src/app.rs b/crates/yt/src/app.rs
new file mode 100644
index 0000000..15a9388
--- /dev/null
+++ b/crates/yt/src/app.rs
@@ -0,0 +1,50 @@
+// yt - A fully featured command line YouTube client
+//
+// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// This file is part of Yt.
+//
+// You should have received a copy of the License along with this program.
+// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
+
+use anyhow::{Context, Result};
+use log::warn;
+use sqlx::{SqlitePool, sqlite::SqliteConnectOptions};
+
+use crate::{config::Config, storage::migrate::migrate_db};
+
+#[derive(Debug)]
+pub struct App {
+    pub database: SqlitePool,
+    pub config: Config,
+}
+
+impl App {
+    pub async fn new(config: Config, should_migrate_db: bool) -> Result<Self> {
+        let options = SqliteConnectOptions::new()
+            .filename(&config.paths.database_path)
+            .optimize_on_close(true, None)
+            .create_if_missing(true);
+
+        let pool = SqlitePool::connect_with(options)
+            .await
+            .context("Failed to connect to database!")?;
+
+        let app = App {
+            database: pool,
+            config,
+        };
+
+        if should_migrate_db {
+            migrate_db(&app)
+                .await
+                .context("Failed to migrate db to new version")?;
+        } else {
+            warn!("Skipping database migration.");
+        }
+
+        Ok(app)
+    }
+}