Changing the handling of undefined default value in columns.
If the default value is not defined, it should be noted as something other than 'null', since this clashes with the case where a default value is actually a null value and the user cannot differentiate between the two while debugging. This has been changed to print "undefined" in this case. Other alternatives that could be used include "missing" or "notDefined".
Bug: 187865510
Test: TableInfoTest.java
Change-Id: I504e3a390c1b88b53111b878f0a51358a1fa2d83
diff --git a/room/room-runtime/src/androidTest/java/androidx/room/migration/TableInfoTest.java b/room/room-runtime/src/androidTest/java/androidx/room/migration/TableInfoTest.java
index 9f0bf62..439997c 100644
--- a/room/room-runtime/src/androidTest/java/androidx/room/migration/TableInfoTest.java
+++ b/room/room-runtime/src/androidTest/java/androidx/room/migration/TableInfoTest.java
@@ -86,9 +86,9 @@
containsString("TableInfo{name='foo', columns={id=Column{name='id'"
+ ", type='INTEGER', "
+ "affinity='3', notNull=false, primaryKeyPosition=1, "
- + "defaultValue='null'}, name=Column{name='name', type='TEXT', "
+ + "defaultValue='undefined'}, name=Column{name='name', type='TEXT', "
+ "affinity='2', notNull=false, primaryKeyPosition=0, "
- + "defaultValue='null'}}, foreignKeys=[]")
+ + "defaultValue='undefined'}}, foreignKeys=[]")
);
}
@@ -165,6 +165,30 @@
assertThat(dbInfo, is(not(expectedInfo)));
}
+ @Test
+ public void defaultValue_missing_should_print_undefined() {
+ mDb = createDatabase(
+ "CREATE TABLE foo (name TEXT)");
+ TableInfo dbInfo = TableInfo.read(mDb, "foo");
+ TableInfo.Column columnInfo = dbInfo.columns.get("name");
+ assertThat(columnInfo.toString()).isEqualTo(
+ "Column{name='name', type='TEXT', affinity='2', notNull=false, "
+ + "primaryKeyPosition=0, defaultValue='undefined'}"
+ );
+ }
+
+ @Test
+ public void defaultValue_null_should_print_null() {
+ mDb = createDatabase(
+ "CREATE TABLE foo (name TEXT DEFAULT null)");
+ TableInfo dbInfo = TableInfo.read(mDb, "foo");
+ TableInfo.Column columnInfo = dbInfo.columns.get("name");
+ assertThat(columnInfo.toString()).isEqualTo(
+ "Column{name='name', type='TEXT', affinity='2', notNull=false, "
+ + "primaryKeyPosition=0, defaultValue='null'}"
+ );
+ }
+
@SuppressWarnings("deprecation")
@Test
public void defaultValue_oldConstructor() {
diff --git a/room/room-runtime/src/main/java/androidx/room/util/TableInfo.kt b/room/room-runtime/src/main/java/androidx/room/util/TableInfo.kt
index acb92ac..50518c1 100644
--- a/room/room-runtime/src/main/java/androidx/room/util/TableInfo.kt
+++ b/room/room-runtime/src/main/java/androidx/room/util/TableInfo.kt
@@ -327,7 +327,7 @@
override fun toString(): String {
return ("Column{name='$name', type='$type', affinity='$affinity', " +
"notNull=$notNull, primaryKeyPosition=$primaryKeyPosition, " +
- "defaultValue='$defaultValue'}")
+ "defaultValue='${defaultValue ?: "undefined"}'}")
}
}