Ejemplo n.º 1
0
 public static void GenerateDb()
 {
     if (ConfigurationHelper.GetAppSettingOrDefault("单元测试-生成数据库", false))
     {
         using (var c = new RafyDbMigrationContext(ConnectionStringNames.DbMigrationHistory))
         {
             c.RunDataLossOperation = DataLossOperation.All;
             c.AutoMigrate();
         }
         using (var c = new RafyDbMigrationContext(ConnectionStringNames.RafyPlugins))
         {
             c.HistoryRepository = new DbHistoryRepository();
             c.RunDataLossOperation = DataLossOperation.All;
             c.AutoMigrate();
         }
         using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName))
         {
             c.HistoryRepository = new DbHistoryRepository();
             c.RunDataLossOperation = DataLossOperation.All;
             c.AutoMigrate();
         }
         using (var c = new RafyDbMigrationContext(UnitTest2EntityRepositoryDataProvider.DbSettingName))
         {
             c.HistoryRepository = new DbHistoryRepository();
             c.RunDataLossOperation = DataLossOperation.All;
             c.AutoMigrate();
         }
         using (var c = new RafyDbMigrationContext(StringTestEntityDataProvider.DbSettingName))
         {
             c.HistoryRepository = new DbHistoryRepository();
             c.RunDataLossOperation = DataLossOperation.All;
             c.AutoMigrate();
         }
     }
 }
Ejemplo n.º 2
0
        public static void DbMigrationTest_ClassInitialize(TestContext context)
        {
            ServerTestHelper.ClassInitialize(context);

            //运行测试前,这个库升级到最新的内容,同时它的历史记录需要清空
            using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName))
            {
                c.HistoryRepository = new DbHistoryRepository();
                c.RunDataLossOperation = DataLossOperation.All;
                //c.DeleteDatabase();

                c.AutoMigrate();

                c.ResetDbVersion();
                c.ResetHistory();
            };
        }
Ejemplo n.º 3
0
        public static void GenerateDb()
        {
            if (ConfigurationHelper.GetAppSettingOrDefault("单元测试-生成数据库", false))
            {
                using (var c = new RafyDbMigrationContext(ConnectionStringNames.DbMigrationHistory))
                {
                    c.RunDataLossOperation = DataLossOperation.All;
                    c.AutoMigrate();
                }
                using (var c = new RafyDbMigrationContext(ConnectionStringNames.RafyPlugins))
                {
                    c.HistoryRepository = new DbHistoryRepository();
                    c.RunDataLossOperation = DataLossOperation.All;
                    c.AutoMigrate();
                }
                using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName))
                {
                    c.HistoryRepository = new DbHistoryRepository();
                    c.RunDataLossOperation = DataLossOperation.All;
                    c.AutoMigrate();
                }
                using (var c = new RafyDbMigrationContext(UnitTest2EntityRepositoryDataProvider.DbSettingName))
                {
                    c.HistoryRepository = new DbHistoryRepository();
                    c.RunDataLossOperation = DataLossOperation.All;
                    c.AutoMigrate();
                }
                using (var c = new RafyDbMigrationContext(StringTestEntityDataProvider.DbSettingName))
                {
                    c.HistoryRepository = new DbHistoryRepository();
                    c.RunDataLossOperation = DataLossOperation.All;
                    c.AutoMigrate();
                }

                //启用批量更新
                var dbSetting = DbSetting.FindOrCreate(UnitTestEntityRepositoryDataProvider.DbSettingName);
                if (DbSetting.IsOracleProvider(dbSetting))
                {
                    OracleBatchImporter.EnableBatchSequence(RF.Concrete<BookRepository>());
                }
            }
        }
Ejemplo n.º 4
0
        protected override void Execute()
        {
            var migrateList = this.Options;

            if (migrateList.Databases.Length > 0)
            {
                if (Options.ReserveHistory)
                {
                    using (var c = new RafyDbMigrationContext(ConnectionStringNames.DbMigrationHistory))
                    {
                        c.RunDataLossOperation = DataLossOperation.All;

                        c.AutoMigrate();
                    }
                }

                foreach (var config in migrateList.Databases)
                {
                    using (var c = new RafyDbMigrationContext(config))
                    {
                        if (Options.ReserveHistory)
                        {
                            c.HistoryRepository = new DbHistoryRepository();
                        }

                        //    c.RollbackAll();
                        //    c.ResetHistory();
                        //    c.ResetDbVersion();
                        if (Options.IgnoreTables != null)
                        {
                            c.ClassMetaReader.IgnoreTables.AddRange(Options.IgnoreTables);
                        }

                        c.RunDataLossOperation = migrateList.RunDataLossOperation;
                        c.AutoMigrate();
                    }
                }
            }
        }
