android怎么迁移数据库,如何在Android中测试数据库迁移?

房是Android架构组件数据库的东西之一。

房提供了一个测试Maven构件,以协助这个测试过程。

首先你需要通过设置room.schemaLocation注解处理器性能在build.gradle文件

android {

...

defaultConfig {

...

javaCompileOptions {

annotationProcessorOptions {

arguments = ["room.schemaLocation":

"$projectDir/schemas".toString()]

}

}

}

}

@RunWith(AndroidJUnit4.class)

public class MigrationTest {

private static final String TEST_DB = "migration-test";

@Rule

public MigrationTestHelper helper;

public MigrationTest() {

helper = new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(),

MigrationDb.class.getCanonicalName(),

new FrameworkSQLiteOpenHelperFactory());

}

@Test

public void migrate1To2() throws IOException {

SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1);

// db has schema version 1. insert some data using SQL queries.

// You cannot use DAO classes because they expect the latest schema.

db.execSQL(...);

// Prepare for the next version.

db.close();

// Re-open the database with version 2 and provide

// MIGRATION_1_2 as the migration process.

db = helper.runMigrationsAndValidate(TEST_DB, 2, true, MIGRATION_1_2);

// MigrationTestHelper automatically verifies the schema changes,

// but you need to validate that the data was migrated properly.

}

}

出口分贝方案您可以阅读的正式文件for more info

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值