/// <summary> /// Initiates a migration operation to the target migration version. /// </summary> /// <param name="plugin">The plugin whose migrations should be run.</param> /// <param name="targetMigration">The target migration.</param> public virtual void Migrate(EntityPlugin plugin, string targetMigration = null) { Logger.LogInformation(LoggingEvents.MigratingId, LoggingEvents.Migrating, plugin.Identifier, Connection.DbConnection.Database, Connection.DbConnection.DataSource); // // Verify the history table exists and if not create it. // if (!PluginHistoryRepository.Exists()) { var command = RawSqlCommandBuilder.Build(PluginHistoryRepository.GetCreateScript()); var query = new RelationalCommandParameterObject(Connection, null, null, CurrentContext.Context, CommandLogger); command.ExecuteNonQuery(query); } // // Get all the command lists to be executed. // var commandLists = GetMigrationCommandLists( plugin, PluginHistoryRepository.GetAppliedMigrations(plugin), targetMigration); // // Execute each command list in order. // foreach (var commandList in commandLists) { MigrationCommandExecutor.ExecuteNonQuery(commandList(), Connection); } }
/// <summary> /// Generates down SQL scripts. /// </summary> /// <param name="plugin">The plugin.</param> /// <param name="migration">The migration.</param> /// <param name="previousMigration">The previous migration.</param> /// <returns></returns> protected virtual IReadOnlyList <MigrationCommand> GenerateDownSql(EntityPlugin plugin, Migration migration, Migration previousMigration) { var historyScript = PluginHistoryRepository.GetDeleteScript(plugin, migration.GetType().GetCustomAttribute <MigrationAttribute>().Id); var historyCommand = RawSqlCommandBuilder.Build(historyScript); return(MigrationsSqlGenerator .Generate(migration.DownOperations, previousMigration?.TargetModel) .Concat(new[] { new MigrationCommand(historyCommand, CurrentContext.Context, CommandLogger) }) .ToList()); }
/// <summary> /// Generates up SQL scripts. /// </summary> /// <param name="plugin">The plugin.</param> /// <param name="migration">The migration.</param> /// <returns></returns> protected virtual IReadOnlyList <MigrationCommand> GenerateUpSql(EntityPlugin plugin, Migration migration) { var migrationId = migration.GetType().GetCustomAttribute <MigrationAttribute>()?.Id; var historyRow = new HistoryRow(migrationId, ProductInfo.GetVersion()); var historyScript = PluginHistoryRepository.GetInsertScript(plugin, historyRow); var historyCommand = RawSqlCommandBuilder.Build(historyScript); return(MigrationsSqlGenerator .Generate(migration.UpOperations, migration.TargetModel) .Concat(new[] { new MigrationCommand(historyCommand, CurrentContext.Context, CommandLogger) }) .ToList()); }