Example #1
0
        public void Migrate(Action<IMigrationRunner> runnerAction)
        {
            var options = new MigrationOptions
            {
                PreviewOnly = false,
                Timeout = 0
            };

            var factory = new SqlServer2008ProcessorFactory();
            var announer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s));
            var migrationContext = new RunnerContext(announer);
            var processor = factory.Create(this.connectionString, announer, options);
            var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), migrationContext, processor);

            runnerAction(runner);
        }
        private static MigrationRunner GetRunner(string connectionString)
        {
            var announcer = new TextWriterAnnouncer(Console.WriteLine);
            var assembly = Assembly.GetExecutingAssembly();

            var migrationContext = new RunnerContext(announcer)
            {
                Namespace = "Rainfall.Integration.Migrations"
            };

            var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 };
            var factory = new SqlServer2008ProcessorFactory();
            var processor = factory.Create(connectionString, announcer, options);
            var runner = new MigrationRunner(assembly, migrationContext, processor);

            return runner;
        }
        /// <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();
            }
        }
        private static IEnumerable<MigrationRunner> CreateMigrator(Assembly migrationAssembly, IEnumerable<string> connectionStrings)
        {
            var nullAnnouncer = new NullAnnouncer();

            foreach (
                var connectionString in connectionStrings.Where(connectionString => ConfigurationManager.ConnectionStrings[connectionString] != null))
            {
                if (string.IsNullOrEmpty(connectionString))
                    throw new Exception(
                        string.Format("Could not find a valid connection string to run the migrations against"));

                var factory = new SqlServer2008ProcessorFactory();
                var processor =
                    factory.Create(ConfigurationManager.ConnectionStrings[connectionString].ConnectionString,
                        nullAnnouncer,
                        new ProcessorOptions());

                var context = new RunnerContext(nullAnnouncer);

                var runner = new MigrationRunner(migrationAssembly, context, processor);

                Console.WriteLine(string.Format("Creating migration using {0}", connectionString));

                yield return runner;
            }
        }
        private static IEnumerable<MigrationRunner> CreateProfileRunner(Assembly migrationAssembly)
        {
            var nullAnnouncer = new NullAnnouncer();
            var potentialConnectionStrings = new[] {"iBrokerConnectionString", "iCimsConnectionString"};

            foreach (
                var connectionString in
                    potentialConnectionStrings.Where(connectionString => ConfigurationManager.ConnectionStrings[connectionString] != null))
            {
                if (string.IsNullOrEmpty(connectionString))
                    throw new Exception(
                        string.Format("Could not find a valid connection string to run the migrations against"));

                var factory = new SqlServer2008ProcessorFactory();
                var processor =
                    factory.Create(ConfigurationManager.ConnectionStrings[connectionString].ConnectionString,
                        nullAnnouncer,
                        new ProcessorOptions());

                var context = new RunnerContext(nullAnnouncer)
                {
                    Profile = "Development"
                };

                var runner = new MigrationRunner(migrationAssembly, context, processor);

                yield return runner;
            }
        }
Example #6
0
        /// <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();
            }
        }
 private MigrationRunner MigrationRunner()
 {
     IAnnouncer announcer = new NullAnnouncer();
     IRunnerContext context = new RunnerContext(announcer);
     IMigrationProcessorFactory processorFactory = new SqlServer2008ProcessorFactory();
     IMigrationProcessorOptions options = new ProcessorOptions { PreviewOnly = false, Timeout = 0 };
     IMigrationProcessor processor = processorFactory.Create(DatabaseConnectionString, announcer, options);
     return new MigrationRunner(Assembly.GetExecutingAssembly(), context, processor);
 }
        protected virtual IMigrationProcessor GetMigrationProcessor(string connectionString, IAnnouncer announcer)
        {
            var sqlProcessor = new SqlServer2008ProcessorFactory().Create(connectionString, announcer, options);

            return sqlProcessor;
        }