Skip to content

Instantly share code, notes, and snippets.

@skttl
Last active February 15, 2024 09:19
Show Gist options
  • Save skttl/2919433f3ff78f820c262c5c6a683667 to your computer and use it in GitHub Desktop.
Save skttl/2919433f3ff78f820c262c5c6a683667 to your computer and use it in GitHub Desktop.
Migrate Vendr database to Umbraco Commerce
using System;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Migrations;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.Migrations;
using Umbraco.Cms.Infrastructure.Migrations.Upgrade;
namespace Umbraco.Community.Migrations;
[ComposeBefore(typeof(Umbraco.Commerce.Cms.UmbracoCommerceComposer))]
public class MigrateVendrToCommerceComposer : ComponentComposer<MigrateVendrToCommerceComponent>
{
}
public class MigrateVendrToCommerceComponent : IComponent
{
private readonly ICoreScopeProvider _scopeProvider;
private readonly IMigrationPlanExecutor _migrationPlanExecutor;
private readonly IKeyValueService _keyValueService;
private readonly IRuntimeState _runtimeState;
public MigrateVendrToCommerceComponent(ICoreScopeProvider scopeProvider, IMigrationPlanExecutor migrationPlanExecutor, IKeyValueService keyValueService, IRuntimeState runtimeState)
{
_scopeProvider = scopeProvider;
_migrationPlanExecutor = migrationPlanExecutor;
_keyValueService = keyValueService;
_runtimeState = runtimeState;
}
public void Initialize()
{
if (_runtimeState.Level < RuntimeLevel.Run)
{
return;
}
var migrationPlan = new MigrationPlan(nameof(MigrateVendrToCommerceMigrations));
migrationPlan.From(string.Empty)
.To<MigrateVendrToCommerceMigrations.RenameDatabaseTables>(nameof(MigrateVendrToCommerceMigrations.RenameDatabaseTables))
.To<MigrateVendrToCommerceMigrations.SwapVendrPropertyEditors>(nameof(MigrateVendrToCommerceMigrations.SwapVendrPropertyEditors))
.To<MigrateVendrToCommerceMigrations.SwapVendrVariantsEditor>(nameof(MigrateVendrToCommerceMigrations.SwapVendrVariantsEditor))
.To<MigrateVendrToCommerceMigrations.SwapVendrPriceAmountAdjustments>(nameof(MigrateVendrToCommerceMigrations.SwapVendrPriceAmountAdjustments))
.To<MigrateVendrToCommerceMigrations.UpdateTemplatePaths>(nameof(MigrateVendrToCommerceMigrations.UpdateTemplatePaths))
.To<MigrateVendrToCommerceMigrations.UpdateMigrationsLog>(nameof(MigrateVendrToCommerceMigrations.UpdateMigrationsLog))
.To<MigrateVendrToCommerceMigrations.UpdateActivityLogs>(nameof(MigrateVendrToCommerceMigrations.UpdateActivityLogs))
;
var upgrader = new Upgrader(migrationPlan);
upgrader.Execute(_migrationPlanExecutor, _scopeProvider, _keyValueService);
}
public void Terminate() { }
}
public class MigrateVendrToCommerceMigrations
{
public class RenameDatabaseTables : MigrationBase
{
public RenameDatabaseTables(IMigrationContext context) : base(context)
{
}
protected override void Migrate()
{
try
{
var sql = Context.SqlContext.Sql(@"
EXEC sp_rename 'vendrCurrency', 'umbracoCommerceCurrency';
EXEC sp_rename 'vendrTaxClass', 'umbracoCommerceTaxClass';
EXEC sp_rename 'vendrStock', 'umbracoCommerceStock';
EXEC sp_rename 'vendrOrderStatus', 'umbracoCommerceOrderStatus';
EXEC sp_rename 'vendrEmailTemplate', 'umbracoCommerceEmailTemplate';
EXEC sp_rename 'vendrPaymentMethod', 'umbracoCommercePaymentMethod';
EXEC sp_rename 'vendrShippingMethod', 'umbracoCommerceShippingMethod';
EXEC sp_rename 'vendrCountry', 'umbracoCommerceCountry';
EXEC sp_rename 'vendrRegion', 'umbracoCommerceRegion';
EXEC sp_rename 'vendrCurrencyAllowedCountry', 'umbracoCommerceCurrencyAllowedCountry';
EXEC sp_rename 'vendrPaymentMethodAllowedCountryRegion', 'umbracoCommercePaymentMethodAllowedCountryRegion';
EXEC sp_rename 'vendrPaymentMethodCountryRegionPrice', 'umbracoCommercePaymentMethodCountryRegionPrice';
EXEC sp_rename 'vendrPaymentMethodPaymentProviderSetting', 'umbracoCommercePaymentMethodPaymentProviderSetting';
EXEC sp_rename 'vendrShippingMethodAllowedCountryRegion', 'umbracoCommerceShippingMethodAllowedCountryRegion';
EXEC sp_rename 'vendrShippingMethodCountryRegionPrice', 'umbracoCommerceShippingMethodCountryRegionPrice';
EXEC sp_rename 'vendrTaxClassCountryRegionTaxRate', 'umbracoCommerceTaxClassCountryRegionTaxRate';
EXEC sp_rename 'vendrDiscount', 'umbracoCommerceDiscount';
EXEC sp_rename 'vendrDiscountCode', 'umbracoCommerceDiscountCode';
EXEC sp_rename 'vendrOrder', 'umbracoCommerceOrder';
EXEC sp_rename 'vendrOrderProperty', 'umbracoCommerceOrderProperty';
EXEC sp_rename 'vendrOrderLine', 'umbracoCommerceOrderLine';
EXEC sp_rename 'vendrOrderLineProperty', 'umbracoCommerceOrderLineProperty';
EXEC sp_rename 'vendrGiftCard', 'umbracoCommerceGiftCard';
EXEC sp_rename 'vendrOrderAppliedDiscountCode', 'umbracoCommerceOrderAppliedDiscountCode';
EXEC sp_rename 'vendrOrderAppliedGiftCard', 'umbracoCommerceOrderAppliedGiftCard';
EXEC sp_rename 'vendrStoreAllowedUserRole', 'umbracoCommerceStoreAllowedUserRole';
EXEC sp_rename 'vendrStoreAllowedUser', 'umbracoCommerceStoreAllowedUser';
EXEC sp_rename 'vendrFrozenPrice', 'umbracoCommerceFrozenPrice';
EXEC sp_rename 'vendrGiftCardProperty', 'umbracoCommerceGiftCardProperty';
EXEC sp_rename 'vendrActivityLog', 'umbracoCommerceActivityLog';
EXEC sp_rename 'vendrOrderPriceAdjustment', 'umbracoCommerceOrderPriceAdjustment';
EXEC sp_rename 'vendrOrderAmountAdjustment', 'umbracoCommerceOrderAmountAdjustment';
EXEC sp_rename 'vendrProductAttribute', 'umbracoCommerceProductAttribute';
EXEC sp_rename 'vendrProductAttributeValue', 'umbracoCommerceProductAttributeValue';
EXEC sp_rename 'vendrTranslatedValue', 'umbracoCommerceTranslatedValue';
EXEC sp_rename 'vendrProductAttributePreset', 'umbracoCommerceProductAttributePreset';
EXEC sp_rename 'vendrProductAttributePresetAllowedAttribute', 'umbracoCommerceProductAttributePresetAllowedAttribute';
EXEC sp_rename 'vendrOrderLineAttribute', 'umbracoCommerceOrderLineAttribute';
EXEC sp_rename 'vendrPrintTemplate', 'umbracoCommercePrintTemplate';
EXEC sp_rename 'vendrExportTemplate', 'umbracoCommerceExportTemplate';
EXEC sp_rename 'vendrStoreEntityTag', 'umbracoCommerceStoreEntityTag';
EXEC sp_rename 'vendrMigrations', 'umbracoCommerceMigrations';
EXEC sp_rename 'vendrStore', 'umbracoCommerceStore';
");
Context.Database.Execute(sql);
}
catch (Exception)
{
// if it fails, we are probably running in SQLite. So try migrating using that syntax instead.
var sql = @"
ALTER TABLE vendrCurrency RENAME TO umbracoCommerceCurrency;
ALTER TABLE vendrTaxClass RENAME TO umbracoCommerceTaxClass;
ALTER TABLE vendrStock RENAME TO umbracoCommerceStock;
ALTER TABLE vendrOrderStatus RENAME TO umbracoCommerceOrderStatus;
ALTER TABLE vendrEmailTemplate RENAME TO umbracoCommerceEmailTemplate;
ALTER TABLE vendrPaymentMethod RENAME TO umbracoCommercePaymentMethod;
ALTER TABLE vendrShippingMethod RENAME TO umbracoCommerceShippingMethod;
ALTER TABLE vendrCountry RENAME TO umbracoCommerceCountry;
ALTER TABLE vendrRegion RENAME TO umbracoCommerceRegion;
ALTER TABLE vendrCurrencyAllowedCountry RENAME TO umbracoCommerceCurrencyAllowedCountry;
ALTER TABLE vendrPaymentMethodAllowedCountryRegion RENAME TO umbracoCommercePaymentMethodAllowedCountryRegion;
ALTER TABLE vendrPaymentMethodCountryRegionPrice RENAME TO umbracoCommercePaymentMethodCountryRegionPrice;
ALTER TABLE vendrPaymentMethodPaymentProviderSetting RENAME TO umbracoCommercePaymentMethodPaymentProviderSetting;
ALTER TABLE vendrShippingMethodAllowedCountryRegion RENAME TO umbracoCommerceShippingMethodAllowedCountryRegion;
ALTER TABLE vendrShippingMethodCountryRegionPrice RENAME TO umbracoCommerceShippingMethodCountryRegionPrice;
ALTER TABLE vendrTaxClassCountryRegionTaxRate RENAME TO umbracoCommerceTaxClassCountryRegionTaxRate;
ALTER TABLE vendrDiscount RENAME TO umbracoCommerceDiscount;
ALTER TABLE vendrDiscountCode RENAME TO umbracoCommerceDiscountCode;
ALTER TABLE vendrOrder RENAME TO umbracoCommerceOrder;
ALTER TABLE vendrOrderProperty RENAME TO umbracoCommerceOrderProperty;
ALTER TABLE vendrOrderLine RENAME TO umbracoCommerceOrderLine;
ALTER TABLE vendrOrderLineProperty RENAME TO umbracoCommerceOrderLineProperty;
ALTER TABLE vendrGiftCard RENAME TO umbracoCommerceGiftCard;
ALTER TABLE vendrOrderAppliedDiscountCode RENAME TO umbracoCommerceOrderAppliedDiscountCode;
ALTER TABLE vendrOrderAppliedGiftCard RENAME TO umbracoCommerceOrderAppliedGiftCard;
ALTER TABLE vendrStoreAllowedUserRole RENAME TO umbracoCommerceStoreAllowedUserRole;
ALTER TABLE vendrStoreAllowedUser RENAME TO umbracoCommerceStoreAllowedUser;
ALTER TABLE vendrFrozenPrice RENAME TO umbracoCommerceFrozenPrice;
ALTER TABLE vendrGiftCardProperty RENAME TO umbracoCommerceGiftCardProperty;
ALTER TABLE vendrActivityLog RENAME TO umbracoCommerceActivityLog;
ALTER TABLE vendrOrderPriceAdjustment RENAME TO umbracoCommerceOrderPriceAdjustment;
ALTER TABLE vendrOrderAmountAdjustment RENAME TO umbracoCommerceOrderAmountAdjustment;
ALTER TABLE vendrProductAttribute RENAME TO umbracoCommerceProductAttribute;
ALTER TABLE vendrProductAttributeValue RENAME TO umbracoCommerceProductAttributeValue;
ALTER TABLE vendrTranslatedValue RENAME TO umbracoCommerceTranslatedValue;
ALTER TABLE vendrProductAttributePreset RENAME TO umbracoCommerceProductAttributePreset;
ALTER TABLE vendrProductAttributePresetAllowedAttribute RENAME TO umbracoCommerceProductAttributePresetAllowedAttribute;
ALTER TABLE vendrOrderLineAttribute RENAME TO umbracoCommerceOrderLineAttribute;
ALTER TABLE vendrPrintTemplate RENAME TO umbracoCommercePrintTemplate;
ALTER TABLE vendrExportTemplate RENAME TO umbracoCommerceExportTemplate;
ALTER TABLE vendrStoreEntityTag RENAME TO umbracoCommerceStoreEntityTag;
ALTER TABLE vendrMigrations RENAME TO umbracoCommerceMigrations;
ALTER TABLE vendrStore RENAME TO umbracoCommerceStore;
";
Context.Database.Execute(sql);
}
}
}
public class SwapVendrPropertyEditors : MigrationBase
{
public SwapVendrPropertyEditors(IMigrationContext context) : base(context)
{
}
protected override void Migrate()
{
Context.Database.Execute(
@"UPDATE umbracoDataType
SET propertyEditorAlias = REPLACE(propertyEditorAlias, 'Vendr.', 'Umbraco.Commerce.')
WHERE propertyEditorAlias LIKE 'Vendr.%'"
);
}
}
public class SwapVendrVariantsEditor : MigrationBase
{
public SwapVendrVariantsEditor(IMigrationContext context) : base(context)
{
}
protected override void Migrate()
{
Context.Database.Execute(
@"
UPDATE umbracoPropertyData
SET textValue = REPLACE(textValue, 'Vendr.VariantsEditor', 'Umbraco.Commerce.VariantsEditor')
WHERE textValue LIKE '%Vendr.VariantsEditor%';
"
);
}
}
public class SwapVendrPriceAmountAdjustments : MigrationBase
{
public SwapVendrPriceAmountAdjustments(IMigrationContext context) : base(context)
{
}
protected override void Migrate()
{
Context.Database.Execute(
@"
UPDATE umbracoCommerceOrderPriceAdjustment
SET type = REPLACE(type, 'Vendr.', 'Umbraco.Commerce.')
WHERE type LIKE '%Vendr.%';
UPDATE umbracoCommerceOrderAmountAdjustment
SET type = REPLACE(type, 'Vendr.', 'Umbraco.Commerce.')
WHERE type LIKE '%Vendr.%';
"
);
}
}
public class UpdateTemplatePaths : MigrationBase
{
public UpdateTemplatePaths(IMigrationContext context) : base(context)
{
}
protected override void Migrate()
{
Context.Database.Execute(
@"
UPDATE umbracoCommerceEmailTemplate
SET templateView = REPLACE(templateView, '/App_Plugins/Vendr/templates/email', '/Views/UmbracoCommerce/Templates/Email')
WHERE templateView LIKE '%/Vendr/%';
UPDATE umbracoCommercePrintTemplate
SET templateView = REPLACE(templateView, '/App_Plugins/Vendr/templates/print', '/Views/UmbracoCommerce/Templates/Print')
WHERE templateView LIKE '%/Vendr/%';
UPDATE umbracoCommerceExportTemplate
SET templateView = REPLACE(templateView, '/App_Plugins/Vendr/templates/email', '/Views/UmbracoCommerce/Templates/Export')
WHERE templateView LIKE '%/Vendr/%';
"
);
}
}
public class UpdateMigrationsLog : MigrationBase
{
public UpdateMigrationsLog(IMigrationContext context) : base(context)
{
}
protected override void Migrate()
{
Context.Database.Execute(
@"
UPDATE umbracoCommerceMigrations
SET migration = REPLACE(migration, 'Vendr.', 'Umbraco.Commerce.')
WHERE migration LIKE 'Vendr.%';
"
);
}
}
public class UpdateActivityLogs : MigrationBase
{
public UpdateActivityLogs(IMigrationContext context) : base(context)
{
}
protected override void Migrate()
{
Context.Database.Execute(
@"
UPDATE umbracoCommerceActivityLog
SET eventType = REPLACE(eventType, 'vendr/', 'commerce/')
WHERE eventType LIKE 'vendr/%';
"
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment