[WEB-3705] improvement: clear local db on version change#6843
[WEB-3705] improvement: clear local db on version change#6843sriramveeraghanta merged 2 commits intopreviewfrom
Conversation
|
Pull Request Linked with Plane Work Items
Comment Automatically Generated by Plane |
WalkthroughThe pull request refactors the database storage initialization in the web module. A new private method Changes
Sequence Diagram(s)sequenceDiagram
participant S as Storage Instance
participant WB as Web Worker
participant W as Window
S->>S: initialize()
S->>S: initializeWorker()
S->>WB: Import DBClass, create & wrap worker via Comlink
WB-->>S: Worker ready
W->>S: Window unload event
S->>WB: Terminate worker
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
web/core/local-db/storage.sqlite.ts (3)
127-143: Well-implemented version check with appropriate loggingThe version check logic handles all edge cases (undefined, empty string, non-numeric values) and properly clears storage when a version mismatch is detected. The debugging logs help track the flow during development.
Consider extracting the "DB_VERSION" string into a constant to avoid duplication and improve maintainability.
+const DB_VERSION_KEY = "DB_VERSION"; // ... -const dbVersion = await this.getOption("DB_VERSION"); +const dbVersion = await this.getOption(DB_VERSION_KEY); // ... -await this.setOption("DB_VERSION", DB_VERSION.toString()); +await this.setOption(DB_VERSION_KEY, DB_VERSION.toString());
125-143: Consider refining the initialization flow after version mismatchWhen a version mismatch is detected, the worker is reinitialized, but then the normal initialization flow continues with
createTables()and setting the DB_VERSION again. This might lead to redundant operations.Consider restructuring the flow to avoid potential redundancy:
await this.initializeWorker(workspaceSlug); const dbVersion = await this.getOption("DB_VERSION"); log("Stored db version", dbVersion); log("Current db version", DB_VERSION); // Check if the database version matches the current version // If there's a mismatch, clear storage to avoid compatibility issues if ( dbVersion !== undefined && dbVersion !== "" && !isNaN(Number(dbVersion)) && Number(dbVersion) !== DB_VERSION ) { log("Database version mismatch detected - clearing storage to ensure compatibility"); await this.clearStorage(); await this.initializeWorker(workspaceSlug); + this.status = "ready"; + // Your SQLite code here. + await createTables(); + await this.setOption("DB_VERSION", DB_VERSION.toString()); + return true; } else { log("Database version matches current version - proceeding with data load"); + this.status = "ready"; + // Your SQLite code here. + await createTables(); + await this.setOption("DB_VERSION", DB_VERSION.toString()); + return true; } -this.status = "ready"; -// Your SQLite code here. -await createTables(); - -await this.setOption("DB_VERSION", DB_VERSION.toString()); -return true;
127-143:Details
❓ Verification inconclusive
Add version comparison for future maintainability
As the application evolves, you might need more complex version comparisons. Consider using a semantic versioning approach for better future-proofing.
🌐 Web query:
How to implement semantic versioning comparison in TypeScript?💡 Result:
To implement semantic versioning comparison in TypeScript, you can either use existing libraries or create a custom parser. Here are two robust approaches:
1. Using Established Libraries
Recommended solution: Use the
compare-versionspackage ([source][11]):import compareVersions from 'compare-versions'; // Basic comparison returns -1, 0, or 1 console.log(compareVersions('2.2.0', '2.2.1')); // -1 // Direct boolean checks console.log(compareVersions.gt('3.1.0', '3.0.5')); // true console.log(compareVersions.satisfies('4.7.2', '^4.6')); // trueThis library handles:
- Standard SemVer format (
MAJOR.MINOR.PATCH)- Pre-release versions (
3.0.0-beta.1)- Build metadata (
2.1.0+20230328)2. Custom Implementation (Basic)
For simple comparisons without prerelease/build metadata:
function semverCompare(a: string, b: string): -1 | 0 | 1 { const partsA = a.split('.').map(Number); const partsB = b.split('.').map(Number); for (let i = 0; i < 3; i++) { if (partsA[i] > partsB[i]) return 1; if (partsA[i] < partsB[i]) return -1; } return 0; } // Usage semverCompare('2.1.9', '2.2.0'); // -1Key considerations:
- Always validate version strings first
- Handle edge cases like missing segments (
2→2.0.0)- Use zero-padding for comparisons (
2.1→2.1.0)- Consider using [semver.org]'s official regex for validation[8]
Comparison Table
Approach Pros Cons compare-versionsFull spec compliance, pre-release support Adds 1.4kB dependency Custom Implementation No dependencies Lacks advanced feature support For most projects, we recommend using
compare-versionsdue to its robust handling of edge cases and compliance with the official SemVer specification[8]. The custom solution works for basic comparisons but misses critical features like pre-release version handling[11].Citations:
- 1: https://rfcs.emberjs.com/id/0730-semver-for-ts/
- 2: https://www.semver-ts.org
- 3: microsoft/TypeScript#14116
- 4: https://www.learningtypescript.com/articles/why-typescript-doesnt-follow-strict-semantic-versioning
- 5: https://docs.npmjs.com/about-semantic-versioning/
- 6: https://graphite.dev/guides/typescript-versioning
- 7: https://v5.chriskrycho.com/elsewhere/rfc-semver-for-typescript-types/
- 8: https://semver.org
- 9: https://v5.chriskrycho.com/journal/semver-for-ts-in-practice/
- 10: https://www.npmjs.com/package/@types/semver-compare
- 11: https://www.npmjs.com/package/compare-versions
Action Required: Transition to Semantic Versioning Comparison in DB Version Check
- Location:
web/core/local-db/storage.sqlite.ts(Lines: 127–143)- Context: The current implementation compares numerical values of versions. While sufficient for numeric versions, this approach won’t scale if version strings evolve to Semantic Versioning.
- Suggestion:
- For better future-proofing, implement a semantic version comparator. Consider using the
compare-versionspackage, which offers robust comparison (including handling pre-release and build metadata), or, if requirements are minimal, a custom semver parser.- Refactor the version check to validate and compare semantic version strings (e.g., using
compareVersions.gt(dbVersion, DB_VERSION)or similar) rather than converting to a number.- This change will help prevent compatibility issues as your versioning scheme becomes more complex over time.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
web/core/local-db/storage.sqlite.ts(4 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
web/core/local-db/storage.sqlite.ts (1)
web/core/local-db/utils/utils.ts (1)
log(7-11)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: Analyze (javascript)
- GitHub Check: Analyze (python)
🔇 Additional comments (4)
web/core/local-db/storage.sqlite.ts (4)
22-22: Version update to trigger DB clearing logicThe DB_VERSION is updated from 1 to 1.1, which will trigger the new version checking logic to clear local storage when users update their application. This is a good approach to prevent compatibility issues between database versions.
72-89: Good refactoring of worker initialization logicMoving the worker initialization code into a separate private method improves code organization and enables reuse in the version mismatch handling path.
125-125: Clean replacement of inline worker initializationThe refactored method is correctly called here to initialize the worker.
339-339: Enhanced error logging for database fallbackAdding this log statement helps developers understand when the system is falling back to the server due to local database issues, which is valuable for debugging.
* [WEB-3705] improvement: clear local db on version change * chore: remove console.log
Description
This PR adds a check to clear local DB if the stored version differs from the current version to prevent compatibility issues.
Type of Change
Summary by CodeRabbit