Skip to content

Commit 9c184f7

Browse files
committed
More EF 6 Migrations
1 parent 5217706 commit 9c184f7

35 files changed

Lines changed: 2038 additions & 180 deletions

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ csx
143143
AppPackages/
144144

145145
# Others
146-
sql/
146+
# sql/
147147
*.Cache
148148
ClientBin/
149149
[Ss]tyle[Cc]op.*
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Reflection;
7+
using System.Text;
8+
using SmartStore.Utilities;
9+
10+
namespace SmartStore.Core.Data
11+
{
12+
13+
public class SqlFileTokenizer
14+
{
15+
16+
public SqlFileTokenizer(string fileName, Assembly assembly = null, string location = null)
17+
{
18+
Guard.ArgumentNotEmpty(() => fileName);
19+
20+
this.FileName = fileName;
21+
this.Assembly = assembly;
22+
this.Location = location;
23+
}
24+
25+
public string FileName
26+
{
27+
get;
28+
private set;
29+
}
30+
31+
public Assembly Assembly
32+
{
33+
get;
34+
private set;
35+
}
36+
37+
public string Location
38+
{
39+
get;
40+
private set;
41+
}
42+
43+
public IEnumerable<string> Tokenize()
44+
{
45+
if (this.Assembly == null)
46+
{
47+
this.Assembly = Assembly.GetCallingAssembly();
48+
}
49+
50+
using (var reader = ReadSqlFile())
51+
{
52+
var statement = string.Empty;
53+
while ((statement = ReadNextSqlStatement(reader)) != null)
54+
{
55+
yield return statement;
56+
}
57+
}
58+
}
59+
60+
protected virtual StreamReader ReadSqlFile()
61+
{
62+
63+
var fileName = this.FileName;
64+
65+
if (fileName.StartsWith("~") || fileName.StartsWith("/"))
66+
{
67+
string path = CommonHelper.MapPath(fileName);
68+
if (!File.Exists(path))
69+
{
70+
return StreamReader.Null;
71+
}
72+
73+
return new StreamReader(File.OpenRead(path));
74+
}
75+
76+
// SQL file is obviously an embedded resource
77+
var assembly = this.Assembly;
78+
var asmName = assembly.FullName.Substring(0, assembly.FullName.IndexOf(','));
79+
var location = this.Location ?? asmName + ".Sql";
80+
var name = String.Format("{0}.{1}", location, fileName);
81+
var stream = assembly.GetManifestResourceStream(name);
82+
Debug.Assert(stream != null);
83+
return new StreamReader(stream);
84+
}
85+
86+
private string ReadNextSqlStatement(TextReader reader)
87+
{
88+
var sb = new StringBuilder();
89+
90+
string lineOfText;
91+
92+
while (true)
93+
{
94+
lineOfText = reader.ReadLine();
95+
if (lineOfText == null)
96+
{
97+
if (sb.Length > 0)
98+
return sb.ToString();
99+
else
100+
return null;
101+
}
102+
103+
if (lineOfText.TrimEnd().ToUpper() == "GO")
104+
break;
105+
106+
sb.Append(lineOfText + Environment.NewLine);
107+
}
108+
109+
return sb.ToString();
110+
}
111+
112+
}
113+
114+
}

src/Libraries/SmartStore.Core/SmartStore.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
<Compile Include="Data\Impex\ImportProgressInfo.cs" />
157157
<Compile Include="Data\Impex\ImportResult.cs" />
158158
<Compile Include="Data\RepositoryExtensions.cs" />
159+
<Compile Include="Data\SqlFileTokenizer.cs" />
159160
<Compile Include="Domain\Catalog\ProductBundleData.cs" />
160161
<Compile Include="Domain\Catalog\ProductBundleItemAttributeFilter.cs" />
161162
<Compile Include="Domain\Catalog\ProductBundleItem.cs" />

src/Libraries/SmartStore.Data/Extensions/DbMigrationExtensions.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Libraries/SmartStore.Data/IEfDataProvider.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,5 @@ public interface IEfDataProvider : IDataProvider
1212
/// <returns>Connection factory</returns>
1313
IDbConnectionFactory GetConnectionFactory();
1414

15-
/// <summary>
16-
/// Set database initializer
17-
/// </summary>
18-
IDatabaseInitializer<SmartObjectContext> GetDatabaseInitializer();
1915
}
2016
}

src/Libraries/SmartStore.Data/Migrations/201403010028232_Initial.cs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
namespace SmartStore.Data.Migrations
22
{
3-
using System;
4-
using System.Data.Entity.Migrations;
3+
using System;
4+
using System.Data.Entity.Migrations;
5+
using SmartStore.Core.Data;
6+
using SmartStore.Data.Setup;
57

68
public partial class Initial : DbMigration
79
{
810
public override void Up()
911
{
1012
if (DbMigrationContext.Current.SuppressInitialCreate<SmartObjectContext>())
1113
return;
12-
14+
15+
#region auto generated
16+
1317
CreateTable(
1418
"dbo.ProductBundleItemAttributeFilter",
1519
c => new
@@ -1755,12 +1759,37 @@ public override void Up()
17551759
.ForeignKey("dbo.Product", t => t.ProductId, cascadeDelete: true)
17561760
.Index(t => t.Id)
17571761
.Index(t => t.ProductId);
1758-
1759-
}
1762+
1763+
#endregion
1764+
1765+
#region Custom
1766+
1767+
this.SqlFile("Indexes.sql");
1768+
if (DataSettings.Current.IsSqlServer)
1769+
{
1770+
this.SqlFile("Indexes.SqlServer.sql");
1771+
this.SqlFile("StoredProcedures.sql");
1772+
}
1773+
1774+
#endregion
1775+
}
17601776

17611777
public override void Down()
1762-
{
1763-
DropForeignKey("dbo.ProductReview", "ProductId", "dbo.Product");
1778+
{
1779+
#region Custom
1780+
1781+
this.SqlFile("Indexes.Reverse.sql");
1782+
if (DataSettings.Current.IsSqlServer)
1783+
{
1784+
this.SqlFile("Indexes.SqlServer.Reverse.sql");
1785+
this.SqlFile("StoredProcedures.Reverse.sql");
1786+
}
1787+
1788+
#endregion
1789+
1790+
#region auto generated
1791+
1792+
DropForeignKey("dbo.ProductReview", "ProductId", "dbo.Product");
17641793
DropForeignKey("dbo.ProductReview", "Id", "dbo.CustomerContent");
17651794
DropForeignKey("dbo.PollVotingRecord", "PollAnswerId", "dbo.PollAnswer");
17661795
DropForeignKey("dbo.PollVotingRecord", "Id", "dbo.CustomerContent");
@@ -2049,6 +2078,8 @@ public override void Down()
20492078
DropTable("dbo.Product");
20502079
DropTable("dbo.ProductBundleItem");
20512080
DropTable("dbo.ProductBundleItemAttributeFilter");
2052-
}
2081+
2082+
#endregion
2083+
}
20532084
}
20542085
}

src/Libraries/SmartStore.Data/DbMigrationContext.cs renamed to src/Libraries/SmartStore.Data/Setup/DbMigrationContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Linq;
66
using System.Text;
77

8-
namespace SmartStore.Data
8+
namespace SmartStore.Data.Setup
99
{
1010

1111
public class DbMigrationContext

0 commit comments

Comments
 (0)