using Microsoft.Extensions.Logging; using NPoco; using Umbraco.Cms.Core.Composing; 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; using Umbraco.Cms.Infrastructure.Persistence; using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax; namespace Umbraco.Cms.Infrastructure.Migrations { /// /// Provides a base class to all migrations. /// public abstract partial class MigrationBase : IDiscoverable { /// /// Initializes a new instance of the class. /// /// A migration context. protected MigrationBase(IMigrationContext context) => Context = context; /// /// Gets the migration context. /// protected IMigrationContext Context { get; } /// /// Gets the logger. /// protected ILogger Logger => Context.Logger; /// /// Gets the Sql syntax. /// protected ISqlSyntaxProvider SqlSyntax => Context.SqlContext.SqlSyntax; /// /// Gets the database instance. /// protected IUmbracoDatabase Database => Context.Database; /// /// Gets the database type. /// protected DatabaseType DatabaseType => Context.Database.DatabaseType; /// /// Creates a new Sql statement. /// protected Sql Sql() => Context.SqlContext.Sql(); /// /// Creates a new Sql statement with arguments. /// protected Sql Sql(string sql, params object[] args) => Context.SqlContext.Sql(sql, args); /// /// Executes the migration. /// protected abstract void Migrate(); /// /// Runs the migration. /// public void Run() { Migrate(); // ensure there is no building expression // ie we did not forget to .Do() an expression if (Context.BuildingExpression) { throw new IncompleteMigrationExpressionException("The migration has run, but leaves an expression that has not run."); } } // ensures we are not already building, // ie we did not forget to .Do() an expression private protected T BeginBuild(T builder) { if (Context.BuildingExpression) throw new IncompleteMigrationExpressionException("Cannot create a new expression: the previous expression has not run."); Context.BuildingExpression = true; return builder; } /// /// Builds an Alter expression. /// public IAlterBuilder Alter => BeginBuild(new AlterBuilder(Context)); /// /// Builds a Create expression. /// public ICreateBuilder Create => BeginBuild(new CreateBuilder(Context)); /// /// Builds a Delete expression. /// public IDeleteBuilder Delete => BeginBuild(new DeleteBuilder(Context)); /// /// Builds an Execute expression. /// public IExecuteBuilder Execute => BeginBuild(new ExecuteBuilder(Context)); /// /// Builds an Insert expression. /// public IInsertBuilder Insert => BeginBuild(new InsertBuilder(Context)); /// /// Builds a Rename expression. /// public IRenameBuilder Rename => BeginBuild(new RenameBuilder(Context)); /// /// Builds an Update expression. /// public IUpdateBuilder Update => BeginBuild(new UpdateBuilder(Context)); } }