1
0
mirror of synced 2024-12-13 06:51:08 +01:00
GC-local-server-rewrite/Infrastructure/Migrations/MigrationHelper.cs

34 lines
1.2 KiB
C#
Raw Normal View History

using System.Data;
using Infrastructure.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace Infrastructure.Migrations;
public static class MigrationHelper
{
public static bool Exists(string tableName)
{
var options = new DbContextOptionsBuilder().UseSqlite($"Data Source={GetConnectionString()}");
using var context = new DbContext(options.Options);
using var command = context.Database.GetDbConnection().CreateCommand();
command.CommandText = $"SELECT count(*) FROM sqlite_master WHERE type='table' AND name='{tableName}';";
command.CommandType = CommandType.Text;
context.Database.OpenConnection();
using var reader = command.ExecuteReader();
return reader.Read()? (long)reader[0] == 1 : false;
}
2023-02-09 10:25:42 +01:00
private static string GetConnectionString()
{
var builder = new ConfigurationBuilder()
.SetBasePath(PathHelper.ConfigurationPath)
.AddJsonFile("database.json", optional: false, reloadOnChange: false);
2023-02-09 10:25:42 +01:00
var cardDbName = builder.Build()["CardDbName"] ?? "card.db3";
var cardDbPath = Path.Combine(PathHelper.DatabasePath, cardDbName);
return cardDbPath;
}
}