forked from SimonCropp/LocalDb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSqlInstance.cs
202 lines (173 loc) · 8.14 KB
/
SqlInstance.cs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
namespace EfLocalDb
{
public class SqlInstance<TDbContext>
where TDbContext : DbContext
{
Wrapper wrapper = null!;
Func<DbContextOptionsBuilder<TDbContext>, TDbContext> constructInstance = null!;
public string ServerName => wrapper.ServerName;
public SqlInstance(
Func<DbContextOptionsBuilder<TDbContext>, TDbContext> constructInstance,
Func<TDbContext, Task>? buildTemplate = null,
string? instanceSuffix = null,
DateTime? timestamp = null,
ushort templateSize = 3)
{
Guard.AgainstWhiteSpace(nameof(instanceSuffix), instanceSuffix);
Guard.AgainstNull(nameof(constructInstance), constructInstance);
var instanceName = GetInstanceName(instanceSuffix);
var directory = DirectoryFinder.Find(instanceName);
var convertedBuildTemplate = BuildTemplateConverter.Convert(constructInstance, buildTemplate);
Init(convertedBuildTemplate, constructInstance, instanceName, directory, timestamp, templateSize);
}
public SqlInstance(
Func<DbContextOptionsBuilder<TDbContext>, TDbContext> constructInstance,
string name,
string directory,
Func<TDbContext, Task>? buildTemplate = null,
DateTime? timestamp = null,
ushort templateSize = 3)
{
var convertedBuildTemplate = BuildTemplateConverter.Convert(constructInstance, buildTemplate);
Init(convertedBuildTemplate, constructInstance, name, directory, timestamp, templateSize);
}
public SqlInstance(
Func<SqlConnection, DbContextOptionsBuilder<TDbContext>, Task> buildTemplate,
Func<DbContextOptionsBuilder<TDbContext>, TDbContext> constructInstance,
string? instanceSuffix = null,
DateTime? timestamp = null,
ushort templateSize = 3)
{
var instanceName = GetInstanceName(instanceSuffix);
var directory = DirectoryFinder.Find(instanceName);
Init(buildTemplate, constructInstance, instanceName, directory, timestamp, templateSize);
}
public SqlInstance(
Func<SqlConnection, DbContextOptionsBuilder<TDbContext>, Task> buildTemplate,
Func<DbContextOptionsBuilder<TDbContext>, TDbContext> constructInstance,
string name,
string directory,
DateTime? timestamp = null,
ushort templateSize = 3)
{
Init(buildTemplate, constructInstance, name, directory, timestamp, templateSize);
}
void Init(
Func<SqlConnection, DbContextOptionsBuilder<TDbContext>, Task> buildTemplate,
Func<DbContextOptionsBuilder<TDbContext>, TDbContext> constructInstance,
string name,
string directory,
DateTime? timestamp,
ushort templateSize)
{
Guard.AgainstNullWhiteSpace(nameof(directory), directory);
Guard.AgainstNullWhiteSpace(nameof(name), name);
Guard.AgainstNull(nameof(constructInstance), constructInstance);
this.constructInstance = constructInstance;
Task BuildTemplate(SqlConnection connection)
{
var builder = DefaultOptionsBuilder.Build<TDbContext>();
builder.UseSqlServer(connection);
return buildTemplate(connection, builder);
}
var resultTimestamp = timestamp.GetValueOrDefault(Timestamp.LastModified<TDbContext>());
wrapper = new Wrapper(name, directory, templateSize);
wrapper.Start(resultTimestamp, BuildTemplate);
}
static string GetInstanceName(string? scopeSuffix)
{
Guard.AgainstWhiteSpace(nameof(scopeSuffix), scopeSuffix);
#region GetInstanceName
if (scopeSuffix == null)
{
return typeof(TDbContext).Name;
}
return $"{typeof(TDbContext).Name}_{scopeSuffix}";
#endregion
}
public void Cleanup() => wrapper.DeleteInstance();
Task<string> BuildDatabase(string dbName)
{
return wrapper.CreateDatabaseFromTemplate(dbName, false);
}
/// <summary>
/// Build DB with a name based on the calling Method.
/// </summary>
/// <param name="data">The seed data.</param>
/// <param name="testFile">The path to the test class. Used to make the db name unique per test type.</param>
/// <param name="databaseSuffix">For Xunit theories add some text based on the inline data to make the db name unique.</param>
/// <param name="memberName">Used to make the db name unique per method. Will default to the caller method name is used.</param>
public Task<SqlDatabase<TDbContext>> Build(
IEnumerable<object>? data,
[CallerFilePath] string testFile = "",
string? databaseSuffix = null,
[CallerMemberName] string memberName = "")
{
Guard.AgainstNullWhiteSpace(nameof(testFile), testFile);
Guard.AgainstNullWhiteSpace(nameof(memberName), memberName);
Guard.AgainstWhiteSpace(nameof(databaseSuffix), databaseSuffix);
var testClass = Path.GetFileNameWithoutExtension(testFile);
var dbName = DbNamer.DeriveDbName(databaseSuffix, memberName, testClass);
return Build(dbName, data);
}
/// <summary>
/// Build DB with a name based on the calling Method.
/// </summary>
/// <param name="testFile">The path to the test class. Used to make the db name unique per test type.</param>
/// <param name="databaseSuffix">For Xunit theories add some text based on the inline data to make the db name unique.</param>
/// <param name="memberName">Used to make the db name unique per method. Will default to the caller method name is used.</param>
public Task<SqlDatabase<TDbContext>> Build(
[CallerFilePath] string testFile = "",
string? databaseSuffix = null,
[CallerMemberName] string memberName = "")
{
return Build(null, testFile, databaseSuffix, memberName);
}
public async Task<SqlDatabase<TDbContext>> Build(
string dbName,
IEnumerable<object>? data)
{
Guard.AgainstNullWhiteSpace(nameof(dbName), dbName);
var connection = await BuildDatabase(dbName);
var database = new SqlDatabase<TDbContext>(connection,dbName, constructInstance, () => wrapper.DeleteDatabase(dbName), data);
await database.Start();
return database;
}
public Task<SqlDatabase<TDbContext>> Build(string dbName)
{
return Build(dbName, (IEnumerable<object>?) null);
}
/// <summary>
/// Build DB with a transaction that is rolled back when disposed.
/// </summary>
/// <param name="data">The seed data.</param>
public Task<SqlDatabase<TDbContext>> BuildWithRollback(params object[] data)
{
return BuildWithRollback((IEnumerable<object>)data);
}
/// <summary>
/// Build DB with a transaction that is rolled back when disposed.
/// </summary>
/// <param name="data">The seed data.</param>
public async Task<SqlDatabase<TDbContext>> BuildWithRollback(IEnumerable<object> data)
{
var connection = await BuildWithRollbackDatabase();
var database = new SqlDatabase<TDbContext>(connection, constructInstance, data);
await database.Start();
return database;
}
async Task<string> BuildWithRollbackDatabase()
{
await wrapper.CreateWithRollbackDatabase();
return wrapper.WithRollbackConnectionString;
}
public string MasterConnectionString => wrapper.MasterConnectionString;
}
}