Resolving auto migration issue where SQLite keywords are failed to be escaped in column names.
The issue was arising from the part of AutoMigrationWriter generating code to transfer data from the old table to the new table, preserving the order of the columns. The SQL statement used did not utilize `` to escape the column names, resulting in a compiler error in scenarios where a SQLite keyword was being used as a column name.
Issue was repro'd in the tests by using the column name "index" and confirming the escape is working as expected with the new changes.
Test: AutoMigrationTest.java
Bug: 197133152
Change-Id: Idbed488acb2027fb1c0d9c302d1cc83ed5bfba2c
diff --git a/room/integration-tests/testapp/schemas/androidx.room.integration.testapp.migration.AutoMigrationDb/1.json b/room/integration-tests/testapp/schemas/androidx.room.integration.testapp.migration.AutoMigrationDb/1.json
index 3ca5c0e..6a4c8e9 100644
--- a/room/integration-tests/testapp/schemas/androidx.room.integration.testapp.migration.AutoMigrationDb/1.json
+++ b/room/integration-tests/testapp/schemas/androidx.room.integration.testapp.migration.AutoMigrationDb/1.json
@@ -503,7 +503,7 @@
},
{
"tableName": "Entity16",
- "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `name` TEXT, `addedInV1` INTEGER NOT NULL DEFAULT 1, PRIMARY KEY(`id`))",
+ "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `name` TEXT, `index` INTEGER NOT NULL DEFAULT 1, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
@@ -518,8 +518,8 @@
"notNull": false
},
{
- "fieldPath": "addedInV1",
- "columnName": "addedInV1",
+ "fieldPath": "index",
+ "columnName": "index",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "1"
diff --git a/room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/migration/AutoMigrationDb.java b/room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/migration/AutoMigrationDb.java
index f910674..deb192f 100644
--- a/room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/migration/AutoMigrationDb.java
+++ b/room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/migration/AutoMigrationDb.java
@@ -460,7 +460,7 @@
@RenameTable(fromTableName = "Entity19", toTableName = "Entity19_V2")
@RenameTable(fromTableName = "Entity20", toTableName = "Entity20_V2")
@RenameTable(fromTableName = "Entity13", toTableName = "Entity13_V2")
- @RenameColumn(tableName = "Entity16", fromColumnName = "addedInV1",
+ @RenameColumn(tableName = "Entity16", fromColumnName = "index",
toColumnName = "renamedInV2")
@RenameColumn(tableName = "Entity17", fromColumnName = "addedInV1",
toColumnName = "renamedInV2")
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/writer/AutoMigrationWriter.kt b/room/room-compiler/src/main/kotlin/androidx/room/writer/AutoMigrationWriter.kt
index 290606e..a264fab 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/writer/AutoMigrationWriter.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/writer/AutoMigrationWriter.kt
@@ -242,9 +242,10 @@
migrateBuilder,
buildString {
append(
- "INSERT INTO `${newTable.tableName}` (${newColumnSequence.joinToString(",")})" +
- " SELECT ${oldColumnSequence.joinToString(",")} FROM " +
- "`$selectFromTable`",
+ "INSERT INTO `${newTable.tableName}` " +
+ "(${newColumnSequence.joinToString(",") { "`$it`" }})" +
+ " SELECT ${oldColumnSequence.joinToString(",") { "`$it`" }} " +
+ "FROM `$selectFromTable`",
)
}
)
@@ -312,8 +313,8 @@
buildString {
append(
"INSERT INTO `$tableNameWithNewPrefix` " +
- "(${newColumnSequence.joinToString(",")})" +
- " SELECT ${oldColumnSequence.joinToString(",")} FROM " +
+ "(${newColumnSequence.joinToString(",") { "`$it`" }})" +
+ " SELECT ${oldColumnSequence.joinToString(",") { "`$it`" }} FROM " +
"`$oldTableName`",
)
}