Skip to content

Commit 4ed872d

Browse files
committed
feat(release): implement two-tier SQL validation for hash mismatch detection
- Add original SQL storage columns to metadata schema for two-tier validation - Implement custom SQL normalization in worker for F-003 feature - Create enhanced HashMismatchError with SQL diff formatting - Update release manager to populate original SQL maps on database open - Add comprehensive unit tests for validation logic - Update README to document v2.2.0 two-tier validation feature
1 parent e058879 commit 4ed872d

53 files changed

Lines changed: 12883 additions & 1773 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.claude

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<img src="https://img.shields.io/npm/v/web-sqlite-js.svg" alt="NPM Version" />
1515
</a>
1616
<a href="https://github.com/wuchuheng/web-sqlite-js/discussions" target="_blank">
17-
<img src="https://img.shields.io/badge/v2.0.0-new%20features-blue" alt="v2.0.0" />
17+
<img src="https://img.shields.io/badge/v2.2.0-two--tier%20validation-brightgreen" alt="v2.2.0" />
1818
</a>
1919
<a href="https://github.com/wuchuheng/web-sqlite-js/blob/main/LICENSE" target="_blank">
2020
<img src="https://img.shields.io/github/license/wuchuheng/web-sqlite-js.svg" alt="License" />
@@ -49,6 +49,8 @@ Designed to be truly effortless, it allows you to get a high-performance relatio
4949
- **Type-Safe**: Written in TypeScript with full type definitions.
5050
- **Transactions**: Supports atomic transactions with automatic rollback on error.
5151
- **Schema Migrations**: Built-in versioning system for database schema changes.
52+
- **Two-Tier Validation** (v2.2.0): Distinguishes between whitespace-only changes and actual SQL changes.
53+
- **Enhanced Error Messages** (v2.2.0): SQL diffs and truncation for better debugging.
5254
- **Structured Logging**: Subscribe to SQL execution logs via `onLog()`.
5355

5456
## Quick start
@@ -73,7 +75,7 @@ For quick demos or plain HTML pages you can load the prebuilt module directly:
7375

7476
```html
7577
<script type="module">
76-
import openDB from "https://cdn.jsdelivr.net/npm/web-sqlite-js@1.0.9/dist/index.js";
78+
import openDB from "https://cdn.jsdelivr.net/npm/web-sqlite-js@2.2.0/dist/index.js";
7779
// ...
7880
</script>
7981
```
@@ -322,12 +324,72 @@ console.log(users);
322324
// Output: [{ id: 1, name: 'Alice', email: '[email protected]', created_at: '...' }, ...]
323325
```
324326

327+
### Two-Tier Validation (v2.2.0)
328+
329+
Starting with v2.2.0, web-sqlite-js uses a two-tier validation system that intelligently handles SQL formatting changes:
330+
331+
**Tier 1: Fast Path** (< 0.1ms)
332+
333+
- Trims whitespace and compares hashes
334+
- Passes immediately if SQL hasn't changed
335+
336+
**Tier 2: Slow Path** (1-5ms, only on hash mismatch)
337+
338+
- Normalizes SQL using SQLite prepare
339+
- Auto-updates hash for whitespace-only changes
340+
- Throws enhanced error for actual SQL changes
341+
342+
**What this means for you:**
343+
344+
```typescript
345+
// You can reformat your SQL without breaking migrations!
346+
const db1 = await openDB("myapp", {
347+
releases: [
348+
{
349+
version: "1.0.0",
350+
migrationSQL: "CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT);",
351+
},
352+
],
353+
});
354+
await db1.close();
355+
356+
// Reopen with reformatted SQL (whitespace-only change)
357+
const db2 = await openDB("myapp", {
358+
releases: [
359+
{
360+
version: "1.0.0",
361+
migrationSQL: `
362+
CREATE TABLE items (
363+
id INTEGER PRIMARY KEY,
364+
name TEXT
365+
);
366+
`,
367+
},
368+
],
369+
});
370+
// ✅ Works! Hash auto-updated (no error)
371+
```
372+
373+
**Enhanced Error Messages:**
374+
375+
If you accidentally change the SQL semantics, you'll get detailed error messages:
376+
377+
```
378+
Hash mismatch for 1.0.0 migrationSQL:
379+
Expected: abc123...
380+
Actual: def456...
381+
SQL has changed:
382+
- Original: CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT);
383+
+ Current: CREATE TABLE items (id INTEGER PRIMARY KEY, email TEXT);
384+
```
385+
325386
### How It Works
326387

327388
1. **Version Tracking**: Each release has a semantic version (e.g., "1.0.0")
328389
2. **Automatic Migration**: When opening a database, new releases are applied in order
329390
3. **Hash Verification**: Migration SQL is hashed to prevent tampering
330-
4. **OPFS Storage**: Each version is stored as a separate file (`1.0.0.sqlite3`, `1.1.0.sqlite3`)
391+
4. **Two-Tier Validation**: Distinguishes whitespace-only changes from actual SQL changes
392+
5. **OPFS Storage**: Each version is stored as a separate file (`1.0.0.sqlite3`, `1.1.0.sqlite3`)
331393

332394
### Best Practices
333395

agent-docs/00-control/01-status.md

Lines changed: 213 additions & 920 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)