Last active
September 6, 2023 09:32
-
-
Save drummerboy46/0caf52fca4c504212d24479fff8fab2d to your computer and use it in GitHub Desktop.
Umbraco 7 Migration of Url Redirects
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
SET ANSI_NULLS ON | |
GO | |
SET QUOTED_IDENTIFIER ON | |
GO | |
CREATE TABLE [dbo].[umbracoRedirectUrl2]( | |
[id] [uniqueidentifier] NOT NULL, | |
[createDateUtc] [datetime2](7) NOT NULL, | |
[url] [nvarchar](200) NOT NULL, | |
[contentKey] [uniqueidentifier] NOT NULL, | |
[urlHash] [nvarchar](50) NOT NULL, | |
CONSTRAINT [PK_umbracoRedirectUrl2] PRIMARY KEY CLUSTERED | |
( | |
[id] ASC | |
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] | |
) ON [PRIMARY] | |
GO |
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 NPoco; | |
using Umbraco.Cms.Core.Models; | |
using Umbraco.Cms.Core.Persistence.Repositories; | |
using Umbraco.Cms.Core.Services; | |
using Umbraco.Cms.Infrastructure.Migrations; | |
using Umbraco.Cms.Infrastructure.Scoping; | |
namespace Lcp.Core.Features.Shared.UrlRedirectsMigration | |
{ | |
public class UrlRedirectsMigration : MigrationBase | |
{ | |
private readonly IContentService _contentService; | |
private readonly IScopeProvider _scopeProvider; | |
private readonly IMigrationContext _context; | |
private readonly IRedirectUrlRepository _redirectUrlRepository; | |
public UrlRedirectsMigration( | |
IMigrationContext context, | |
IContentService contentService, | |
IScopeProvider scopeProvider, | |
IRedirectUrlRepository redirectUrlRepository | |
) : base(context) | |
{ | |
_contentService = contentService; | |
_scopeProvider = scopeProvider; | |
_context = context; | |
_redirectUrlRepository = redirectUrlRepository; | |
} | |
protected override void Migrate() | |
{ | |
using (var scope = _scopeProvider.CreateScope(autoComplete: true)) | |
{ | |
var db = scope.Database; | |
var sql = new Sql("SELECT * FROM umbracoRedirectUrl2"); | |
var urlRedirects = db.Fetch<UrlRedirectsModel>(sql); | |
foreach (var item in urlRedirects) | |
{ | |
var content = _contentService.GetById(item.ContentKey); | |
if (content is { Published: true }) | |
_redirectUrlRepository.Save(new RedirectUrl { ContentKey = content.Key, Url = item.Url, Culture = string.Empty }); | |
} | |
scope.Complete(); | |
} | |
} | |
} | |
} |
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.Cms.Core.Composing; | |
using Umbraco.Cms.Core.Migrations; | |
using Umbraco.Cms.Core.Scoping; | |
using Umbraco.Cms.Core.Services; | |
using Umbraco.Cms.Core; | |
using Umbraco.Cms.Infrastructure.Migrations.Upgrade; | |
using Umbraco.Cms.Infrastructure.Migrations; | |
namespace Lcp.Core.Features.Shared.UrlRedirectsMigration | |
{ | |
public class UrlRedirectsMigrationComposer : ComponentComposer<UrlRedirectsMigrationComponent> | |
{ | |
} | |
public class UrlRedirectsMigrationComponent : IComponent | |
{ | |
private readonly ICoreScopeProvider _scopeProvider; | |
private readonly IMigrationPlanExecutor _migrationPlanExecutor; | |
private readonly IKeyValueService _keyValueService; | |
private readonly IRuntimeState _runtimeState; | |
public UrlRedirectsMigrationComponent(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("UrlRedirectsMigration"); | |
migrationPlan.From(string.empty).To<UrlRedirectsMigration>("url-redirects-migration-1"); | |
var upgrader = new Upgrader(migrationPlan); | |
upgrader.Execute(_migrationPlanExecutor, _scopeProvider, _keyValueService); | |
} | |
public void Terminate() | |
{ | |
} | |
} | |
} |
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 System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Lcp.Core.Features.Shared.UrlRedirectsMigration | |
{ | |
public class UrlRedirectsModel | |
{ | |
public Guid Id { get; set; } | |
public DateTime CreateDateUtc { get; set; } | |
public string Url { get; set; } | |
public Guid ContentKey { get; set; } | |
public string UrlHash { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment