Ver código fonte

加入可控的字段版本管理

john 1 ano atrás
pai
commit
d410da03af

+ 2 - 1
package.json

@@ -20,7 +20,8 @@
     "react": "^18.3.1",
     "react-dom": "^18.3.1",
     "react-router": "^6.23.1",
-    "react-router-dom": "^6.23.1"
+    "react-router-dom": "^6.23.1",
+    "tauri-plugin-sql-api": "https://github.com/tauri-apps/tauri-plugin-sql#v1"
   },
   "devDependencies": {
     "@rollup/plugin-alias": "^5.1.0",

+ 59 - 1
src-tauri/src/main.rs

@@ -16,17 +16,75 @@ use self_plugin::tauri_plugin_sqlite;
 use self_plugin::tauri_plugin_file;
 
 
+use tauri::{Manager, generate_context};
+// use tauri_plugin_sql::{Builder as SqlBuilder, TauriSql};
+use tauri::api::path::app_data_dir;
+
+use tauri_plugin_sql::{Migration, MigrationKind};
+
 fn main() {
+    let migrations = vec![
+        // Define your migrations here
+        Migration {
+            version: 1,
+            description: "create_initial_tables",
+            sql: "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);",
+            kind: MigrationKind::Up,
+        },
+        Migration {
+            version: 2,
+            description: "create_initial_tables",
+            sql: "ALTER TABLE 'users' ADD 'addType' TEXT;ALTER TABLE 'users' ADD 'userTime' TEXT;",
+            kind: MigrationKind::Up,
+        }
+        // Migration {
+        //     version: 3,
+        //     description: "create_initial_tables",
+        //     sql: "ALTER TABLE users DROP COLUMN userTime;",
+        //     kind: MigrationKind::Up,
+        // },
+        // Migration {
+        //     version: 4,
+        //     description: "create_initial_tables",
+        //     sql: "ALTER TABLE 'users' ADD 'addType' TEXT;",
+        //     kind: MigrationKind::Up,
+        // },
+        // Migration {
+        //     version: 5,
+        //     description: "create_initial_tables",
+        //     sql: "ALTER TABLE 'users' ADD 'addType2' TEXT;",
+        //     kind: MigrationKind::Up,
+        // }
+    ];
     tauri::Builder::default()
         .plugin(tauri_plugin_sqlite::init())
         .plugin(tauri_plugin_file::init())
-        .plugin(tauri_plugin_sql::Builder::default().build())
+        .plugin(
+            tauri_plugin_sql::Builder::default()
+            .add_migrations("sqlite:test.db", migrations)
+        .build()
+    )
+
         .menu(use_memu())
         .on_menu_event(|event| {
             // 处理菜单事件
             m_event(event);
         })
         .invoke_handler(tauri::generate_handler![greet, file_path, file_sort])
+        .setup(|app| {
+            let app_handle = app.handle();
+            let app_dir = app_data_dir(&app_handle.config());
+
+            // 打印应用程序目录路径
+            println!("打印应用程序目录路径App directory: {:?}", app_dir);
+
+
+            // // 设定数据库文件路径
+            // let db_path = app_dir.join("test.db");
+            // println!("Database file path: {:?}", db_path);
+
+            Ok(())
+        })
         .run(tauri::generate_context!())
         .expect("error while running tauri application");
 }

+ 13 - 12
src/databases/createTableSql.ts

@@ -1,17 +1,18 @@
 export const createSql = {
     select_history: `CREATE TABLE select_history (
-        id INTEGER PRIMARY KEY AUTOINCREMENT,
-        time TIMESTAMP,
-        name TEXT CHECK(length(name) <= 255),
-        path TEXT CHECK(length(path) <= 500),
-        unique(path),
-        checkboxAll INTEGER NOT NULL CHECK (Enabled IN (0, 1)),
-        addType TEXT,
-        passType TEXT,
-        checkedSizeValues TEXT,
-        checkboxSizeAll INTEGER NOT NULL CHECK (Enabled IN (0, 1)),
-        checkedTypeValues TEXT
-    );`,
+     id INTEGER PRIMARY KEY AUTOINCREMENT,
+    time TIMESTAMP,
+    name TEXT CHECK(length(name) <= 255),
+    path TEXT CHECK(length(path) <= 500),
+    checkboxAll INTEGER NOT NULL CHECK (checkboxAll IN (0, 1)),
+    addType TEXT,
+    passType TEXT,
+    checkedSizeValues TEXT,
+    checkboxSizeAll INTEGER NOT NULL CHECK (checkboxSizeAll IN (0, 1)),
+    checkedTypeValues TEXT,
+    UNIQUE (path)
+);
+`,
     search_files: `CREATE TABLE search_files (
         id INTEGER PRIMARY KEY AUTOINCREMENT,
         time TIMESTAMP,

+ 19 - 3
src/pages/DuplicateFile/DuplicateFile.tsx

@@ -20,6 +20,10 @@ import FileInfoEditer from "./FileInfoEditer";
 import { FileInfoType } from "@/types/files";
 import { get_info_by_path, insertSeletedFileHistory } from "@/services";
 
+import Database from "tauri-plugin-sql-api";
+import { createSql } from "@/databases/createTableSql";
+const db = await Database.load("sqlite:test.db");
+
 const { Search } = Input;
 const { TextArea } = Input;
 
@@ -50,6 +54,8 @@ export default function DuplicateFile() {
   const [fileInfo, setFileInfo] = useState<any>({});
   const [fileInfoSource, setFileInfoSource] = useState<FileInfoType>({});
 
+
+
   const columns = [
     {
       title: "ID",
@@ -130,7 +136,8 @@ export default function DuplicateFile() {
   }
 
   async function openModal(info?: FileInfoType) {
-    setIsModalOpen(true);
+    initDB()
+    // setIsModalOpen(true);
     // const res = await insertSeletedFileHistory('/Users/sysadmin/Downloads');
     // console.log(133, res);
     // const res = await get_info_by_path('/Users/sysadmin/Downloads')
@@ -142,8 +149,8 @@ export default function DuplicateFile() {
       checkedSizeValues: ["巨大(4GB+)", "大(128MB ~ 1GB-)"],
       addType: "2131231231231"
     }); */
-    /* 
-    
+    /*
+
     {path: "/Users/sysadmin/Downloads", checkedTypeValues: ["音频", "图片"], checkedSizeValues: ["巨大(4GB+)", "大(128MB ~ 1GB-)"]}
 
 
@@ -164,6 +171,15 @@ Object Prototype
     */
   }
 
+
+  async function initDB() {
+    try {
+      const result = await db.execute(createSql.search_files);
+      console.log(179, result);
+    } catch (error) {
+      console.log(182, error);
+    }
+  }
   return (
     <div className={styles.DuplicateFileBox}>
       <FileInfoEditer

+ 7 - 1
yarn.lock

@@ -589,7 +589,7 @@
   resolved "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.18.0.tgz#5d694d345ce36b6ecf657349e03eb87297e68da4"
   integrity sha512-UOo5FdvOL0+eIVTgS4tIdbW+TtnBLWg1YBCcU2KWM7nuNwRz9bksDX1bekJJCpu25N1DVWaCwnT39dVQxzqS8g==
 
-"@tauri-apps/api@^1.5.6":
+"@tauri-apps/api@1.5.6", "@tauri-apps/api@^1.5.6":
   version "1.5.6"
   resolved "https://registry.npmjs.org/@tauri-apps/api/-/api-1.5.6.tgz#7496d79f6586659a59bc6e9d6666eed8d24dc6da"
   integrity sha512-LH5ToovAHnDVe5Qa9f/+jW28I6DeMhos8bNDtBOmmnaDpPmJmYLyHdeDblAWWWYc7KKRDg9/66vMuKyq0WIeFA==
@@ -1602,6 +1602,12 @@ supports-color@^5.3.0:
   dependencies:
     has-flag "^3.0.0"
 
+"tauri-plugin-sql-api@https://github.com/tauri-apps/tauri-plugin-sql#v1":
+  version "0.0.0"
+  resolved "https://github.com/tauri-apps/tauri-plugin-sql#6a4a14c36b1ad49c9f468351706d104a54e0540c"
+  dependencies:
+    "@tauri-apps/api" "1.5.6"
+
 throttle-debounce@^5.0.0:
   version "5.0.0"
   resolved "https://registry.npmjs.org/throttle-debounce/-/throttle-debounce-5.0.0.tgz#a17a4039e82a2ed38a5e7268e4132d6960d41933"