/// <summary>
        /// The ensure migration key.
        /// </summary>
        /// <param name="schemaResult">
        /// The schema result.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string EnsureMigrationKey(MerchelloDatabaseSchemaResult schemaResult)
        {
            var migrationSetting =
                schemaResult.StoreSettings.FirstOrDefault(
                    x => x.Key == Constants.StoreSetting.MigrationKey);

            var nullSettingKey = Guid.NewGuid().ToString();

            if (migrationSetting == null)
            {
                this.InsertMigrationKey(nullSettingKey);
            }

            var migrationKey = migrationSetting != null?
                               string.IsNullOrEmpty(migrationSetting.Value) ? nullSettingKey : migrationSetting.Value:
                               nullSettingKey;

            // Saves a previously saved migration key without a value
            if (migrationKey.Equals(nullSettingKey))
            {
                var setting =
                    MerchelloContext.Current.Services.StoreSettingService.GetByKey(
                        Constants.StoreSetting.MigrationKey);
                if (setting != null)
                {
                    setting.Value = migrationKey;
                }
                MerchelloContext.Current.Services.StoreSettingService.Save(setting);
            }

            Guid validGuid;

            if (Guid.TryParse(migrationKey, out validGuid))
            {
                if (validGuid.Equals(Guid.Empty))
                {
                    // reset the key
                    nullSettingKey = Guid.NewGuid().ToString();
                    var dto = migrationSetting;
                    if (dto != null)
                    {
                        dto.Value = nullSettingKey;
                        _database.Update(dto);
                        migrationKey = nullSettingKey;
                    }
                }
            }

            return(migrationKey);
        }
Exemplo n.º 2
0
        /// <summary>
        /// The ensure migration key.
        /// </summary>
        /// <param name="schemaResult">
        /// The schema result.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string EnsureMigrationKey(MerchelloDatabaseSchemaResult schemaResult)
        {
            var migrationSetting =
                schemaResult.StoreSettings.FirstOrDefault(
                    x => x.Key == Constants.StoreSettingKeys.MigrationKey);

            var nullSettingKey = Guid.NewGuid().ToString();

            if (migrationSetting == null)
            {
                this.InsertMigrationKey(nullSettingKey);
            }

            var migrationKey = migrationSetting != null?
                               string.IsNullOrEmpty(migrationSetting.Value) ? nullSettingKey : migrationSetting.Value:
                               nullSettingKey;

            Guid validGuid;

            if (Guid.TryParse(migrationKey, out validGuid))
            {
                if (validGuid.Equals(Guid.Empty))
                {
                    // reset the key
                    nullSettingKey = Guid.NewGuid().ToString();
                    var dto = migrationSetting;
                    if (dto != null)
                    {
                        dto.Value = nullSettingKey;
                        _database.Update(dto);
                        migrationKey = nullSettingKey;
                    }
                }
            }

            return(migrationKey);
        }