-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimescaleTestStore.cs
More file actions
55 lines (45 loc) · 2.1 KB
/
TimescaleTestStore.cs
File metadata and controls
55 lines (45 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Npgsql;
using System.Collections.Concurrent;
using System.Data;
using System.Data.Common;
namespace CmdScale.EntityFrameworkCore.TimescaleDB.FunctionalTests.Utils;
public class TimescaleTestStore : RelationalTestStore
{
private static readonly ConcurrentDictionary<string, TimescaleTestStore> _sharedStores = new();
private TimescaleTestStore(string name, bool shared, string connectionString)
: base(name, shared, new NpgsqlConnection(connectionString))
{
}
public static TimescaleTestStore Create(string name, string connectionString)
=> new(name, shared: false, connectionString);
public static TimescaleTestStore GetOrCreateShared(string name, string connectionString)
=> _sharedStores.GetOrAdd(name, key =>
{
TimescaleTestStore store = new(name, shared: true, connectionString);
DbContextOptions<MigrationsInfrastructureFixtureBase.MigrationsContext> options =
new DbContextOptionsBuilder<MigrationsInfrastructureFixtureBase.MigrationsContext>()
.UseNpgsql(connectionString).UseTimescaleDb().Options;
_ = store.InitializeAsync(null,
() => new MigrationsInfrastructureFixtureBase.MigrationsContext(options), null).Result;
return store;
});
public override DbContextOptionsBuilder AddProviderOptions(DbContextOptionsBuilder builder)
=> builder.AddInterceptors(new TimescaleMigrationsTestInterceptor()).UseNpgsql(ConnectionString, options => { })
.UseTimescaleDb().EnableSensitiveDataLogging();
public override Task CleanAsync(DbContext context)
{
context.Database.EnsureClean();
return Task.CompletedTask;
}
public void ExecuteScript(string script)
{
if (ConnectionState != ConnectionState.Open)
Connection.Open();
using DbCommand cmd = Connection.CreateCommand();
cmd.CommandText = script;
cmd.ExecuteNonQuery();
}
}