Fix the install process

This commit is contained in:
Stephan
2018-03-21 11:32:07 +01:00
parent df312309a3
commit 33a24e0e2f
6 changed files with 74 additions and 15 deletions

View File

@@ -2,6 +2,7 @@
using NPoco;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Migrations.Upgrade;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence.Dtos;
@@ -72,6 +73,9 @@ namespace Umbraco.Core.Migrations.Install
if (tableName.Equals(Constants.DatabaseSchema.Tables.TaskType))
CreateTaskTypeData();
if (tableName.Equals(Constants.DatabaseSchema.Tables.KeyValue))
CreateKeyValueData();
_logger.Info<DatabaseDataCreator>($"Done creating table {tableName} data.");
}
@@ -278,5 +282,16 @@ namespace Umbraco.Core.Migrations.Install
{
_database.Insert(Constants.DatabaseSchema.Tables.TaskType, "id", false, new TaskTypeDto { Id = 1, Alias = "toTranslate" });
}
private void CreateKeyValueData()
{
// on install, initialize the umbraco migration plan with the final state
var plan = new UmbracoPlan();
var stateValueKey = Upgrader.GetStateValueKey(plan);
var finalState = plan.FinalState;
_database.Insert(Constants.DatabaseSchema.Tables.KeyValue, "key", false, new KeyValueDto { Key = stateValueKey, Value = finalState, Updated = DateTime.Now });
}
}
}

View File

@@ -399,13 +399,13 @@ namespace Umbraco.Core.Migrations.Install
{
//Execute the Create Table sql
var created = _database.Execute(new Sql(createSql));
_logger.Info<Database>($"Create Table sql {created}:\n {createSql}");
_logger.Info<DatabaseSchemaCreator>($"Create Table '{tableName}' ({created}):\n {createSql}");
//If any statements exists for the primary key execute them here
if (string.IsNullOrEmpty(createPrimaryKeySql) == false)
{
var createdPk = _database.Execute(new Sql(createPrimaryKeySql));
_logger.Info<Database>($"Primary Key sql {createdPk}:\n {createPrimaryKeySql}");
_logger.Info<DatabaseSchemaCreator>($"Create Primary Key ({createdPk}):\n {createPrimaryKeySql}");
}
//Turn on identity insert if db provider is not mysql
@@ -431,21 +431,21 @@ namespace Umbraco.Core.Migrations.Install
foreach (var sql in indexSql)
{
var createdIndex = _database.Execute(new Sql(sql));
_logger.Info<Database>($"Create Index sql {createdIndex}:\n {sql}");
_logger.Info<DatabaseSchemaCreator>($"Create Index ({createdIndex}):\n {sql}");
}
//Loop through foreignkey statements and execute sql
foreach (var sql in foreignSql)
{
var createdFk = _database.Execute(new Sql(sql));
_logger.Info<Database>($"Create Foreign Key sql {createdFk}:\n {sql}");
_logger.Info<DatabaseSchemaCreator>($"Create Foreign Key ({createdFk}):\n {sql}");
}
transaction.Complete();
}
}
_logger.Info<Database>($"New table '{tableName}' was created");
_logger.Info<DatabaseSchemaCreator>($"Created table '{tableName}'");
}
public void DropTable(string tableName)

View File

@@ -23,7 +23,7 @@ namespace Umbraco.Core.Migrations.Upgrade
public string Name => Plan.Name;
public string StateValueKey => "Umbraco.Core.Upgrader.State+" + Plan.Name;
public string StateValueKey => GetStateValueKey(Plan);
protected IScopeProvider ScopeProvider { get; }
@@ -71,5 +71,7 @@ namespace Umbraco.Core.Migrations.Upgrade
scope.Complete();
}
}
public static string GetStateValueKey(MigrationPlan plan) => "Umbraco.Core.Upgrader.State+" + plan.Name;
}
}

View File

