当前位置:网站首页>Room database migration
Room database migration
2022-07-25 07:20:00 【Programmer Xiao He SS】
Preface
Be careful : This article is advanced Room Part of a series , This series covers Room All details of the persistence Library .
Database migration is a very important concept in any application development . When we add and change functions in your application , We must update the database architecture . Whenever the architecture of any of our tables changes , If we don't want our users to lose all existing data , We need to write migrations for existing applications .
for example , We can consider a table called users, It contains user information , It has 3 Columns uid, Namely first_name and last_name. Now? , If we Add a new column age, We need to write a migration to change the current table schema —— namely , Add a name The new column of age.
Room The persistence library allows us to write classes in this way Migration To save user data . Every Migration Class specifies a startVersion and endVersion. At run time ,Room Run each Migration Class migrate() Method , Migrate the database to a later version in the correct order .
val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE users ADD COLUMN age INTEGER")
}
}
Now we have created the migration object MIGRATION_1_2, We need to use the database builder to add it to the database configuration .
Room.databaseBuilder(
applicationContext,
UserDatabase::class.java,
"users-db"
).addMigrations(MIGRATION_1_2)
.build()
Gracefully handle missing migration paths
After updating the schema of our database , Databases on some devices may still use older schema versions . If Room Cannot find the migration rule to upgrade the database of the device from the old version to the current version , It throws out one IllegalStateException The error that caused our application to crash .
To prevent the application from crashing when this happens , please fallbackToDestructiveMigration() Call... When creating the database builder Method .
Room.databaseBuilder(
applicationContext,
UserDatabase::class.java,
"users-db"
).fallbackToDestructiveMigration()
.build()
Destructive entertainment fallback logic includes several additional options :
- If there is an error in the schema history of a specific version that cannot be solved through the migration path , Please use fallbackToDestructiveMigrationFrom(). This method indicates that you want Room Use fallback logic only if the database attempts to migrate from one of these problematic versions .
- To perform destructive reconstruction only when attempting mode degradation , please fallbackToDestructiveMigrationOnDowngrade() change to the use of sth. .
This is the use of Room How to complete the migration .
边栏推荐
- Ask the bosses: MySQL CDC stores configuration data, and Kafka has history
- 【obs】视频包发送的dts_usec 计算
- 【程序员2公务员】三、资源搜集
- QT学习日记20——飞机大战项目
- LeetCode118. 杨辉三角
- GBase 8a中关于--skip-networking 问题
- 集群聊天服务器:项目问题汇总
- knapsack problem
- [semidrive source code analysis] [drive bringup] 38 - norflash & EMMC partition configuration
- Wechat applet switchtab transmit parameters and receive parameters
猜你喜欢

北京内推 | 微软STCA招聘NLP/IR/DL方向研究型实习生(可远程)

Day by day, month by month | Shenzhen potential technology released the extreme accelerated version of molecular docking engine uni docking

Lidar construction map (overlay grid construction map)

leetcode刷题:动态规划06(整数拆分)

list的模拟实现

曼哈顿距离简介

BOM overview

diagramscene工程难点分析

"Wei Lai Cup" 2022 Niuke summer multi school training camp 1 supplementary problem solution (incomplete)

Openatom xuprechain open source biweekly report | 2022.7.11-2022.7.22
随机推荐
First, how about qifujin
What if Oracle 19C migration encounters large lob tables?
列表推导式
How to use network installation to deploy multiple virtual servers in KVM environment
北京内推 | 微软STCA招聘NLP/IR/DL方向研究型实习生(可远程)
Servlet常用类剖析
A domestic open source redis visualization tool that is super easy to use, with a high-value UI, which is really fragrant!!
The relationship between Informatics, mathematics and Mathematical Olympiad (July 19, 2022) C
[programmer 2 Civil Servant] summary of some common problems about system research
【电脑讲解】去电脑维修店修电脑需要注意什么?
数据提交类型 Request Payload 与 Form Data 的区别总结
Luo min's backwater battle in qudian
Security compliance, non-stop discounts! High quality travel service, "enjoy the road" for you
New functions of shixizhi are online. These new functions are online in June. Can you use them?
从ACL 2022 Onsite经历看NLP热点
MATLAB自编程系列(1)---角分布函数
【terminal】x86 Native Tools Command Prompt for VS 2017
华为无线设备配置WPA2-802.1X-AES安全策略
Incremental crawler in distributed crawler
【刷题笔记】搜索旋转排序数组