Created
October 21, 2014 13:35
-
-
Save jamiepollock/8c527fb50fde65ef50e1 to your computer and use it in GitHub Desktop.
A custom database event intended for pre-Umbraco v7.2 website using ArcheType which generates long data type prevalues.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using My.Website.ExtensionMethods; | |
using Umbraco.Core; | |
using Umbraco.Core.Persistence; | |
namespace My.Website.Events { | |
public class CustomDatabaseEvents : ApplicationEventHandler { | |
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { | |
ApplyUmbracoDatabaseHotfixForLongArchetypeModels(applicationContext.DatabaseContext.Database); | |
} | |
private void ApplyUmbracoDatabaseHotfixForLongArchetypeModels(UmbracoDatabase db) { | |
const string tableName = "cmsDataTypePreValues"; | |
const string columnName = "value"; | |
const string expectedColumnDataType = "ntext"; | |
if (db.TableExist(tableName)) { | |
var columnDataType = db.GetColumnDataType(tableName, columnName); | |
var hasValidField = string.Equals(columnDataType, expectedColumnDataType, | |
StringComparison.OrdinalIgnoreCase); | |
if (hasValidField == false) { | |
var updateSql = string.Format("ALTER TABLE {0} ALTER COLUMN {1} {2}", tableName, columnName, | |
expectedColumnDataType); | |
db.Execute(updateSql); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Umbraco.Core.Persistence; | |
namespace My.Website.ExtensionMethods { | |
public static class CustomDatabaseExtensionMethods { | |
public static string GetColumnDataType(this Database db, string tableName, string columName) { | |
var query = | |
new Sql().Select("DATA_TYPE").From("INFORMATION_SCHEMA.COLUMNS") | |
.Where(string.Format("(TABLE_NAME = '{0}') AND (COLUMN_NAME = '{1}')", tableName, columName)); | |
return db.ExecuteScalar<string>(query); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment