-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpgradeMigrations.cs
63 lines (48 loc) · 2.87 KB
/
UpgradeMigrations.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
using Onestop.Seo.Models;
using Orchard.ContentManagement;
using Orchard.Data.Migration;
using Orchard.Environment.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using Upgrade.Services;
namespace Onestop.Seo {
[OrchardFeature("Onestop.Seo.Upgrade")]
public class UpgradeMigrations : DataMigrationImpl {
private readonly IContentManager _contentManager;
private readonly IUpgradeService _upgradeService;
public UpgradeMigrations(IContentManager contentManager, IUpgradeService upgradeService) {
_contentManager = contentManager;
_upgradeService = upgradeService;
}
// Migrating versioned SeoGlobalSettingsPartRecord entries to versioned infoset.
public int Create() {
var tableName = _upgradeService.GetPrefixedTableName("Onestop_SEO_SeoGlobalSettingsPartRecord");
IEnumerable<ContentItem> allVersions = null;
if (_upgradeService.TableExists(tableName)) {
_upgradeService.ExecuteReader(string.Format("SELECT * FROM {0}", tableName),
(reader, connection) => {
if (allVersions == null) allVersions = _contentManager.GetAllVersions((int)reader["ContentItemRecord_id"]);
var item = allVersions.FirstOrDefault(p => p.VersionRecord.Id == ConvertFromDBValue<int>(reader["Id"]));
if (item != null && item.Has<SeoGlobalSettingsPart>()) {
var part = item.As<SeoGlobalSettingsPart>();
part.HomeTitle = ConvertFromDBValue<string>(reader["HomeTitle"]);
part.HomeDescription = ConvertFromDBValue<string>(reader["HomeDescription"]);
part.HomeKeywords = ConvertFromDBValue<string>(reader["HomeKeywords"]);
part.SeoPatternsDefinition = ConvertFromDBValue<string>(reader["SeoPatternsDefinition"]);
part.SearchTitlePattern = ConvertFromDBValue<string>(reader["SearchTitlePattern"]);
part.EnableCanonicalUrls = ConvertFromDBValue<bool>(reader["EnableCanonicalUrls"]);
part.TitleOverrideMaxLength = ConvertFromDBValue<int>(reader["TitleOverrideMaxLength"]);
part.DescriptionOverrideMaxLength = ConvertFromDBValue<int>(reader["DescriptionOverrideMaxLength"]);
part.KeywordsOverrideMaxLength = ConvertFromDBValue<int>(reader["KeywordsOverrideMaxLength"]);
}
});
_upgradeService.ExecuteReader(string.Format("DROP TABLE {0}", tableName), null);
}
return 1;
}
private static T ConvertFromDBValue<T>(object obj) {
return (obj == null || obj == DBNull.Value) ? default(T) : (T)obj;
}
}
}