Exemple #1
0
        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);
        }
Exemple #2
0
        public void MigrateToLatest()
        {
            var providerMap = new[] {
                new { Name = "SqlServer", Provider = "System.Data.SqlClient" },
                new { Name = "MySql", Provider = "MySql.Data.MySqlClient" },
                new { Name = "SQLite", Provider = "System.Data.SQLite" },
                new { Name = "Postgres", Provider = "Npgsql" },
                new { Name = "Firebird", Provider = "FirebirdSql.Data.FirebirdClient" },
                new { Name = "SqlServerCe", Provider = "System.Data.SqlServerCe.4.0" },
                new { Name = "Jet", Provider = "Microsoft.Jet.OLEDB.4.0" },
                new { Name = "Hana", Provider = "Sap.Data.Hana" },
                new { Name = "Db2", Provider = "IBM.Data.DB2" },
            };

            // Unmapped names that FluentMigrator supports but the provider invariant name isn't known
            // DotConnectOracle,
            // new { Name = "Oracle", Provider = "System.Data.OracleClient" },
            // OracleManaged,
            // SqlServer2000,
            // SqlServer2005,
            // SqlServer2008,
            // SqlServer2012,
            // SqlServer2014,


            //ConnectionStringSettings css = ConfigurationManager.ConnectionStrings["DbLocal"];
            this.CreateDatabaseIfNeeded();

            string migrationFactoryName = providerMap.Single(item => item.Provider == this.dbLocal.ConnectionFactory.ProviderName).Name;

            var options   = new MigrationOptions();
            var announcer = new FluentMigrator.Runner.Announcers.TextWriterAnnouncer(s => Console.WriteLine(s));
            //var factory = new FluentMigrator.Runner.Processors.SqlServer.SqlServerProcessorFactory();
            var factory = new FluentMigrator.Runner.Processors.MigrationProcessorFactoryProvider().GetFactory(migrationFactoryName);
            var context = new FluentMigrator.Runner.Initialization.RunnerContext(announcer)
            {
                Namespace        = "MahloService.DbMigrations",
                NestedNamespaces = false,
            };

            var processor = factory.Create(this.dbLocal.ConnectionFactory.ConnectionString, announcer, options);
            var runner    = new FluentMigrator.Runner.MigrationRunner(Assembly.GetExecutingAssembly(), context, processor);

            runner.MigrateUp();
        }
 public static void MigrateToLatest(string connectionString)
 {
     // var announcer = new NullAnnouncer();
     var announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s));
     var assembly = Assembly.GetExecutingAssembly();
     
     var migrationContext = new RunnerContext(announcer)
     {
         Namespace = typeof(MigrationsRunner).Namespace + ".Migrations"
     };
     
     var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 };
     var factory = new FluentMigrator.Runner.Processors.SQLite.SQLiteProcessorFactory();
     using (var processor = factory.Create(connectionString, announcer, options))
     {
         var runner = new FluentMigrator.Runner.MigrationRunner(assembly, migrationContext, processor);
         runner.MigrateUp(true);
     }
 }
Exemple #4
0
        public static void MigrateToLatest(string connectionString)
        {
            // var announcer = new NullAnnouncer();
            var announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s));
            var assembly  = Assembly.GetExecutingAssembly();

            var migrationContext = new RunnerContext(announcer)
            {
                Namespace = typeof(MigrationsRunner).Namespace + ".Migrations"
            };

            var options = new MigrationOptions {
                PreviewOnly = false, Timeout = 60
            };
            var factory = new FluentMigrator.Runner.Processors.SQLite.SQLiteProcessorFactory();

            using (var processor = factory.Create(connectionString, announcer, options))
            {
                var runner = new FluentMigrator.Runner.MigrationRunner(assembly, migrationContext, processor);
                runner.MigrateUp(true);
            }
        }
Exemple #5
0
        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);
            }
        }