public static void MigrateUp(string connectionString, string MigrationNamespace, long?toVersion = null) { var announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s)); var assembly = Assembly.GetExecutingAssembly(); var migrationContext = new RunnerContext(announcer) { Namespace = MigrationNamespace }; var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 }; var factory = new FluentMigrator.Runner.Processors.Postgres.PostgresProcessorFactory(); using (var processor = factory.Create(connectionString, announcer, options)) { var runner = new MigrationRunner(assembly, migrationContext, processor); if (toVersion == null) { runner.MigrateUp(true); } else { runner.MigrateUp(toVersion.Value); } } }
public void UpgradeDatabase() { var assembly = System.Reflection.Assembly.GetExecutingAssembly(); var announcer = new TextWriterAnnouncer(s => Log.Debug(s)); var context = new RunnerContext(announcer) { Namespace = "Causal.Server.Migrations" }; var providerName = ConfigurationManager.ConnectionStrings["CausalContext"].ProviderName; MigrationProcessorFactory factory; if (providerName.Equals("Npgsql", StringComparison.InvariantCultureIgnoreCase)) { factory = new FluentMigrator.Runner.Processors.Postgres.PostgresProcessorFactory(); } else if (providerName.Equals("System.Data.SqlClient", StringComparison.InvariantCultureIgnoreCase)) { factory = new FluentMigrator.Runner.Processors.SqlServer.SqlServer2008ProcessorFactory(); } else { throw new Exception("Unknown provider for connection string CausalContext"); } var options = new ProcessorOptions { PreviewOnly = false }; var processor = factory.Create(ConfigurationManager.ConnectionStrings["CausalContext"].ConnectionString, announcer, options); var migrator = new MigrationRunner(assembly, context, processor); migrator.MigrateUp(true); }
public void MigrateToLatest() { var announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s)); var assembly = Assembly.GetExecutingAssembly(); var migrationContext = new RunnerContext(announcer) { Targets = new[] { assembly.FullName } }; var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 }; var factory = new FluentMigrator.Runner.Processors.Postgres.PostgresProcessorFactory(); var processor = factory.Create(_connectionString, announcer, options); var runner = new MigrationRunner(assembly, migrationContext, processor); runner.MigrateUp(true); }
/// <summary> /// Runs database migrations of the specified module descriptor. /// </summary> /// <param name="moduleDescriptor">The module descriptor.</param> /// <param name="up">if set to <c>true</c> migrates up; otherwise migrates down.</param> public void Migrate(ModuleDescriptor moduleDescriptor, bool up = true) { var announcer = new TextWriterAnnouncer( s => { if (!string.IsNullOrWhiteSpace(s)) { Log.Info(string.Concat("Migration on ", moduleDescriptor.Name, ". ", s)); } }); var assembly = moduleDescriptor.GetType().Assembly; var migrationTypes = assemblyLoader.GetLoadableTypes(assembly, typeof(Migration)); if (!migrationTypes.Any()) { Log.Info(string.Concat("Migration on ", moduleDescriptor.Name, ". No migrations found.")); return; } var migrationContext = new RunnerContext(announcer) { Namespace = migrationTypes.First().Namespace }; IMigrationProcessorOptions options = new ProcessorOptions { PreviewOnly = false, Timeout = (int)migrationTimeout.TotalSeconds }; IMigrationProcessor processor; var connectionString = ConfigurationManager.ConnectionStrings["BetterCms"].ConnectionString; if (databaseType == DatabaseType.SqlAzure || databaseType == DatabaseType.SqlServer) { var factory = new FluentMigrator.Runner.Processors.SqlServer.SqlServer2008ProcessorFactory(); processor = factory.Create(connectionString, announcer, options); } else if (databaseType == DatabaseType.PostgreSQL) { var factory = new FluentMigrator.Runner.Processors.Postgres.PostgresProcessorFactory(); processor = factory.Create(connectionString, announcer, options); } else if (databaseType == DatabaseType.Oracle) { var factory = new FluentMigrator.Runner.Processors.Oracle.OracleProcessorFactory(); processor = factory.Create(connectionString, announcer, options); } else { throw new NotSupportedException(string.Format("Database type {0} is not supported for data migrations.", databaseType)); } var runner = new MigrationRunner(assembly, migrationContext, processor); if (up) { runner.MigrateUp(); } else { runner.MigrateDown(0); } }