public void DMT_DataLoss_DropColumn() { using (var context = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName)) { context.HistoryRepository = new DbHistoryRepository(); var destination = context.ClassMetaReader.Read(); var taskTable = destination.FindTable("Task"); taskTable.Columns.Remove(taskTable.FindColumn("AllTime")); try { context.MigrateTo(destination); var result = context.DatabaseMetaReader.Read(); var resultTable = result.FindTable("Task"); var column = resultTable.FindColumn("AllTime"); Assert.IsTrue(column != null); } finally { context.RollbackAll(RollbackAction.DeleteHistory); } } }
public static void DbMigrationTest_ClassInitialize(TestContext context) { ServerTestHelper.ClassInitialize(context); //运行测试前,这个库升级到最新的内容,同时它的历史记录需要清空 using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName)) { c.RunDataLossOperation = DataLossOperation.All; if (IsTestDbSQLite()) { c.DeleteAllTables(); } else { c.HistoryRepository = new DbHistoryRepository(); } c.AutoMigrate(); c.ResetDbVersion(); if (!IsTestDbSQLite()) { c.ResetHistory(); } var dbProvider = DbMigrationProviderFactory.GetProvider(c.DbSetting); dbTypeConverter = (dbProvider.CreateRunGenerator() as SqlRunGenerator).DbTypeCoverter; }; }
public void DMDBT_DeleteAllTables() { using (var context = new RafyDbMigrationContext("Test_TestingDataBase")) { context.RunDataLossOperation = DataLossOperation.All; if (context.DbSetting.ProviderName != DbSetting.Provider_SQLite) { context.HistoryRepository = new DbHistoryRepository(); } if (context.DatabaseExists() && !(context.DbVersionProvider is EmbadedDbVersionProvider)) { context.DeleteAllTables(); //数据库结构 var db = context.DatabaseMetaReader.Read(); Assert.AreEqual(2, db.Tables.Count); } if (context.SupportHistory) { context.ResetHistory(); } else { context.DeleteAllTables(); context.AutoMigrate(); } } }
public void DMDBT_CreateDatabase() { using (var context = new RafyDbMigrationContext("Test_TestingDataBase")) { context.RunDataLossOperation = DataLossOperation.All; if (context.DbSetting.ProviderName != DbSetting.Provider_SQLite) { context.HistoryRepository = new DbHistoryRepository(); } if (!context.DatabaseExists()) { var destination = new DestinationDatabase("Test_TestingDataBase"); var tmpTable = new Table("TestingTable", destination); tmpTable.AddColumn("Id", DbType.Int32, isPrimaryKey: true); tmpTable.AddColumn("Name", DbType.String); destination.Tables.Add(tmpTable); context.MigrateTo(destination); //历史记录 if (context.SupportHistory) { var histories = context.GetHistories(); Assert.IsTrue(histories.Count == 3); Assert.IsTrue(histories[2] is CreateDatabase); } //数据库结构 Assert.IsTrue(context.DatabaseExists()); } } }
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(); } } }
public void DMT_DataLoss_DropTable() { using (var context = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName)) { if (!IsTestDbSQLite()) { 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 { if (context.SupportHistory) { context.RollbackAll(RollbackAction.DeleteHistory); } else { context.DeleteAllTables(); context.AutoMigrate(); } } } }
public static void DropAllTables() { using (var c = new RafyDbMigrationContext(DbSettingNames.DbMigrationHistory)) { DropTables(c); } using (var c = new RafyDbMigrationContext(DbSettingNames.RafyPlugins)) { DropTables(c); } using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName)) { DropTables(c); } using (RdbDataProvider.RedirectDbSetting( UnitTestEntityRepositoryDataProvider.DbSettingName, UnitTestEntityRepositoryDataProvider.DbSettingName_Duplicate )) { using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName_Duplicate)) { c.ClassMetaReader.EntityDbSettingName = UnitTestEntityRepositoryDataProvider.DbSettingName; c.ClassMetaReader.IsGeneratingForeignKey = false; DropTables(c); } } using (var c = new RafyDbMigrationContext(UnitTest2EntityRepositoryDataProvider.DbSettingName)) { DropTables(c); } using (var c = new RafyDbMigrationContext(StringTestEntityDataProvider.DbSettingName)) { DropTables(c); } }
public static void GenerateDb() { if (ConfigurationHelper.GetAppSettingOrDefault("单元测试-生成数据库", false)) { if (ClearDb && ConfigurationHelper.GetAppSettingOrDefault("单元测试-生成数据库-先清空数据库", false)) { //不想手工去删除数据库,可以使用下面这个方法来在程序中删除所有的表。 DropAllTables(); ClearDb = false; } using (var c = new RafyDbMigrationContext(DbSettingNames.DbMigrationHistory)) { c.RunDataLossOperation = DataLossOperation.All; c.AutoMigrate(); } using (var c = new RafyDbMigrationContext(DbSettingNames.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(); } UnitTestPlugin.InitailizeSequences(); using (RdbDataProvider.RedirectDbSetting( UnitTestEntityRepositoryDataProvider.DbSettingName, UnitTestEntityRepositoryDataProvider.DbSettingName_Duplicate )) { using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName_Duplicate)) { c.ClassMetaReader.EntityDbSettingName = UnitTestEntityRepositoryDataProvider.DbSettingName; c.ClassMetaReader.IsGeneratingForeignKey = false; c.HistoryRepository = new DbHistoryRepository(); c.RunDataLossOperation = DataLossOperation.All; c.AutoMigrate(); } UnitTestPlugin.InitailizeSequences(); } 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(); } } }
public void DMT_RefreshComments() { using (var context = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName)) { context.RefreshComments(); if (context.DbSetting.ProviderName.Contains("MySql")) { //数据库数据 using (var db = DbAccesserFactory.Create(context.DbSetting)) { Func <string, DataRow[]> queryComments = tableName => { var table = db.QueryDataTable(@"show full columns from `" + tableName + "`"); return(table.Rows.Cast <DataRow>().ToArray()); }; var rows = queryComments("ARTICLE"); Assert.IsTrue(rows.Any(r => r["Field"].ToString() == "Id"), "实体的标识属性。"); Assert.IsTrue(rows.Any(r => r["Field"].ToString() == "AdministratorId"), "文章的管理员"); Assert.IsTrue(rows.Any(r => r["Field"].ToString() == "CreatedTime"), "实体的创建时间。"); rows = queryComments("Roles"); var roleTypeDesc = rows.FirstOrDefault(r => r["Field"].ToString() == "RoleType"); Assert.IsNotNull(roleTypeDesc, "枚举属性必须有注释。"); var comment = roleTypeDesc["Comment"].ToString(); Assert.AreEqual(comment, @"角色的类型 0:(Normal, 一般) 1:(Administrator, 管理员)"); } } else if (!DbSetting.IsOracleProvider(context.DbSetting)) { //数据库数据 using (var db = DbAccesserFactory.Create(context.DbSetting)) { Func <string, DataRow[]> queryComments = tableName => { var table = db.QueryDataTable(@"select t.name tableName, c.name columnName, p.Value Comment from sys.all_columns c join sys.tables t on c.object_id = t.object_id join sys.extended_properties p on p.major_id = c.object_id and p.minor_id = c.column_id where t.name = '" + tableName + "'"); return(table.Rows.Cast <DataRow>().ToArray()); }; var rows = queryComments("ARTICLE"); Assert.IsTrue(rows.Any(r => r["columnName"].ToString() == "Id"), "主键必须有注释。"); Assert.IsTrue(rows.Any(r => r["columnName"].ToString() == "AdministratorId"), "外键必须有注释。"); Assert.IsTrue(rows.Any(r => r["columnName"].ToString() == "CreatedTime"), "扩展属性必须有注释。"); rows = queryComments("Roles"); var roleTypeDesc = rows.FirstOrDefault(r => r["columnName"].ToString() == "RoleType"); Assert.IsNotNull(roleTypeDesc, "枚举属性必须有注释。"); var comment = roleTypeDesc["Comment"].ToString(); Assert.AreEqual(comment, @"角色的类型 0:(Normal, 一般) 1:(Administrator, 管理员)"); //WF_ 开头的动态属性。 } } } }
public void DMT_AutoMigrate() { using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName)) { c.HistoryRepository = new DbHistoryRepository(); c.RunDataLossOperation = DataLossOperation.All; c.AutoMigrate(); } }
public static void GenerateDb() { if (ConfigurationHelper.GetAppSettingOrDefault("单元测试-生成数据库", false)) { using (var c = new RafyDbMigrationContext(DbSettingNames.DbMigrationHistory)) { c.RunDataLossOperation = DataLossOperation.All; c.AutoMigrate(); } using (var c = new RafyDbMigrationContext(DbSettingNames.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 (RdbDataProvider.RedirectDbSetting(UnitTestEntityRepositoryDataProvider.DbSettingName, UnitTestEntityRepositoryDataProvider.DbSettingName_Duplicate)) { using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName_Duplicate)) { c.ClassMetaReader.EntityDbSettingName = UnitTestEntityRepositoryDataProvider.DbSettingName; c.ClassMetaReader.IsGeneratingForeignKey = false; 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.ResolveInstance <BookRepository>()); OracleBatchImporter.EnableBatchSequence(RF.ResolveInstance <InvoiceRepository>()); } } }
/// <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; 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); } } }
public static void GenerateDb() { if (ConfigurationHelper.GetAppSettingOrDefault("单元测试-生成数据库", false)) { using (var c = new RafyDbMigrationContext(DbSettingNames.DbMigrationHistory)) { c.RunDataLossOperation = DataLossOperation.All; c.AutoMigrate(); } using (var c = new RafyDbMigrationContext(DbSettingNames.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 (RdbDataProvider.RedirectDbSetting( UnitTestEntityRepositoryDataProvider.DbSettingName, UnitTestEntityRepositoryDataProvider.DbSettingName_Duplicate )) { using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName_Duplicate)) { c.ClassMetaReader.EntityDbSettingName = UnitTestEntityRepositoryDataProvider.DbSettingName; c.ClassMetaReader.IsGeneratingForeignKey = false; 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(); } } }
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.DbType == DbType.Int32); //数据库数据 using (var db = DbAccesserFactory.Create(context.DbSetting)) { 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); } } }
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()); } }
public void DMT_AutoMigrate_ChangeDbSetting() { using (RdbDataProvider.RedirectDbSetting(UnitTestEntityRepositoryDataProvider.DbSettingName, UnitTestEntityRepositoryDataProvider.DbSettingName_Duplicate)) { using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName_Duplicate)) { /// 实体元数据默认设置的连接字符串 /// 如果切换新的数据库需要设置,否则不用设置 c.ClassMetaReader.EntityDbSettingName = UnitTestEntityRepositoryDataProvider.DbSettingName; c.HistoryRepository = new DbHistoryRepository(); c.RunDataLossOperation = DataLossOperation.All; c.AutoMigrate(); } } }
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(); }; }
public void DMDBT_DropDatabase() { //以下代码不能运行,会提示数据库正在被使用 using (var context = new RafyDbMigrationContext("Test_TestingDataBase")) { context.HistoryRepository = new DbHistoryRepository(); context.RunDataLossOperation = DataLossOperation.All; if (context.DatabaseExists() && !(context.DbVersionProvider is EmbadedDbVersionProvider)) { //context.DeleteDatabase(); var database = new DestinationDatabase("Test_TestingDataBase") { Removed = true }; context.MigrateTo(database); //历史记录 var histories = context.GetHistories(); Assert.IsTrue(histories[0] is DropDatabase); //数据库结构 Assert.IsTrue(!context.DatabaseExists()); } context.ResetHistory(); } //using (var context = new RafyDbMigrationContext("Test_TestingDataBase")) //{ // context.ManualMigrations.Add(new DMDBT_DropDatabase_Migration()); // context.MigrateManually(); // //历史记录 // var histories = context.GetHistories(); // Assert.IsTrue(histories[0] is DMDBT_DropDatabase_Migration); // //数据库结构 // var database = context.DatabaseMetaReader.Read(); // Assert.IsTrue(database == null); // context.ResetHistory(); //} }
public void DMT_RefreshComments() { using (var context = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName)) { context.RefreshComments(); if (!DbSetting.IsOracleProvider(context.DbSetting)) { //数据库数据 using (var db = DbAccesserFactory.Create(context.DbSetting)) { var rowsCount = Convert.ToInt32(db.QueryValue( @"select COUNT(0) from sys.extended_properties p join sys.tables t on p.major_id = t.object_id where t.name = 'ARTICLE'")); Assert.IsTrue(rowsCount > 0); } } } }
public static void GenerateDb() { if (ConfigurationHelper.GetAppSettingOrDefault("单元测试-生成数据库", false)) { using (var c = new RafyDbMigrationContext(DbSettingNames.DbMigrationHistory)) { c.RunDataLossOperation = DataLossOperation.All; c.AutoMigrate(); } using (var c = new RafyDbMigrationContext(DbSettingNames.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>()); } } }
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); } } }
public void DMDBT_DropDatabase() { //以下代码不能运行,会提示数据库正在被使用 using (var context = new RafyDbMigrationContext("Test_TestingDataBase")) { context.RunDataLossOperation = DataLossOperation.All; if (context.DbSetting.ProviderName != DbSetting.Provider_SQLite) { context.HistoryRepository = new DbHistoryRepository(); } if (context.DatabaseExists() && !(context.DbVersionProvider is EmbadedDbVersionProvider)) { //context.DeleteDatabase(); var database = new DestinationDatabase("Test_TestingDataBase") { Removed = true }; context.MigrateTo(database); //历史记录 if (context.SupportHistory) { var histories = context.GetHistories(); Assert.IsTrue(histories[0] is DropDatabase); } //数据库结构 Assert.IsTrue(!context.DatabaseExists()); } if (context.SupportHistory) { context.ResetHistory(); } } }
/// <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); } } } }
public static void GenerateDb() { if (ConfigurationHelper.GetAppSettingOrDefault("Test_GenerateDb", false)) { //生成数据库时,为简单起见,需要先加载所有的插件。 //注意,此行代码会导致按需加载的单元测试全部通过。 //所以平时应该通过配置文件来关闭数据库生成功能。 RafyEnvironment.EnsureAllPluginsLoaded(); ForceAllPluginsLoaded = true; if (ClearDb && ConfigurationHelper.GetAppSettingOrDefault("Test_GenerateDb_Clear", false)) { //不想手工去删除数据库,可以使用下面这个方法来在程序中删除所有的表。 DropAllTables(); ClearDb = false; } using (var c = new RafyDbMigrationContext(DbSettingNames.DbMigrationHistory)) { c.RunDataLossOperation = DataLossOperation.All; c.AutoMigrate(); } using (var c = new RafyDbMigrationContext(DbSettingNames.RafyPlugins)) { if (c.DbSetting.ProviderName != DbSetting.Provider_SQLite) { c.HistoryRepository = new DbHistoryRepository(); } c.RunDataLossOperation = DataLossOperation.All; c.AutoMigrate(); } using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName)) { if (c.DbSetting.ProviderName != DbSetting.Provider_SQLite) { c.HistoryRepository = new DbHistoryRepository(); } c.RunDataLossOperation = DataLossOperation.All; c.AutoMigrate(); } UnitTestPlugin.InitailizeSequences(); using (RdbDataProvider.RedirectDbSetting( UnitTestEntityRepositoryDataProvider.DbSettingName, UnitTestEntityRepositoryDataProvider.DbSettingName_Duplicate )) { using (var c = new RafyDbMigrationContext(UnitTestEntityRepositoryDataProvider.DbSettingName_Duplicate)) { c.ClassMetaReader.EntityDbSettingName = UnitTestEntityRepositoryDataProvider.DbSettingName; c.ClassMetaReader.IsGeneratingForeignKey = false; if (c.DbSetting.ProviderName != DbSetting.Provider_SQLite) { c.HistoryRepository = new DbHistoryRepository(); } c.RunDataLossOperation = DataLossOperation.All; c.AutoMigrate(); } UnitTestPlugin.InitailizeSequences(); } using (var c = new RafyDbMigrationContext(UnitTest2EntityRepositoryDataProvider.DbSettingName)) { if (c.DbSetting.ProviderName != DbSetting.Provider_SQLite) { c.HistoryRepository = new DbHistoryRepository(); } c.RunDataLossOperation = DataLossOperation.All; c.AutoMigrate(); } using (var c = new RafyDbMigrationContext(StringTestEntityDataProvider.DbSettingName)) { if (c.DbSetting.ProviderName != DbSetting.Provider_SQLite) { c.HistoryRepository = new DbHistoryRepository(); } c.RunDataLossOperation = DataLossOperation.All; c.AutoMigrate(); } } }
private static void DropTables(RafyDbMigrationContext c) { c.RunDataLossOperation = DataLossOperation.All; c.MigrateTo(new DestinationDatabase(c.DbSetting.Name)); }