Skip to content

Commit ccffb13

Browse files
committed
feat: refactor tx API + add backing functions
- Consolidated transaction API to address a design flaw - Calling BEGIN and then ceding the write conn could leave an open transaction dangling - Added backing functions to wrapper.rs - Added decoder and tests - Added tests for error types - Remove the idea that an error will throw when attempting a RO query with the writer. It's obvious from the return type you'll get no query results - NOTE: Integration tests coming in a future PR
1 parent 0536341 commit ccffb13

11 files changed

Lines changed: 1056 additions & 144 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ futures-core = "0.3.31"
2323
time = "0.3.44"
2424
tokio = { version = "1.48.0", features = ["sync"] }
2525
indexmap = { version = "2.12.1", features = ["serde"] }
26+
base64 = "0.22.1"
2627

2728
# SQLx for types and queries (time feature enables datetime type decoding)
2829
sqlx = { version = "0.8.6", features = ["sqlite", "json", "time", "runtime-tokio"] }

MIGRATION_PLAN.md

Lines changed: 355 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,355 @@
1+
# Plugin Code Migration Plan
2+
3+
This document outlines the phased approach for migrating the working
4+
prototype from `../plugins-workspace/plugins/sqlite` into this project.
5+
6+
## Overview
7+
8+
The migration is organized into 5 phases, each designed to be a reviewable
9+
PR that builds upon the previous phase. The goal is to make reviews
10+
manageable while ensuring each phase leaves the code in a working state.
11+
12+
## Phase 1: Foundation - README, Types, and Build Configuration
13+
14+
**Goal:** Establish the public API surface and documentation without
15+
implementation details.
16+
17+
**Changes:**
18+
19+
1. **README.md** - Merge and enhance:
20+
* Preserve existing project structure documentation and standards info
21+
* Add comprehensive usage examples from prototype
22+
* Add API reference documentation
23+
* Add migration guide from tauri-plugin-sql
24+
* Keep CI badge and project metadata from current README
25+
* Add feature list and architecture overview
26+
27+
2. **Cargo.toml** - Update dependencies:
28+
* Add `serde_json`, `thiserror`, `log`, `futures-core`, `time`,
29+
`tokio`, `indexmap`
30+
* Add `sqlx` with features: `["sqlite", "json", "time",
31+
"runtime-tokio"]`
32+
* Update `sqlx-sqlite-conn-mgr` path from workspace member to local
33+
crate
34+
* Keep workspace structure intact
35+
36+
3. **package.json** - Update for proper publishing:
37+
* Update package name to `@tauri-apps/plugin-sqlite` (or keep
38+
`@silvermine/tauri-plugin-sqlite`)
39+
* Add proper exports configuration (types, import, require)
40+
* Update build script to use rollup instead of raw tsc
41+
* Add dual licensing if using @tauri-apps namespace
42+
43+
4. **TypeScript interfaces** (`guest-js/index.ts`):
44+
* Add `QueryResult` interface
45+
* Add `CustomConfig` interface
46+
* Add `Database` class skeleton with method signatures only (no
47+
implementation)
48+
* Keep all JSDoc comments for API documentation
49+
* Replace hello() function with Database class structure
50+
51+
5. **Build configuration**:
52+
* Add `rollup.config.js` for JS bundling
53+
* Add `api-iife.js` for IIFE builds
54+
* Update `tsconfig.json` if needed
55+
56+
6. **Licensing**:
57+
* Add `LICENSE_APACHE-2.0` and `LICENSE_MIT` files
58+
* Update headers in files to dual license format
59+
60+
**Verification:**
61+
62+
* README is comprehensive and accurate
63+
* TypeScript types compile successfully
64+
* `npm run build` succeeds
65+
* Documentation is complete (even though implementation is pending)
66+
67+
**Files Modified:**
68+
69+
* `README.md`
70+
* `Cargo.toml`
71+
* `package.json`
72+
* `guest-js/index.ts`
73+
* `rollup.config.js` (new)
74+
* `api-iife.js` (new)
75+
* `LICENSE_APACHE-2.0` (new)
76+
* `LICENSE_MIT` (new)
77+
78+
**Estimated Size:** ~300 lines changed (mostly docs and config)
79+
80+
---
81+
82+
## Phase 2: Core Error Handling and Models
83+
84+
**Goal:** Add error types and data models needed by the plugin commands.
85+
86+
**Changes:**
87+
88+
1. **src/error.rs** - Replace with comprehensive error handling:
89+
* Add `Error` enum with variants for:
90+
* `Sqlx(sqlx::Error)`
91+
* `Io(std::io::Error)`
92+
* `Tauri(tauri::Error)`
93+
* `Migration(String)`
94+
* `InvalidQuery(String)`
95+
* `Connection(String)`
96+
* Implement `From` conversions for each error type
97+
* Implement `Serialize` for Tauri command responses
98+
* Keep `Result<T>` type alias
99+
100+
2. **src/lib.rs** - Add type definitions only:
101+
* Add `DbInstances` struct
102+
* Add `PluginConfig` struct with `preload` field
103+
* Add `Migration` struct
104+
* Add `MigrationKind` enum
105+
* Add `MigrationList` struct implementing `MigrationSource`
106+
* Add `Builder` struct skeleton (no implementation yet)
107+
* Add `run_async_command` helper function
108+
* Keep existing module declarations
109+
110+
3. **Documentation:**
111+
* Add inline documentation for all public types
112+
* Add module-level documentation with examples
113+
114+
4. **Update README.md:**
115+
* Update the "Project Structure" section to reflect any new files added
116+
117+
**Verification:**
118+
119+
* Rust code compiles (`cargo build`)
120+
* No warnings
121+
* Error types properly implement required traits
122+
123+
**Files Modified:**
124+
125+
* `src/error.rs`
126+
* `src/lib.rs` (types only, no builder implementation)
127+
128+
**Estimated Size:** ~200 lines
129+
130+
---
131+
132+
## Phase 3: Database Wrapper and Decode Logic
133+
134+
**Goal:** Add the database wrapper that manages connections and the decode
135+
logic for query results.
136+
137+
**Changes:**
138+
139+
1. **src/wrapper.rs** (new file):
140+
* `DatabaseWrapper` struct
141+
* Connection initialization logic
142+
* Migration support
143+
* Pool management (read/write pools)
144+
* Close and remove operations
145+
* All methods fully implemented
146+
147+
2. **src/decode.rs** (new file):
148+
* `decode_query_result` function
149+
* Row conversion logic
150+
* Type mapping from SQLite to Serde JSON
151+
* Null handling
152+
* Time type support
153+
154+
3. **Update src/lib.rs**:
155+
* Add `pub use wrapper::DatabaseWrapper;`
156+
* Add `pub use decode::decode_query_result;` if needed
157+
158+
4. **Update README.md:**
159+
* Update the "Project Structure" section to reflect new wrapper.rs and decode.rs files
160+
161+
**Verification:**
162+
163+
* Rust code compiles
164+
* Unit tests pass for decode logic (if we add them)
165+
* No warnings
166+
167+
**Files Modified:**
168+
169+
* `src/wrapper.rs` (new)
170+
* `src/decode.rs` (new)
171+
* `src/lib.rs` (add module declarations and exports)
172+
173+
**Estimated Size:** ~400 lines
174+
175+
---
176+
177+
## Phase 4: Commands Implementation
178+
179+
**Goal:** Implement the actual Tauri command handlers that the frontend
180+
calls.
181+
182+
**Changes:**
183+
184+
1. **src/commands.rs** - Replace hello with real commands:
185+
* `load` - Load/connect to database
186+
* `execute_write` - Execute write queries
187+
* `select` - Execute select queries
188+
* `close` - Close specific database
189+
* `close_all` - Close all databases
190+
* `remove` - Delete database files
191+
* Add proper error handling
192+
* Add parameter validation
193+
194+
2. **guest-js/index.ts** - Add actual invoke calls:
195+
* Implement all Database class methods
196+
* Wire up to `plugin:sqlite|*` commands
197+
* Keep all existing type definitions and documentation
198+
199+
3. **Update src/lib.rs**:
200+
* Update `generate_handler!` macro to include all commands
201+
* Keep command list updated
202+
203+
4. **Update README.md:**
204+
* Update the "Project Structure" section if commands.rs changes significantly
205+
206+
**Verification:**
207+
208+
* Rust code compiles
209+
* TypeScript compiles and generates proper types
210+
* Commands are properly registered
211+
* JSDoc comments match implementation
212+
213+
**Files Modified:**
214+
215+
* `src/commands.rs`
216+
* `guest-js/index.ts`
217+
* `src/lib.rs` (update handler registration)
218+
219+
**Estimated Size:** ~250 lines
220+
221+
---
222+
223+
## Phase 5: Plugin Builder and Lifecycle
224+
225+
**Goal:** Complete the plugin initialization, migration system, and
226+
lifecycle management.
227+
228+
**Changes:**
229+
230+
1. **src/lib.rs** - Implement Builder:
231+
* Implement `Builder::new()`
232+
* Implement `Builder::add_migrations()`
233+
* Implement `Builder::build()` with full setup logic:
234+
* Database preloading
235+
* Migration execution
236+
* State management
237+
* Cleanup on app exit
238+
* Wire up all lifecycle hooks
239+
240+
2. **permissions/default.toml** - Update permissions:
241+
* Replace `allow-hello` with actual command permissions
242+
* Add descriptive documentation
243+
* Follow prototype's permission structure
244+
245+
3. **permissions/autogenerated/** - Regenerate:
246+
* Run Tauri's permission generation
247+
* Ensure all commands have permission definitions
248+
* Update schemas
249+
250+
4. **build.rs** - Update if needed:
251+
* Ensure it properly generates permissions
252+
* Add any additional build steps
253+
254+
5. **CHANGELOG.md** (new):
255+
* Add initial version entry
256+
* Document features
257+
258+
6. **Update README.md:**
259+
* Update the "Project Structure" section to reflect completed implementation
260+
* Remove "Phase 2" notation from commands.rs description
261+
262+
**Verification:**
263+
264+
* Full plugin lifecycle works
265+
* Migrations run correctly
266+
* Preload functionality works
267+
* Database cleanup on exit works
268+
* Permissions are properly enforced
269+
* All npm scripts work
270+
* All cargo commands work
271+
272+
**Files Modified:**
273+
274+
* `src/lib.rs` (complete Builder implementation)
275+
* `permissions/default.toml`
276+
* `permissions/autogenerated/*` (regenerated)
277+
* `build.rs` (if needed)
278+
* `CHANGELOG.md` (new)
279+
280+
**Estimated Size:** ~200 lines
281+
282+
---
283+
284+
## Phase 6 (Optional): Testing and Examples
285+
286+
**Goal:** Add comprehensive tests and example application.
287+
288+
**Changes:**
289+
290+
1. **Add integration tests:**
291+
* Test database loading
292+
* Test migrations
293+
* Test CRUD operations
294+
* Test concurrent reads
295+
* Test cleanup
296+
297+
2. **Add example application:**
298+
* Simple Tauri app demonstrating plugin usage
299+
* Example migrations
300+
* Example frontend code
301+
302+
3. **Add CI improvements:**
303+
* Test workflows
304+
* Documentation generation
305+
306+
4. **Update README.md:**
307+
* Update the "Project Structure" section to include tests/ and examples/ directories
308+
309+
**Verification:**
310+
311+
* All tests pass
312+
* Example app runs
313+
* CI pipeline succeeds
314+
315+
**Estimated Size:** ~500+ lines (tests and examples)
316+
317+
---
318+
319+
## Summary
320+
321+
| Phase | Focus | PR Size | Risk |
322+
|-------|-------|---------|------|
323+
| 1 | Foundation | Medium | Low |
324+
| 2 | Types & Errors | Small | Low |
325+
| 3 | Wrapper & Decode | Large | Medium |
326+
| 4 | Commands | Medium | Medium |
327+
| 5 | Builder & Lifecycle | Medium | High |
328+
| 6 | Testing (Optional) | Large | Low |
329+
330+
**Key Principles:**
331+
332+
* Each phase compiles successfully
333+
* Incremental functionality additions
334+
* Clear separation of concerns
335+
* Easy to review diffs
336+
* Logical progression from types → implementation → integration
337+
338+
**Dependencies:**
339+
340+
* Phase 2 depends on Phase 1 (needs types from updated Cargo.toml)
341+
* Phase 3 depends on Phase 2 (needs error types)
342+
* Phase 4 depends on Phase 3 (needs DatabaseWrapper)
343+
* Phase 5 depends on Phase 4 (needs commands)
344+
* Phase 6 depends on Phase 5 (needs complete implementation)
345+
346+
## Next Steps
347+
348+
1. Review this plan
349+
2. Approve or suggest modifications
350+
3. Execute Phase 1
351+
4. Review PR for Phase 1
352+
5. Proceed to next phase
353+
354+
Would you like to proceed with Phase 1, or would you like any adjustments
355+
to this plan?

0 commit comments

Comments
 (0)