Make bit Besql's ConfigureSqliteSynchronous public (#9819)#9820
Make bit Besql's ConfigureSqliteSynchronous public (#9819)#9820msynk merged 2 commits intobitfoundation:developfrom yasmoradi:9819
Conversation
WalkthroughThis pull request removes the inline SQLite connection initialization from the BesqlPooledDbContextFactory class and introduces a new public asynchronous extension method—ConfigureSqliteSynchronous—in a dedicated DatabaseFacadeExtensions class. The service registration in IServiceCollectionBesqlExtentions is updated to invoke the new method before calling the database initializer asynchronously. Additionally, a new "Re-create database" button with its corresponding ReCreateDatabase method is added to the demo Razor page to facilitate database recreation. Changes
Sequence Diagram(s)sequenceDiagram
participant SP as Service Provider
participant SC as AddBesqlDbContextFactory
participant DBF as ConfigureSqliteSynchronous
participant DI as dbContextInitializer
SP->>SC: Invoke service registration
SC->>DBF: Asynchronously configure SQLite connection
DBF-->>SC: Return configured DatabaseFacade
SC->>DI: Invoke dbContext initializer
DI-->>SC: Initialize database context
SC-->>SP: Return registered service
sequenceDiagram
participant U as User
participant R as Weather.razor
participant DB as Database
U->>R: Click "Re-create database" button
R->>DB: Delete existing database
R->>DB: Recreate database (migrate/ensure created)
R->>DB: Apply SQLite synchronous configuration
DB-->>R: Database configured and ready
R-->>U: Update UI
Assessment against linked issues
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 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 (4)
src/Besql/Bit.Besql/DatabaseFacadeExtensions.cs (2)
8-11: Enhance XML documentation with parameter and return value details.Add parameter and return value documentation to make the API more discoverable.
/// <summary> /// This method must be called before invoking Database.MigrateAsync or Database.EnsureCreatedAsync, to ensure proper functionality of bit Besql. /// </summary> +/// <param name="database">The database facade to configure.</param> +/// <returns>The same database facade instance for method chaining.</returns> +/// <exception cref="InvalidOperationException">Thrown when the database is not SQLite.</exception>
22-22: Consider extracting the PRAGMA command as a constant.For better maintainability and reusability, consider extracting the SQL command text.
+ private const string SetSynchronousFullCommand = "PRAGMA synchronous = FULL;"; + public static async Task<DatabaseFacade> ConfigureSqliteSynchronous(this DatabaseFacade database) { // ... - command.CommandText = "PRAGMA synchronous = FULL;"; + command.CommandText = SetSynchronousFullCommand; // ...src/Besql/Bit.Besql/IServiceCollectionBesqlExtentions.cs (1)
20-24: Consider adding error handling in the async lambda.While the implementation is correct, consider adding error handling to ensure database configuration failures are properly handled.
services.AddSingleton(async (IServiceProvider sp, TDbContext dbContext) => { - await dbContext.Database.ConfigureSqliteSynchronous(); - await dbContextInitializer(sp, dbContext); + try + { + await dbContext.Database.ConfigureSqliteSynchronous(); + await dbContextInitializer(sp, dbContext); + } + catch (Exception ex) + { + throw new InvalidOperationException("Failed to initialize database context.", ex); + } });src/Besql/Demo/Bit.Besql.Demo.Client/Pages/Weather.razor (1)
102-113: Consider adding user feedback for database operations.The implementation is correct, but consider adding loading states and success/error notifications for a better user experience.
+ private bool isRecreating = false; + private async Task ReCreateDatabase() { + if (isRecreating) return; + isRecreating = true; + await using var dbContext = await DbContextFactory.CreateDbContextAsync(); + try + { + await dbContext.Database.EnsureDeletedAsync(); + await dbContext.Database.ConfigureSqliteSynchronous(); + await dbContext.Database.MigrateAsync(); + await ReadDataSample(); // Refresh the count + } + catch (Exception ex) + { + Console.Error.WriteLine($"Failed to recreate database: {ex}"); + throw; + } + finally + { + isRecreating = false; + } }And update the button:
-<button class="btn btn-primary" @onclick="ReCreateDatabase">Re-create database</button> +<button class="btn btn-primary" @onclick="ReCreateDatabase" disabled="@isRecreating"> + @if (isRecreating) + { + <span>Recreating...</span> + } + else + { + <span>Re-create database</span> + } +</button>
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
src/Besql/Bit.Besql/BesqlPooledDbContextFactory.cs(0 hunks)src/Besql/Bit.Besql/DatabaseFacadeExtensions.cs(1 hunks)src/Besql/Bit.Besql/IServiceCollectionBesqlExtentions.cs(1 hunks)src/Besql/Demo/Bit.Besql.Demo.Client/Pages/Weather.razor(3 hunks)
💤 Files with no reviewable changes (1)
- src/Besql/Bit.Besql/BesqlPooledDbContextFactory.cs
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: build and test
🔇 Additional comments (3)
src/Besql/Bit.Besql/DatabaseFacadeExtensions.cs (2)
13-14: LGTM! Robust error handling.The null check with string comparison and clear error message is well implemented.
19-24: LGTM! Proper connection and command management.Good use of:
await usingfor automatic disposalConfigureAwait(false)for performance- Async operations throughout
src/Besql/Demo/Bit.Besql.Demo.Client/Pages/Weather.razor (1)
18-18: LGTM! Consistent button implementation.The new button follows the same style and pattern as other buttons in the component.
closes #9819
Summary by CodeRabbit