-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathDbInitializer.cs
36 lines (30 loc) · 929 Bytes
/
DbInitializer.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
using System.Data;
using System.Diagnostics.CodeAnalysis;
using Dapper;
using HappyCode.NetCoreBoilerplate.BooksModule.Dtos;
namespace HappyCode.NetCoreBoilerplate.BooksModule.Infrastructure;
[ExcludeFromCodeCoverage]
internal class DbInitializer
{
private static readonly string _createBooks = @$"
CREATE TABLE IF NOT EXISTS Books
(
{nameof(BookDto.Id)} INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
{nameof(BookDto.Title)} TEXT
)
";
private readonly IDbConnection _db;
public DbInitializer(IDbConnection db)
{
_db = db;
}
public void Init()
{
_db.Execute(_createBooks);
var count = _db.ExecuteScalar<int>("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name = 'Books'");
if (count == 0)
{
throw new ApplicationException("SQLite DB not initialized properly");
}
}
}