Skip to content

Instantly share code, notes, and snippets.

@jamiepollock
Created October 21, 2014 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiepollock/8c527fb50fde65ef50e1 to your computer and use it in GitHub Desktop.
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.
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);
}
}
}
}
}
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