@@ -330,17 +330,19 @@ namespace Umbraco.Core.Runtime
{
// no scope, no key value service - just directly accessing the database
var umbracoPlan = new UmbracoPlan();
var stateValueKey = Upgrader.GetStateValueKey(umbracoPlan);
string state;
using (var database = databaseFactory.CreateDatabase())
{
var sql = databaseFactory.SqlContext.Sql()
.Select<KeyValueDto>()
.From<KeyValueDto>()
.Where<KeyValueDto>(x => x.Key == "Umbraco.Core.Upgrader.State+Umbraco.Core");
.Where<KeyValueDto>(x => x.Key == stateValueKey);
state = database.FirstOrDefault<KeyValueDto>(sql)?.Value;
}
var umbracoPlan = new UmbracoPlan();
var finalState = umbracoPlan.FinalState;
logger.Debug<CoreRuntime>($"Final upgrade state is \"{finalState}\", database contains \"{state ?? "<null>"}\".");

View File

@@ -1,9 +1,35 @@
namespace Umbraco.Core.Services
{
/// <summary>
/// Manages the simplified key/value store.
/// </summary>
public interface IKeyValueService
{
/// <summary>
/// Gets a value.
/// </summary>
/// <remarks>Returns <c>null</c> if no value was found for the key.</remarks>
string GetValue(string key);
/// <summary>
/// Sets a value.
/// </summary>
void SetValue(string key, string value);
/// <summary>
/// Sets a value.
/// </summary>
/// <remarks>Sets the value to <paramref name="newValue"/> if the value is <paramref name="originValue"/>,
/// and returns true; otherwise throws an exception. In other words, ensures that the value has not changed
/// before setting it.</remarks>
void SetValue(string key, string originValue, string newValue);
/// <summary>
/// Tries to set a value.
/// </summary>
/// <remarks>Sets the value to <paramref name="newValue"/> if the value is <paramref name="originValue"/>,
/// and returns true; otherwise returns false. In other words, ensures that the value has not changed
/// before setting it.</remarks>
bool TrySetValue(string key, string originValue, string newValue);
}
}

View File

@@ -39,23 +39,28 @@ namespace Umbraco.Core.Services.Implement
using (var scope = _scopeProvider.CreateScope())
{
// assume that if the lock object exists, then everything is ok
// assume that if the lock object for key/value exists, then everything is ok
if (scope.Database.Exists<LockDto>(Constants.Locks.KeyValues))
{
scope.Complete();
return;
}
// drop the 'identity' on primary key
// drop the 'identity' on umbracoLock primary key
foreach (var sql in new[]
{
// create a temp. id column and copy values
"alter table umbracoLock add column nid int null;",
"update umbracoLock set nid = id;",
// drop the id column entirely (cannot just drop identity)
"alter table umbracoLock drop constraint PK_umbracoLock;",
"alter table umbracoLock drop column id;",
// recreate the id column without identity and copy values
"alter table umbracoLock add column id int null;",
"update umbracoLock set id = nid;",
// drop the temp. id column
"alter table umbracoLock drop column nid;",
// complete the primary key
"alter table umbracoLock alter column id int not null;",
"alter table umbracoLock add constraint PK_umbracoLock primary key (id);"
})
@@ -73,6 +78,7 @@ VALUES ({Constants.Locks.KeyValues}, 'KeyValues', 1);");
}
}
/// <inheritdoc />
public string GetValue(string key)
{
EnsureInitialized();
@@ -86,6 +92,7 @@ VALUES ({Constants.Locks.KeyValues}, 'KeyValues', 1);");
}
}
/// <inheritdoc />
public void SetValue(string key, string value)
{
EnsureInitialized();
@@ -119,7 +126,15 @@ VALUES ({Constants.Locks.KeyValues}, 'KeyValues', 1);");
}
}
/// <inheritdoc />
public void SetValue(string key, string originValue, string newValue)
{
if (!TrySetValue(key, originValue, newValue))
throw new InvalidOperationException("Could not set the value.");
}
/// <inheritdoc />
public bool TrySetValue(string key, string originValue, string newValue)
{
EnsureInitialized();
@@ -130,11 +145,8 @@ VALUES ({Constants.Locks.KeyValues}, 'KeyValues', 1);");
var sql = scope.SqlContext.Sql().Select<KeyValueDto>().From<KeyValueDto>().Where<KeyValueDto>(x => x.Key == key);
var dto = scope.Database.Fetch<KeyValueDto>(sql).FirstOrDefault();
if (dto == null)
throw new InvalidOperationException("Key not found.");
if (dto.Value != originValue)
throw new InvalidOperationException("Value has changed.");
if (dto == null || dto.Value != originValue)
return false;
dto.Value = newValue;
dto.Updated = DateTime.Now;
@@ -142,6 +154,8 @@ VALUES ({Constants.Locks.KeyValues}, 'KeyValues', 1);");
scope.Complete();
}
return true;
}
}
}