public void SetUp() { factory = new OracleProcessorFactory(); connectionString = "Data Source=localhost/XE;User Id=Something;Password=Something"; announcer = new NullAnnouncer(); options = new ProcessorOptions(); }
/// <summary> /// Asserts that a given oracle table have been migrated /// </summary> /// <param name="createTables">The tables to be checked</param> protected void AssertOracleTablesExist(params CreateTableExpression[] createTables) { if (createTables == null) return; var oracleProcessor = new OracleProcessorFactory().Create(OracleContext.ConnectionString, new NullAnnouncer(), new ProcessorOptions()); foreach (var create in createTables) Assert.IsTrue(oracleProcessor.TableExists(string.Empty, create.TableName), "Oracle"); }
/// <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(); } }
/// <summary> /// Gets data that exists in an existing Oracle table /// </summary> /// <param name="tableName"></param> /// <returns></returns> private DataSet GetOracleTableData(string tableName) { var oracleProcessor = new OracleProcessorFactory().Create(_oracleContext.ConnectionString, new NullAnnouncer(), new ProcessorOptions()); return oracleProcessor.ReadTableData(string.Empty, tableName); }