Skip to content

Commit

Permalink
Create the folders for the Sqlite blob files if they do not exist. (#39)
Browse files Browse the repository at this point in the history
* Create the folders for the Sqlite blob files if they do not exist.

As mentioned in bagetter/BaGette#38 under some special configurations Sqlite failed with a "folder not found" exception.

* Simplified folder creation logic.

* Simply create the path given by the Database::ConnectionString config.
  • Loading branch information
seriouz authored Jan 25, 2024
1 parent 18bcc1c commit 954e60d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

<Nullable>enable</Nullable>
<PackageTags>NuGet</PackageTags>
<Description>The libraries to host BaGetter on SQLite.</Description>
</PropertyGroup>
Expand Down
29 changes: 29 additions & 0 deletions src/BaGetter.Database.Sqlite/SqliteContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using BaGetter.Core;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -45,5 +50,29 @@ protected override void OnModelCreating(ModelBuilder builder)
.Property(f => f.Moniker)
.HasColumnType("TEXT COLLATE NOCASE");
}

public override async Task RunMigrationsAsync(CancellationToken cancellationToken)
{
if (Database.GetDbConnection() is SqliteConnection connection)
{
/* Create the folder of the Sqlite blob if it does not exist. */
CreateSqliteDataSourceDirectory(connection);
}

await base.RunMigrationsAsync(cancellationToken);
}

/// <summary>
/// Creates directories specified in the Database::ConnectionString config for the Sqlite database file.
/// </summary>
/// <param name="connection">Instance of the <see cref="SqliteConnection"/>.</param>
private static void CreateSqliteDataSourceDirectory(SqliteConnection connection)
{
var pathToCreate = Path.GetDirectoryName(connection.DataSource);

if (pathToCreate is null) return;

Directory.CreateDirectory(pathToCreate);
}
}
}
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<!-- Compiler properties -->
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>7.2</LangVersion>
<LangVersion>latest</LangVersion>

<!-- Don't warn if there are missing XMl comment for publicly visible type or member-->
<NoWarn>$(NoWarn);1591</NoWarn>
Expand Down

0 comments on commit 954e60d

Please sign in to comment.