public Migrator(string connectionString, params Assembly[] assemblies) { var announcer = new TextWriterAnnouncer(Console.Write); IAssemblyCollection assemblyCollection = new AssemblyCollection(assemblies); var migrationContext = new RunnerContext(announcer); var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 }; PostgresProcessorFactory factory = new PostgresProcessorFactory(); IMigrationProcessor processor = factory.Create(connectionString, announcer, options); runner = new MigrationRunner(assemblyCollection, migrationContext, processor); }
/// <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> private void Migrate(ModuleDescriptor moduleDescriptor, IEnumerable<Type> migrationTypes = null, long? version = null) { var announcer = new TextWriterAnnouncer( s => { if (!string.IsNullOrWhiteSpace(s)) { Log.Info(string.Concat("Migration on ", moduleDescriptor.Name, ". ", s)); } }); var assembly = moduleDescriptor.GetType().Assembly; if (migrationTypes == null) { migrationTypes = assemblyLoader.GetLoadableTypes(assembly, typeof(Migration)); } if (migrationTypes == null || !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; IDbConnection dbConnection = null; string connectionString; if (!string.IsNullOrEmpty(configuration.Database.ConnectionString)) { connectionString = configuration.Database.ConnectionString; } else if (!string.IsNullOrEmpty(configuration.Database.ConnectionStringName)) { connectionString = ConfigurationManager.ConnectionStrings[configuration.Database.ConnectionStringName].ConnectionString; } else { throw new ConfigurationErrorsException("Missing connection string."); } if (databaseType == DatabaseType.SqlAzure || databaseType == DatabaseType.SqlServer) { var factory = new FluentMigrator.Runner.Processors.SqlServer.SqlServer2008ProcessorFactory(); processor = factory.Create(connectionString, announcer, options); dbConnection = ((SqlServerProcessor)processor).Connection; } else if (databaseType == DatabaseType.PostgreSQL) { var factory = new FluentMigrator.Runner.Processors.Postgres.PostgresProcessorFactory(); processor = factory.Create(connectionString, announcer, options); dbConnection = ((PostgresProcessor)processor).Connection; } else if (databaseType == DatabaseType.Oracle) { var factory = new FluentMigrator.Runner.Processors.Oracle.OracleProcessorFactory(); processor = factory.Create(connectionString, announcer, options); dbConnection = ((OracleProcessor)processor).Connection; } else { throw new NotSupportedException(string.Format("Database type {0} is not supported for data migrations.", databaseType)); } var runner = new MigrationRunner(assembly, migrationContext, processor); if (version != null) { runner.MigrateUp(version.Value); } else { throw new NotSupportedException("Migrations without target version are not supported."); } // If connection is still opened, close it. if (dbConnection != null && dbConnection.State != ConnectionState.Closed) { dbConnection.Close(); } }
/// <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> private void Migrate(ModuleDescriptor moduleDescriptor, IEnumerable <Type> migrationTypes = null, long?version = null) { var announcer = new TextWriterAnnouncer( s => { if (!string.IsNullOrWhiteSpace(s)) { Log.Info(string.Concat("Migration on ", moduleDescriptor.Name, ". ", s)); } }); var assembly = moduleDescriptor.GetType().Assembly; if (migrationTypes == null) { migrationTypes = assemblyLoader.GetLoadableTypes(assembly, typeof(Migration)); } if (migrationTypes == null || !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; IDbConnection dbConnection = null; string connectionString; if (!string.IsNullOrEmpty(configuration.Database.ConnectionString)) { connectionString = configuration.Database.ConnectionString; } else if (!string.IsNullOrEmpty(configuration.Database.ConnectionStringName)) { connectionString = ConfigurationManager.ConnectionStrings[configuration.Database.ConnectionStringName].ConnectionString; } else { throw new ConfigurationErrorsException("Missing connection string."); } if (databaseType == DatabaseType.SqlAzure || databaseType == DatabaseType.SqlServer) { var factory = new FluentMigrator.Runner.Processors.SqlServer.SqlServer2008ProcessorFactory(); processor = factory.Create(connectionString, announcer, options); dbConnection = ((SqlServerProcessor)processor).Connection; } else if (databaseType == DatabaseType.PostgreSQL) { var factory = new FluentMigrator.Runner.Processors.Postgres.PostgresProcessorFactory(); processor = factory.Create(connectionString, announcer, options); dbConnection = ((PostgresProcessor)processor).Connection; } else if (databaseType == DatabaseType.Oracle) { var factory = new FluentMigrator.Runner.Processors.Oracle.OracleProcessorFactory(); processor = factory.Create(connectionString, announcer, options); dbConnection = ((OracleProcessor)processor).Connection; } else { throw new NotSupportedException(string.Format("Database type {0} is not supported for data migrations.", databaseType)); } var runner = new MigrationRunner(assembly, migrationContext, processor); if (version != null) { runner.MigrateUp(version.Value); } else { throw new NotSupportedException("Migrations without target version are not supported."); } // If connection is still opened, close it. if (dbConnection != null && dbConnection.State != ConnectionState.Closed) { dbConnection.Close(); } }