Skip to content

Commit

Permalink
feat: added composer, model and components for migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Imran committed Nov 11, 2020
1 parent 93d5ce4 commit 83df507
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,18 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Components\BackOfficeContentBlockerComponent.cs" />
<Compile Include="Composers\BackOfficeContentBlockerComposer.cs" />
<Compile Include="Migrations\AddCommentsTable.cs" />
<Compile Include="Models\Schemas\BackOfficeContentBlockerSchema.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\BackOfficeContentBlockerService.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Umbraco.SqlServerCE.4.0.0.1\build\Umbraco.SqlServerCE.targets" Condition="Exists('..\packages\Umbraco.SqlServerCE.4.0.0.1\build\Umbraco.SqlServerCE.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using BackOfficeContentBlocker.Core.Migrations;
using Umbraco.Core.Composing;
using Umbraco.Core.Logging;
using Umbraco.Core.Migrations;
using Umbraco.Core.Migrations.Upgrade;
using Umbraco.Core.Scoping;
using Umbraco.Core.Services;

namespace BackOfficeContentBlocker.Core.Components
{
public class BackOfficeContentBlockerComponent : IComponent
{
private readonly IScopeProvider _scopeProvider;
private readonly IMigrationBuilder _migrationBuilder;
private readonly IKeyValueService _keyValueService;
private readonly ILogger _logger;

public BackOfficeContentBlockerComponent(IScopeProvider scopeProvider, IMigrationBuilder migrationBuilder,
IKeyValueService keyValueService, ILogger logger)
{
_scopeProvider = scopeProvider;
_migrationBuilder = migrationBuilder;
_keyValueService = keyValueService;
_logger = logger;
}

public void Initialize()
{
// Create a migration plan for a specific project/feature
// We can then track that latest migration state/step for this project/feature
var migrationPlan = new MigrationPlan("BackOfficeContentBlocker");

// This is the steps we need to take
// Each step in the migration adds a unique value
migrationPlan.From(string.Empty)
.To<AddBackOfficeContentBlockerTable>("backofficecontentblocker-db");

// Go and upgrade our site (Will check if it needs to do the work or not)
// Based on the current/latest step
var upgrader = new Upgrader(migrationPlan);

upgrader.Execute(_scopeProvider, _migrationBuilder, _keyValueService, _logger);
}

public void Terminate()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using BackOfficeContentBlocker.Core.Components;
using Umbraco.Core;
using Umbraco.Core.Composing;

namespace BackOfficeContentBlocker.Core.Composers
{
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
public class BackOfficeContentBlockerComposer : ComponentComposer<BackOfficeContentBlockerComponent>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using BackOfficeContentBlocker.Core.Models.Schemas;
using Umbraco.Core.Logging;
using Umbraco.Core.Migrations;

namespace BackOfficeContentBlocker.Core.Migrations
{
public class AddBackOfficeContentBlockerTable : MigrationBase
{
public AddBackOfficeContentBlockerTable(IMigrationContext context) : base(context)
{
}

public override void Migrate()
{
Logger.Debug<AddBackOfficeContentBlockerTable>("Running migration {MigrationStep}", "AddCommentsTable");

// Lots of methods available in the MigrationBase class - discover with this.
if (TableExists("BackOfficeContentBlocker") == false)
{
Create.Table<BackOfficeContentBlockerSchema>().Do();
}
else
{
Logger.Debug<AddBackOfficeContentBlockerTable>("The database table {DbTable} already exists, skipping",
"BlogComments");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using NPoco;
using Umbraco.Core.Persistence.DatabaseAnnotations;

namespace BackOfficeContentBlocker.Core.Models.Schemas
{
[TableName("BackOfficeContentBlocker")]
[PrimaryKey("Id", AutoIncrement = true)]
[ExplicitColumns]
public class BackOfficeContentBlockerSchema
{
[PrimaryKeyColumn(AutoIncrement = true, IdentitySeed = 1)]
[Column("Id")]
public int Id { get; set; }

[Column("UserEmail")] public string UserEmail { get; set; }

[Column("PageId")] public int PageId { get; set; }

[Column("TimeStamp")] public int TimeStamp { get; set; }
}
}

0 comments on commit 83df507

Please sign in to comment.