Browse Source

✨ refactor(database): 移除不必要的 Tauri 插件,重构数据库迁移逻辑

陈雪 23 hours ago
parent
commit
09c1879afc
6 changed files with 61 additions and 99 deletions
  1. 0 3
      package.json
  2. 0 30
      pnpm-lock.yaml
  3. 0 3
      src-tauri/Cargo.toml
  4. 0 8
      src-tauri/capabilities/default.json
  5. 10 55
      src-tauri/src/lib.rs
  6. 51 0
      src-tauri/src/utils.rs

+ 0 - 3
package.json

@@ -18,11 +18,8 @@
     "@element-plus/icons-vue": "^2.3.1",
     "@tailwindcss/vite": "^4.0.8",
     "@tauri-apps/api": "^2",
-    "@tauri-apps/plugin-http": "~2",
-    "@tauri-apps/plugin-opener": "^2",
     "@tauri-apps/plugin-os": "~2",
     "@tauri-apps/plugin-store": "~2",
-    "@tauri-apps/plugin-upload": "~2",
     "element-plus": "^2.9.5",
     "pinia": "^3.0.1",
     "tailwindcss": "^4.0.8",

+ 0 - 30
pnpm-lock.yaml

@@ -17,21 +17,12 @@ importers:
       '@tauri-apps/api':
         specifier: ^2
         version: 2.2.0
-      '@tauri-apps/plugin-http':
-        specifier: ~2
-        version: 2.3.0
-      '@tauri-apps/plugin-opener':
-        specifier: ^2
-        version: 2.2.5
       '@tauri-apps/plugin-os':
         specifier: ~2
         version: 2.2.0
       '@tauri-apps/plugin-store':
         specifier: ~2
         version: 2.2.0
-      '@tauri-apps/plugin-upload':
-        specifier: ~2
-        version: 2.2.1
       element-plus:
         specifier: ^2.9.5
         version: 2.9.5(vue@3.5.13(typescript@5.7.3))
@@ -537,21 +528,12 @@ packages:
     engines: {node: '>= 10'}
     hasBin: true
 
-  '@tauri-apps/plugin-http@2.3.0':
-    resolution: {integrity: sha512-pigTvz+zzAqbIhCzRiR1GE98Jw7A03j2V+Eiexr9thBI8VfMiwFQMcbgON51xlwnVaI72LdbYKNajU84im8tlg==}
-
-  '@tauri-apps/plugin-opener@2.2.5':
-    resolution: {integrity: sha512-hHsJ9RPWpZvZEPVFaL+d25gABMUMOf/A6ESXnvf/ii9guTukj58WXsAE/SOysXRIhej7kseRCxnOnIMpSCdUsQ==}
-
   '@tauri-apps/plugin-os@2.2.0':
     resolution: {integrity: sha512-HszbCdbisMlu5QhCNAN8YIWyz2v33abAWha6+uvV2CKX8P5VSct/y+kEe22JeyqrxCnWlQ3DRx7s49Byg7/0EA==}
 
   '@tauri-apps/plugin-store@2.2.0':
     resolution: {integrity: sha512-hJTRtuJis4w5fW1dkcgftsYxKXK0+DbAqurZ3CURHG5WkAyyZgbxpeYctw12bbzF9ZbZREXZklPq8mocCC3Sgg==}
 
-  '@tauri-apps/plugin-upload@2.2.1':
-    resolution: {integrity: sha512-2EyVhJYLAp2mJH0UzO3QGU0vPk/YWvAfdI2wXbczyzEZY/AZVa9wConyB1TV/NGhyJRim4LwWgkmnEvcKLkECw==}
-
   '@types/estree@1.0.6':
     resolution: {integrity: sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==}
 
@@ -1381,14 +1363,6 @@ snapshots:
       '@tauri-apps/cli-win32-ia32-msvc': 2.2.7
       '@tauri-apps/cli-win32-x64-msvc': 2.2.7
 
-  '@tauri-apps/plugin-http@2.3.0':
-    dependencies:
-      '@tauri-apps/api': 2.2.0
-
-  '@tauri-apps/plugin-opener@2.2.5':
-    dependencies:
-      '@tauri-apps/api': 2.2.0
-
   '@tauri-apps/plugin-os@2.2.0':
     dependencies:
       '@tauri-apps/api': 2.2.0
@@ -1397,10 +1371,6 @@ snapshots:
     dependencies:
       '@tauri-apps/api': 2.2.0
 
-  '@tauri-apps/plugin-upload@2.2.1':
-    dependencies:
-      '@tauri-apps/api': 2.2.0
-
   '@types/estree@1.0.6': {}
 
   '@types/lodash-es@4.17.12':

+ 0 - 3
src-tauri/Cargo.toml

@@ -24,12 +24,9 @@ sea-orm = { version = "1.1.0", features = [
     "macros",
 ] }
 tauri = { version = "2", features = [] }
-tauri-plugin-opener = "2"
 serde = { version = "1", features = ["derive"] }
 serde_json = "1"
 tauri-plugin-store = "2"
-tauri-plugin-http = "2"
-tauri-plugin-upload = "2"
 tauri-plugin-os = "2"
 anyhow = "1.0.97"
 

+ 0 - 8
src-tauri/capabilities/default.json

@@ -7,15 +7,7 @@
   ],
   "permissions": [
     "core:default",
-    "opener:default",
     "store:default",
-    {
-      "identifier": "http:default",
-      "allow": [
-        "http://192.168.1.110:8080"
-      ]
-    },
-    "upload:default",
     "os:default"
   ]
 }

