public void MigrateDatabase() { // See http://stackoverflow.com/questions/7574417/is-it-possible-to-use-fluent-migrator-in-application-start // var announcer = new NullAnnouncer(); var announcer = new FluentMigrator.Runner.Announcers.TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s)); var assembly = Assembly.GetExecutingAssembly(); var migrationContext = new FluentMigrator.Runner.Initialization.RunnerContext(announcer) { Namespace = "Majorsilence.Vpn.Logic.Migrations", TransactionPerSession = true }; var options = new FluentMigrator.Runner.Processors.ProcessorOptions() { PreviewOnly = false, // set to true to see the SQL Timeout = TimeSpan.FromSeconds(60) }; var factory = new FluentMigrator.Runner.Processors.MySql.MySql5ProcessorFactory(); var processor = factory.Create(InitializeSettings.DbFactory.ConnectionString, announcer, options); var runner = new FluentMigrator.Runner.MigrationRunner(assembly, migrationContext, processor); runner.MigrateUp(true); }
private void btnMigrationUp_Click(object sender, EventArgs e) { ReloadSettings(); if (InvalidateSettings()) { return; } txtOutput.Text = string.Empty; var announcer = new FluentMigrator.Runner.Announcers.TextWriterAnnouncer(s => txtOutput.Text += s); var assembly = Assembly.LoadFile(_assemblyPath); var migrationContext = new FluentMigrator.Runner.Initialization.RunnerContext(announcer) { Namespace = assembly.GetTypes().First(a => a.Name.ToLower().StartsWith("step")).Namespace, TransactionPerSession = false, ApplicationContext = "sqlserver", Targets = new[] { _assemblyPath }, }; var options = new FluentMigrator.Runner.Processors.ProcessorOptions() { PreviewOnly = false, Timeout = new TimeSpan(0, 0, 600) }; var connectionString = $"Data Source={_server};User ID={_user};Password={_password};Database={_database};"; var factory = new FluentMigrator.Runner.Processors.SqlServer.SqlServerProcessorFactory(); try { var task = Task.Factory.StartNew(() => { using (var processor = factory.Create(connectionString, announcer, options)) { this.BeginInvoke(new Action(() => { btnMigrationUp.Text = @"Running.."; this.Enable(false); txtOutput.Enabled = true; })); var runner = new FluentMigrator.Runner.MigrationRunner(assembly, migrationContext, processor); runner.MigrateUp(true); } }); task.ContinueWith((success) => { this.BeginInvoke(new Action(() => { btnMigrationUp.Text = @"Migration Up"; this.Enable(true); MessageBox.Show(@"Database Migrate succeeded!!", @"Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); txtOutput.Text += @"Succeeded!!"; })); }, TaskContinuationOptions.NotOnFaulted); task.ContinueWith((fault) => { this.BeginInvoke(new Action(() => { btnMigrationUp.Text = @"Migration Up"; this.Enable(true); MessageBox.Show($@"Database Migrate failed with {fault?.Exception?.Message}!!", @"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); txtOutput.Text += $@"Failed!! {Environment.NewLine} {fault?.Exception?.Message} {Environment.NewLine} {fault?.Exception?.InnerException?.Message}"; })); }, TaskContinuationOptions.OnlyOnFaulted); } catch (Exception exception) { MessageBox.Show(exception.Message, @"Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } }