Skip to content

Commit 5e51d3d

Browse files
Strowbridge, RexStrowbridge, Rex
Strowbridge, Rex
authored and
Strowbridge, Rex
committed
Added "if not exists" to index creation
1 parent 234bda1 commit 5e51d3d

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

Actias.SQLite/SqliteHelpers.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static IEnumerable<string> CreateIndexStatements(this DbModel model)
106106

107107
return indicies
108108
.Values
109-
.Select(index => string.Format("create index {0} on {1} ({2});", index.Name, index.Table, string.Join(", ", index.Columns)))
109+
.Select(index => string.Format("create index if not exists {0} on {1} ({2});", index.Name, index.Table, string.Join(", ", index.Columns)))
110110
.ToList();
111111
}
112112

Actias.SQLite/SqliteInitializer.cs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.Entity;
4+
5+
namespace Actias.SQLite
6+
{
7+
public class SqliteInitializer<T> : IDatabaseInitializer<T> where T : DbContext
8+
{
9+
public DbModelBuilder ModelBuilder { get; private set; }
10+
public string DbPath { get; private set; }
11+
12+
public SqliteInitializer(string dbPath, DbModelBuilder modelBuilder)
13+
{
14+
DbPath = dbPath;
15+
ModelBuilder = modelBuilder;
16+
}
17+
18+
public virtual void InitializeDatabase(T context)
19+
{
20+
var model = ModelBuilder.Build(context.Database.Connection);
21+
22+
using (var transaction = context.Database.BeginTransaction())
23+
{
24+
try
25+
{
26+
SqliteHelpers.CreateDatabase(context.Database, model);
27+
transaction.Commit();
28+
}
29+
catch (Exception)
30+
{
31+
transaction.Rollback();
32+
throw;
33+
}
34+
}
35+
36+
using (var transaction = context.Database.BeginTransaction())
37+
{
38+
try
39+
{
40+
Seed(context);
41+
transaction.Commit();
42+
}
43+
catch (Exception)
44+
{
45+
transaction.Rollback();
46+
throw;
47+
}
48+
}
49+
}
50+
51+
protected virtual void Seed(T context) { }
52+
}
53+
54+
public class Index
55+
{
56+
public string Name { get; set; }
57+
public string Table { get; set; }
58+
public List<string> Columns { get; set; }
59+
}
60+
}

0 commit comments

Comments
 (0)