Ejemplo n.º 5
0
        protected override void Execute()
        {
            var migrateList = this.Options;
            if (migrateList.Databases.Length > 0)
            {
                if (Options.ReserveHistory)
                {
                    using (var c = new RafyDbMigrationContext(ConnectionStringNames.DbMigrationHistory))
                    {
                        c.RunDataLossOperation = DataLossOperation.All;

                        c.AutoMigrate();
                    }
                }

                foreach (var config in migrateList.Databases)
                {
                    using (var c = new RafyDbMigrationContext(config))
                    {
                        if (Options.ReserveHistory)
                        {
                            c.HistoryRepository = new DbHistoryRepository();
                        }

                        //    c.RollbackAll();
                        //    c.ResetHistory();
                        //    c.ResetDbVersion();
                        if (Options.IgnoreTables != null)
                        {
                            c.ClassMetaReader.IgnoreTables.AddRange(Options.IgnoreTables);
                        }

                        c.RunDataLossOperation = migrateList.RunDataLossOperation;
                        c.AutoMigrate();
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private void Test(Action<Database> action, Action<Database> assertAction)
        {
            using (var context = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName))
            {
                context.HistoryRepository = new DbHistoryRepository();
                context.RunDataLossOperation = DataLossOperation.All;

                var destination = context.ClassMetaReader.Read();
                action(destination);

                try
                {
                    context.MigrateTo(destination);

                    var result = context.DatabaseMetaReader.Read();
                    assertAction(result);
                }
                finally
                {
                    context.RollbackAll(RollbackAction.DeleteHistory);
                }
            }
        }
Ejemplo n.º 7
0
        public void DMT_RollbackAll()
        {
            using (var context = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName))
            {
                context.HistoryRepository = new DbHistoryRepository();
                context.RunDataLossOperation = DataLossOperation.All;

                context.RollbackAll(RollbackAction.DeleteHistory);

                var histories = context.GetHistories();

                Assert.IsTrue(histories.Count == 0);

                Assert.IsTrue(context.HasNoHistory());
            }
        }
Ejemplo n.º 8
0
        public void DMT_ManualMigrate()
        {
            using (var context = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName))
            {
                context.HistoryRepository = new DbHistoryRepository();
                context.RunDataLossOperation = DataLossOperation.All;

                try
                {
                    context.ManualMigrations.Clear();
                    context.ManualMigrations.Add(new DMT_ManualMigrateEntity());
                    context.ManualMigrations.Add(new DMT_ManualMigrateTest());

                    //手工更新
                    context.MigrateManually();

                    //历史记录
                    var histories = context.GetHistories();
                    Assert.IsTrue(histories.Count == 2);
                    Assert.IsTrue(histories[0] is DMT_ManualMigrateTest);
                    Assert.IsTrue(histories[1] is DMT_ManualMigrateEntity);

                    //数据库结构
                    var database = context.DatabaseMetaReader.Read();
                    var table = database.FindTable("TestingTable");
                    Assert.IsTrue(table.Columns.Count == 2);
                    var pk = table.FindPrimaryColumn();
                    Assert.IsTrue(pk.Name.EqualsIgnoreCase("Id"));
                    Assert.IsTrue(pk.DataType == DbType.Int32);

                    //数据库数据
                    using (var db = new DbAccesser(UnitTestEntityRepositoryDataProvider.DbSettingName))
                    {
                        var rows = db.QueryDataTable("select * from TestingTable");
                        Assert.IsTrue(rows.Rows.Count == 2);
                    }
                    var repo = RF.Find<TestUser>();
                    Assert.IsTrue(repo.CountAll() == 10);
                }
                finally
                {
                    //回滚
                    context.RollbackAll(RollbackAction.DeleteHistory);

                    var database = context.DatabaseMetaReader.Read();
                    Assert.IsTrue(database.FindTable("TestingTable") == null);
                }
            }
        }
Ejemplo n.º 9
0
        public void DMT_DataLoss_DropTable()
        {
            using (var context = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName))
            {
                context.HistoryRepository = new DbHistoryRepository();

                var destination = context.ClassMetaReader.Read();
                var taskTable = destination.FindTable("Task");
                destination.Tables.Remove(taskTable);

                try
                {
                    context.MigrateTo(destination);

                    var result = context.DatabaseMetaReader.Read();
                    var resultTable = result.FindTable("Task");
                    Assert.IsTrue(resultTable != null);
                }
                finally
                {
                    context.RollbackAll(RollbackAction.DeleteHistory);
                }
            }
        }
