Example #1
0
        /// <summary>
        /// Upgrades the database to the latest version
        /// </summary>
        /// <param name="processorFactory"></param>
        /// <param name="connectionString"></param>
        public static void MigrateToLatest(
            MigrationProcessorFactory processorFactory,
            string connectionString)
        {
            var announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s));

            var migrationContext = new RunnerContext(announcer)
            {
                Targets = new[]
                {
                    nameof(Migrator)
                }
            };

            // Since migrations might take long increase the command timeout for this. The timeout is in seconds
            var options = new MigrationProcessorOptions {
                PreviewOnly = false, Timeout = 60 * 30
            };                                                                                      // 30 minutes

            using (var processor = processorFactory.Create(connectionString, announcer, options))
            {
                var runner = new MigrationRunner(Assembly.GetAssembly(typeof(Migrator)), migrationContext, processor);
                runner.MigrateUp(true);
            }
        }
Example #2
0
        private IMigrationProcessor GetMigrationProcessor()
        {
            var connectionString = _databaseConfig.GetConnectionString();
            var options          = new Migrator.MigrationOptions {
                PreviewOnly = false, Timeout = 60
            };

            return(MigrationProcessorFactory.Create(connectionString, Announcer, options));
        }
Example #3
0
        // Runs all migrations in the assembly of specified type
        public static void RunMigrations(string connectionString, MigrationsAssembly migrationsAssembly, MigrationProcessorFactory factory = null)
        {
            try
            {
                var announcer = new NullAnnouncer();
                //var announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s));
                var assembly = migrationsAssembly.Assembly;

                var migrationContext = new RunnerContext(announcer);

                var options = new MigrationOptions {
                    PreviewOnly = false, Timeout = 60
                };

                var processor = (factory ?? new SqlServerProcessorFactory()).Create(connectionString, announcer, options);
                var runner    = new MigrationRunner(assembly, migrationContext, processor);
                runner.MigrateUp(useAutomaticTransactionManagement: true);

                processor.Dispose();
            }
            catch (Exception ex)
            {
                var message = string.Format("Failed to run migrations for {0}", connectionString);
                throw new Exception(message, ex);
            }
        }