+ 10 - 55
src-tauri/src/lib.rs

@@ -1,12 +1,14 @@
 mod commands;
 mod models;
 mod service;
+mod utils;
 
-use sea_orm::{ConnectionTrait, DatabaseConnection, Statement, TransactionTrait};
-use std::fs::{self, File};
+use commands::{category, table};
+use sea_orm::{ConnectionTrait, DatabaseConnection};
+use std::fs::File;
 use std::sync::Arc;
 use tauri::State;
-use tauri::{AppHandle, Manager, path::BaseDirectory};
+use tauri::{AppHandle, Manager};
 
 type CmdState<'a> = State<'a, AppState>;
 type CmdResult<T> = Result<T, String>;
@@ -34,22 +36,22 @@ pub fn run() {
                 conn.execute_unprepared("PRAGMA journal_mode=WAL;")
                     .await
                     .map_err(|e| anyhow::anyhow!("Failed to set WAL mode: {}", e))?;
-                migration_db(app, &conn).await?;
+                utils::migration_db(app, &conn).await?;
                 app.manage(AppState {
                     conn: Arc::new(conn),
                 });
                 Ok(())
             })
         })
-        .invoke_handler(tauri::generate_handler![commands::get_category])
+        .invoke_handler(tauri::generate_handler![
+            table::get_table,
+            category::get_category
+        ])
         .plugin(tauri_plugin_os::init())
         .plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {
             let _ = show_window(app);
         }))
-        .plugin(tauri_plugin_upload::init())
-        .plugin(tauri_plugin_http::init())
         .plugin(tauri_plugin_store::Builder::new().build())
-        .plugin(tauri_plugin_opener::init())
         .run(tauri::generate_context!())
         .expect("error while running tauri application");
 }
@@ -64,50 +66,3 @@ fn show_window(app: &AppHandle) {
         .set_focus()
         .expect("Can't Bring Window to Focus");
 }
-
-async fn migration_db(app: &tauri::App, conn: &DatabaseConnection) -> anyhow::Result<()> {
-    let sql_path = app
-        .path()
-        .resolve("resources/table_struct.sql", BaseDirectory::Resource)?;
-    conn.execute(Statement::from_string(
-        conn.get_database_backend(),
-        "CREATE TABLE IF NOT EXISTS seaql_migrations (
-            version VARCHAR(255) PRIMARY KEY,
-            applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-        )"
-        .to_owned(),
-    ))
-    .await?;
-    let applied: Vec<String> = conn
-        .query_all(Statement::from_string(
-            conn.get_database_backend(),
-            "SELECT version FROM seaql_migrations".to_owned(),
-        ))
-        .await?
-        .into_iter()
-        .filter_map(|row| row.try_get("", "version").ok())
-        .collect();
-
-    if let Some(sql_path_stem) = sql_path.file_stem() {
-        if let Some(version) = sql_path_stem.to_str() {
-            if !applied.contains(&version.to_string()) {
-                let sql_str = fs::read_to_string(&sql_path)?;
-                let tx = conn.begin().await?;
-                tx.execute(Statement::from_string(conn.get_database_backend(), sql_str))
-                    .await?;
-                tx.execute(Statement::from_string(
-                    conn.get_database_backend(),
-                    format!(
-                        "INSERT INTO seaql_migrations (version) VALUES ('{}')",
-                        version
-                    ),
-                ))
-                .await?;
-                tx.commit().await?;
-                println!("✅ Applied migration: {}", version);
-            }
-        }
-    }
-
-    Ok(())
-}

+ 51 - 0
src-tauri/src/utils.rs

@@ -0,0 +1,51 @@
+use std::fs;
+
+use sea_orm::{ConnectionTrait, DatabaseConnection, Statement, TransactionTrait};
+use tauri::{Manager, path::BaseDirectory};
+
+pub async fn migration_db(app: &tauri::App, conn: &DatabaseConnection) -> anyhow::Result<()> {
+    let sql_path = app
+        .path()
+        .resolve("resources/table_struct.sql", BaseDirectory::Resource)?;
+    conn.execute(Statement::from_string(
+        conn.get_database_backend(),
+        "CREATE TABLE IF NOT EXISTS seaql_migrations (
+            version VARCHAR(255) PRIMARY KEY,
+            applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+        )"
+        .to_owned(),
+    ))
+    .await?;
+    let applied: Vec<String> = conn
+        .query_all(Statement::from_string(
+            conn.get_database_backend(),
+            "SELECT version FROM seaql_migrations".to_owned(),
+        ))
+        .await?
+        .into_iter()
+        .filter_map(|row| row.try_get("", "version").ok())
+        .collect();
+
+    if let Some(sql_path_stem) = sql_path.file_stem() {
+        if let Some(version) = sql_path_stem.to_str() {
+            if !applied.contains(&version.to_string()) {
+                let sql_str = fs::read_to_string(&sql_path)?;
+                let tx = conn.begin().await?;
+                tx.execute(Statement::from_string(conn.get_database_backend(), sql_str))
+                    .await?;
+                tx.execute(Statement::from_string(
+                    conn.get_database_backend(),
+                    format!(
+                        "INSERT INTO seaql_migrations (version) VALUES ('{}')",
+                        version
+                    ),
+                ))
+                .await?;
+                tx.commit().await?;
+                println!("✅ Applied migration: {}", version);
+            }
+        }
+    }
+
+    Ok(())
+}