2017-12-18 18:26:32 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using NPoco;
|
|
|
|
|
|
using Umbraco.Core.Logging;
|
2017-12-19 15:06:18 +01:00
|
|
|
|
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;
|
2017-12-18 18:26:32 +01:00
|
|
|
|
using Umbraco.Core.Persistence;
|
|
|
|
|
|
using Umbraco.Core.Persistence.SqlSyntax;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Migrations
|
|
|
|
|
|
{
|
2017-12-19 15:06:18 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provides a base class to all migrations.
|
|
|
|
|
|
/// </summary>
|
2018-01-15 17:32:45 +01:00
|
|
|
|
public abstract partial class MigrationBase : IMigration
|
2017-12-18 18:26:32 +01:00
|
|
|
|
{
|
2017-12-19 15:06:18 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="MigrationBase"/> class.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="context">A migration context.</param>
|
2017-12-18 18:26:32 +01:00
|
|
|
|
protected MigrationBase(IMigrationContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
Context = context;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-19 15:06:18 +01:00
|
|
|
|
/// <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>
|
|
|
|
|
|
/// 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);
|
|
|
|
|
|
|
2018-11-15 19:34:32 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Executes the migration.
|
|
|
|
|
|
/// </summary>
|
2017-12-22 12:29:56 +01:00
|
|
|
|
public abstract void Migrate();
|
2017-12-18 18:26:32 +01:00
|
|
|
|
|
2018-11-15 19:34:32 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
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>(T builder)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Context.BuildingExpression)
|
|
|
|
|
|
throw new IncompleteMigrationExpressionException("Cannot create a new expression: the previous expression has not run.");
|
|
|
|
|
|
Context.BuildingExpression = true;
|
|
|
|
|
|
return builder;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-19 15:06:18 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Builds an Alter expression.
|
|
|
|
|
|
/// </summary>
|
2018-11-15 19:34:32 +01:00
|
|
|
|
public IAlterBuilder Alter => BeginBuild(new AlterBuilder(Context));
|
2017-12-18 18:26:32 +01:00
|
|
|
|
|
2017-12-19 15:06:18 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Builds a Create expression.
|
|
|
|
|
|
/// </summary>
|
2018-11-15 19:34:32 +01:00
|
|
|
|
public ICreateBuilder Create => BeginBuild(new CreateBuilder(Context));
|
2017-12-18 18:26:32 +01:00
|
|
|
|
|
2017-12-19 15:06:18 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Builds a Delete expression.
|
|
|
|
|
|
/// </summary>
|
2018-11-15 19:34:32 +01:00
|
|
|
|
public IDeleteBuilder Delete => BeginBuild(new DeleteBuilder(Context));
|
2017-12-18 18:26:32 +01:00
|
|
|
|
|
2017-12-19 15:06:18 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Builds an Execute expression.
|
|
|
|
|
|
/// </summary>
|
2018-11-15 19:34:32 +01:00
|
|
|
|
public IExecuteBuilder Execute => BeginBuild(new ExecuteBuilder(Context));
|
2017-12-18 18:26:32 +01:00
|
|
|
|
|
2017-12-19 15:06:18 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Builds an Insert expression.
|
|
|
|
|
|
/// </summary>
|
2018-11-15 19:34:32 +01:00
|
|
|
|
public IInsertBuilder Insert => BeginBuild(new InsertBuilder(Context));
|
2017-12-18 18:26:32 +01:00
|
|
|
|
|
2017-12-19 15:06:18 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Builds a Rename expression.
|
|
|
|
|
|
/// </summary>
|
2018-11-15 19:34:32 +01:00
|
|
|
|
public IRenameBuilder Rename => BeginBuild(new RenameBuilder(Context));
|
2017-12-18 18:26:32 +01:00
|
|
|
|
|
2017-12-19 15:06:18 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Builds an Update expression.
|
|
|
|
|
|
/// </summary>
|
2018-11-15 19:34:32 +01:00
|
|
|
|
public IUpdateBuilder Update => BeginBuild(new UpdateBuilder(Context));
|
2017-12-18 18:26:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|