1.如果新加了一张数据表则什么都不用干直接database那里将数据库版本升1 就可以nichuang
在entities里增加新加的entity ProviderMeta.DB_VERSION 版本号增1
room会自动生成 一个ProviderMeta.DB_VERSION 版本号的json文件 比如实例中升级到70 就会生成一个70.json的文件这是room自动生成创建表的
2.原有数据表里增加了一个字段比如最后这行的devicetest
需要做的是 MIGRATION_68_69 写一个alertcolumn的sql exec
3.原有数据表如果是改变某个字段的名字 这时候需要复制老表的数据然后插入到新表 然后改变 databse版本号
package com.voicevision.client.database.migrations
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.voicevision.client.database.migrations.DatabaseMigrationUtil.KEYWORD_NOT_NULL
import com.voicevision.client.database.migrations.DatabaseMigrationUtil.TYPE_INTEGER
import com.voicevision.client.database.migrations.DatabaseMigrationUtil.TYPE_TEXT
/**
* Migration from version 67 to 68.
*
* This migration makes the local_id column NOT NULL, with -1 as a default value.
*/
@Suppress("MagicNumber")
class Migration67to68 : Migration(67, 68) {
override fun migrate(database: SupportSQLiteDatabase) {
val tableName = "filelist"
val newTableTempName = "${tableName}_new"
val newColumns = mapOf(
"_id" to DatabaseMigrationUtil.TYPE_INTEGER_PRIMARY_KEY,
"filename" to TYPE_TEXT,
、
)
DatabaseMigrationUtil.migrateTable(database, "filelist", newColumns) { columnName ->
when (columnName) {
"local_id" -> "IFNULL(local_id, -1)"
else -> columnName
}
}
}
}
package com.voicevision.client.database.migrations
import androidx.sqlite.db.SupportSQLiteDatabase
object DatabaseMigrationUtil {
const val TYPE_TEXT = "TEXT"
const val TYPE_INTEGER = "INTEGER"
const val TYPE_INTEGER_PRIMARY_KEY = "INTEGER PRIMARY KEY"
const val KEYWORD_NOT_NULL = "NOT NULL"
/**
* Utility method to add or remove columns from a table
*
* See individual functions for more details
*
* @param newColumns Map of column names and types on the NEW table
* @param selectTransform a function that transforms the select statement. This can be used to change the values
* when copying, such as for removing nulls
*/
fun migrateTable(
database: SupportSQLiteDatabase,
tableName: String,
newColumns: Map<String, String>,
selectTransform: ((String) -> String)? = null
) {
require(newColumns.isNotEmpty())
val newTableTempName = "${tableName}_new"
createNewTable(database, newTableTempName, newColumns)
copyData(database, tableName, newTableTempName, newColumns.keys, selectTransform)
replaceTable(database, tableName, newTableTempName)
}
/**
* Utility method to create a new table with the given columns
*/
private fun createNewTable(
database: SupportSQLiteDatabase,
newTableName: String,
columns: Map<String, String>
) {
val columnsString = columns.entries.joinToString(",") { "${it.key} ${it.value}" }
database.execSQL("CREATE TABLE $newTableName ($columnsString)")
}
/**
* Utility method to copy data from an old table to a new table. Only the columns in [columnNames] will be copied
*
* @param selectTransform a function that transforms the select statement. This can be used to change the values
* when copying, such as for removing nulls
*/
private fun copyData(
database: SupportSQLiteDatabase,
tableName: String,
newTableName: String,
columnNames: Iterable<String>,
selectTransform: ((String) -> String)? = null
) {
val selectColumnsString = columnNames.joinToString(",", transform = selectTransform)
val destColumnsString = columnNames.joinToString(",")
database.execSQL(
"INSERT INTO $newTableName ($destColumnsString) " +
"SELECT $selectColumnsString FROM $tableName"
)
}
/**
* Utility method to replace an old table with a new one, essentially deleting the old one and renaming the new one
*/
private fun replaceTable(
database: SupportSQLiteDatabase,
tableName: String,
newTableTempName: String
) {
database.execSQL("DROP TABLE $tableName")
database.execSQL("ALTER TABLE $newTableTempName RENAME TO $tableName")
}
}