Ejemplo n.º 10
0
 public void DMT_AutoMigrate()
 {
     using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName))
     {
         c.HistoryRepository = new DbHistoryRepository();
         c.RunDataLossOperation = DataLossOperation.All;
         c.AutoMigrate();
     }
 }
Ejemplo n.º 11
0
 public void ORM_DbMigrate_Column_Decimal_SpecifyLength()
 {
     using (var context = new RafyDbMigrationContext(UnitTest2EntityRepositoryDataProvider.DbSettingName))
     {
         var db = context.DatabaseMetaReader.Read();
         var table = db.FindTable("Customer");
         var c1 = table.FindColumn("DecimalProperty2");
         Assert.IsTrue(DbTypeHelper.IsCompatible(c1.DataType, DbType.Decimal));
         //Assert.IsTrue(c1.Length == "18,4");
     }
 }
Ejemplo n.º 12
0
        /// <summary>
        /// 在进行条中执行指定数据库的升级操作。
        /// </summary>
        /// <param name="dbSetting"></param>
        /// <param name="action"></param>
        private static void MigrateInProgressBar(string dbSetting, Action<RafyDbMigrationContext> action)
        {
            using (var c = new RafyDbMigrationContext(dbSetting))
            {
                c.RunDataLossOperation = DataLossOperation.All;

                if (!RafyDbMigrationContext.IsEnabled())
                {
                    action(c);

                    //c.DeleteDatabase();
                    //c.AutoMigrate();

                    //其它一些可用的API
                    //c.ClassMetaReader.IgnoreTables.Add("ReportObjectMetaData");
                    //c.RollbackToHistory(DateTime.Parse("2008-12-31 23:59:58.700"), RollbackAction.DeleteHistory);
                    //c.DeleteDatabase();
                    //c.ResetHistories();
                    //c.RollbackAll();
                    //c.JumpToHistory(DateTime.Parse("2012-01-07 21:27:00.000"));
                }
                else
                {
                    var win = new WaitDialog();
                    win.Width = 500;
                    win.Opacity = 0;
                    win.ShowInTaskbar = false;
                    win.Text = string.Format("正在生成 {0} 数据库,请稍侯……", dbSetting);

                    Exception exception = null;

                    ThreadPool.QueueUserWorkItem(oo =>
                    {
                        try
                        {
                            bool first = false;
                            c.ItemMigrated += (o, e) =>
                            {
                                if (!first)
                                {
                                    Action setVisible = () => win.Opacity = 1;
                                    win.Dispatcher.Invoke(setVisible);
                                    first = true;
                                }
                                win.ProgressValue = new ProgressValue
                                {
                                    Percent = 100 * e.Index / (double)e.TotalCount
                                };
                            };

                            action(c);
                        }
                        catch (Exception ex)
                        {
                            exception = ex;
                        }

                        Action ac = () => win.DialogResult = true;
                        win.Dispatcher.BeginInvoke(ac);
                    });

                    win.ShowDialog();

                    if (exception != null) { throw new Rafy.DbMigration.DbMigrationException("数据库升级时出错,请查看 InnerException。", exception); }
                }
            }
        }
Ejemplo n.º 13
0
 public void ORM_DbMigrate_Column_Decimal_MaptoDouble()
 {
     using (var context = new RafyDbMigrationContext(UnitTest2EntityRepositoryDataProvider.DbSettingName))
     {
         var db = context.DatabaseMetaReader.Read();
         var table = db.FindTable("Customer");
         var c1 = table.FindColumn("DecimalProperty3");
         Assert.IsTrue(c1.DataType == DbType.Double);
     }
 }