begin implementing database. Down migrate doesnt work yet
This commit is contained in:
6112
src-tauri/Cargo.lock
generated
Normal file
6112
src-tauri/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -23,3 +23,7 @@ tauri-plugin-opener = "2"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
|
||||
# SQLx und Tokio für asynchrone DB-Zugriffe
|
||||
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "sqlite", "macros", "migrate"] }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
|
||||
|
||||
8
src-tauri/migrations/0001_path_node_table.sql
Normal file
8
src-tauri/migrations/0001_path_node_table.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
-- migrate:up
|
||||
CREATE TABLE PathNode (
|
||||
id integer primary key,
|
||||
title text,
|
||||
description text
|
||||
)
|
||||
|
||||
-- migrate:down
|
||||
8
src-tauri/migrations/0002_node_exercise.sql
Normal file
8
src-tauri/migrations/0002_node_exercise.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
-- migrate:up
|
||||
create table NodeExercise (
|
||||
id integer primary key,
|
||||
ex_type text,
|
||||
content text
|
||||
)
|
||||
|
||||
-- migrate:down
|
||||
@@ -1,14 +1,50 @@
|
||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||
use sqlx::sqlite::{self, SqlitePool};
|
||||
|
||||
#[tauri::command]
|
||||
fn greet(name: &str) -> String {
|
||||
format!("Hello, {}! You've been greeted from Rust!", name)
|
||||
}
|
||||
|
||||
// Beispiel: SQLite-Version abfragen
|
||||
#[tauri::command]
|
||||
async fn db_version(app_handle: tauri::AppHandle) -> Result<String, String> {
|
||||
use tauri::Manager;
|
||||
let base_dir = app_handle.path().app_data_dir().map_err(|e| format!("Konnte AppData-Verzeichnis nicht bestimmen: {}", e))?;
|
||||
|
||||
let db_path = base_dir.join("paths.sqlite");
|
||||
println!("{:?}", db_path.to_str());
|
||||
|
||||
if let Some(parent) = db_path.parent() {
|
||||
std::fs::create_dir_all(parent).map_err(|e| format!("Konnte Datenbankverzeichnis nicht erstellen: {}", e))?;
|
||||
}
|
||||
|
||||
let db_opts = sqlite::SqliteConnectOptions::new()
|
||||
.filename(&db_path)
|
||||
.create_if_missing(true);
|
||||
|
||||
let pool = SqlitePool::connect_with(db_opts)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
sqlx::migrate!("./migrations").run(&pool).await.map_err(|e| e.to_string())?;
|
||||
|
||||
let row: (String,) = sqlx::query_as("SELECT sqlite_version()")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(row.0)
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.invoke_handler(tauri::generate_handler![greet])
|
||||
.invoke_handler(tauri::generate_handler![greet, db_version])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
||||
// Hinweis: Für produktive Nutzung sollte der Pool als State eingebunden werden,
|
||||
// damit er nicht bei jedem Aufruf neu erstellt wird. Siehe Tauri-Doku:
|
||||
// https://tauri.app/v1/guides/features/state/
|
||||
|
||||
Reference in New Issue
Block a user