using System;
using NPoco;
using Umbraco.Core.Logging;
using Umbraco.Core.Migrations.Expressions.Alter;
using Umbraco.Core.Migrations.Expressions.Create;
using Umbraco.Core.Migrations.Expressions.Delete;
using Umbraco.Core.Migrations.Expressions.Execute;
using Umbraco.Core.Migrations.Expressions.Insert;
using Umbraco.Core.Migrations.Expressions.Rename;
using Umbraco.Core.Migrations.Expressions.Update;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.SqlSyntax;
namespace Umbraco.Core.Migrations
{
///
/// Provides a base class to all migrations.
///
public abstract partial class MigrationBase : IMigration
{
///
/// 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.
///
public abstract void Migrate();
///
void IMigration.Migrate()
{
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 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));
}
}