tauri-apps / plugins-workspace

All of the official Tauri plugins in one place!
https://tauri.app
Apache License 2.0
802 stars 223 forks source link

[sql] Docs missing `preload` migrations info #1555

Closed gabeidx closed 1 month ago

gabeidx commented 1 month ago

While following the docs instructions to use the tauri-plugin-sql with sqlite and use the migrations feature, I noticed the migrations were not executed at all.

Reproduction

  1. Create a new project cargo create-tauri-app --beta
  2. Add the SQL plugin cargo tauri add sql
  3. Update Cargo.toml
    - tauri-plugin-sql = "2.0.0-beta.9"
    + tauri-plugin-sql = { version = "2.0.0-beta.9", features = ["sqlite"] }
  4. Add a migration, as described in the plugin docs
    
    // src-tauri/src/main.rs

fn main() {

Expected behavior

Migrations are applied automatically when the plugin is initialized.

Full capabilities/default.json

{
  "$schema": "../gen/schemas/desktop-schema.json",
  "identifier": "default",
  "description": "Capability for the main window",
  "windows": [
    "main"
  ],
  "permissions": [
    "path:default",
    "event:default",
    "window:default",
    "app:default",
    "image:default",
    "resources:default",
    "menu:default",
    "tray:default",
    "shell:allow-open",
    "sql:default"
  ]
}

Full tauri info output

WARNING: no lock files found, defaulting to npm

[✔] Environment
    - OS: Mac OS 14.5.0 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.79.0 (129f3b996 2024-06-10)
    ✔ cargo: 1.79.0 (ffa9cf99a 2024-06-03)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (environment override by RUSTUP_TOOLCHAIN)
    - node: 22.4.1
    - pnpm: 9.0.2
    - yarn: 1.22.22
    - npm: 10.8.2

[-] Packages
    - tauri [RUST]: 2.0.0-beta.24
    - tauri-build [RUST]: 2.0.0-beta.19
    - wry [RUST]: 0.41.0
    - tao [RUST]: 0.28.1
    - tauri-cli [RUST]: 2.0.0-beta.21
    - @tauri-apps/api : not installed!
    - @tauri-apps/cli [NPM]: 2.0.0-beta.21

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../src
gabeidx commented 1 month ago

After a conversation with @FabianLars on Discord, it seems the current fix is to load the DB from the client side:

import Database from "@tauri-apps/plugin-sql";
const db = await Database.load("sqlite:mydatabase.db"); // <- same db name from plugin setup!

The reason is that the migrations are run on the load() command implementation https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/src/plugin.rs#L166-L169 and not when the plugin is initialized.

A refactoring of the plugin is being considered, which may change the behavior and match the expectation of the migrations being run from the Rust side automatically when the plugin is initialized.

Related to https://github.com/tauri-apps/plugins-workspace/issues/509 and https://github.com/tauri-apps/plugins-workspace/issues/506.

gabeidx commented 1 month ago

It seems this could be solved if there was a way to write config.preload value, based on https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/src/plugin.rs#L313-L317

FabianLars commented 1 month ago

Oh my god, i totally forgot about that. You absolutely can change that value! Simply add this to your tauri.conf.json file:

{
  "plugins": {
    "sql": {
      "preload": ["dbname"]
    }
  }
}
gabeidx commented 1 month ago

I JUST found it in the docs as well! 🤦 https://v2.tauri.app/develop/plugins/#plugin-configuration

gabeidx commented 1 month ago

I've opened a couple of PRs to update the readme and the v2 docs for the plugin: