2021-06-15 15:05:57 +10:00
|
|
|
using Microsoft.Extensions.Logging;
|
2017-12-18 18:26:32 +01:00
|
|
|
using NPoco;
|
2021-06-24 13:35:57 -06:00
|
|
|
using Umbraco.Cms.Core.Composing;
|
2021-02-12 12:40:08 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Migrations.Expressions.Alter;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Migrations.Expressions.Create;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Migrations.Expressions.Delete;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Migrations.Expressions.Execute;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Migrations.Expressions.Insert;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Migrations.Expressions.Rename;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Migrations.Expressions.Update;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax;
|
2017-12-18 18:26:32 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
namespace Umbraco.Cms.Infrastructure.Migrations;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides a base class to all migrations.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract partial class MigrationBase : IDiscoverable
|
2017-12-18 18:26:32 +01:00
|
|
|
{
|
2017-12-19 15:06:18 +01:00
|
|
|
/// <summary>
|
2022-06-02 08:18:31 +02:00
|
|
|
/// Initializes a new instance of the <see cref="MigrationBase" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context">A migration context.</param>
|
|
|
|
|
protected MigrationBase(IMigrationContext context)
|
|
|
|
|
=> Context = context;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Builds an Alter expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IAlterBuilder Alter => BeginBuild(new AlterBuilder(Context));
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the migration context.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected IMigrationContext Context { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the logger.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected ILogger Logger => Context.Logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the Sql syntax.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected ISqlSyntaxProvider SqlSyntax => Context.SqlContext.SqlSyntax;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the database instance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected IUmbracoDatabase Database => Context.Database;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the database type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected DatabaseType DatabaseType => Context.Database.DatabaseType;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Builds a Create expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ICreateBuilder Create => BeginBuild(new CreateBuilder(Context));
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Builds a Delete expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IDeleteBuilder Delete => BeginBuild(new DeleteBuilder(Context));
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Builds an Execute expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IExecuteBuilder Execute => BeginBuild(new ExecuteBuilder(Context));
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Builds an Insert expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IInsertBuilder Insert => BeginBuild(new InsertBuilder(Context));
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Builds a Rename expression.
|
2017-12-19 15:06:18 +01:00
|
|
|
/// </summary>
|
2022-06-02 08:18:31 +02:00
|
|
|
public IRenameBuilder Rename => BeginBuild(new RenameBuilder(Context));
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Builds an Update expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IUpdateBuilder Update => BeginBuild(new UpdateBuilder(Context));
|
|
|
|
|
|
2023-04-13 12:23:44 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// If this is set to true, the published cache will be rebuild upon successful completion of the migration.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool RebuildCache { get; set; }
|
|
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Runs the migration.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Run()
|
2017-12-18 18:26:32 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
Migrate();
|
|
|
|
|
|
|
|
|
|
// ensure there is no building expression
|
|
|
|
|
// ie we did not forget to .Do() an expression
|
|
|
|
|
if (Context.BuildingExpression)
|
2018-11-15 19:34:32 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
throw new IncompleteMigrationExpressionException(
|
|
|
|
|
"The migration has run, but leaves an expression that has not run.");
|
2018-11-15 19:34:32 +01:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2018-11-15 19:34:32 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new Sql statement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected Sql<ISqlContext> Sql() => Context.SqlContext.Sql();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new Sql statement with arguments.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected Sql<ISqlContext> Sql(string sql, params object[] args) => Context.SqlContext.Sql(sql, args);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes the migration.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected abstract void Migrate();
|
|
|
|
|
|
|
|
|
|
// ensures we are not already building,
|
|
|
|
|
// ie we did not forget to .Do() an expression
|
|
|
|
|
private protected T BeginBuild<T>(T builder)
|
|
|
|
|
{
|
|
|
|
|
if (Context.BuildingExpression)
|
2018-11-15 19:34:32 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
throw new IncompleteMigrationExpressionException(
|
|
|
|
|
"Cannot create a new expression: the previous expression has not run.");
|
2018-11-15 19:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
Context.BuildingExpression = true;
|
|
|
|
|
return builder;
|
2017-12-18 18:26:32 +01:00
|
|
|
}
|
|
|
|
|
}
|