Merge "Consider views during database bundle schema equality check." into androidx-main
diff --git a/room/room-migration/api/restricted_current.txt b/room/room-migration/api/restricted_current.txt
index 90348c9..8d5b5f4 100644
--- a/room/room-migration/api/restricted_current.txt
+++ b/room/room-migration/api/restricted_current.txt
@@ -17,12 +17,14 @@
     method public String getIdentityHash();
     method public int getVersion();
     method public java.util.List<androidx.room.migration.bundle.DatabaseViewBundle> getViews();
+    method public final java.util.Map<java.lang.String,androidx.room.migration.bundle.DatabaseViewBundle> getViewsByName();
     method public boolean isSchemaEqual(androidx.room.migration.bundle.DatabaseBundle other);
     property public java.util.List<androidx.room.migration.bundle.EntityBundle> entities;
     property public java.util.Map<java.lang.String,androidx.room.migration.bundle.EntityBundle> entitiesByTableName;
     property public String identityHash;
     property public int version;
     property public java.util.List<androidx.room.migration.bundle.DatabaseViewBundle> views;
+    property public final java.util.Map<java.lang.String,androidx.room.migration.bundle.DatabaseViewBundle> viewsByName;
   }
 
   public static final class DatabaseBundle.FtsEntityCreateComparator implements java.util.Comparator<androidx.room.migration.bundle.EntityBundle> {
diff --git a/room/room-migration/src/main/java/androidx/room/migration/bundle/DatabaseBundle.kt b/room/room-migration/src/main/java/androidx/room/migration/bundle/DatabaseBundle.kt
index dc0e86c..73534e3 100644
--- a/room/room-migration/src/main/java/androidx/room/migration/bundle/DatabaseBundle.kt
+++ b/room/room-migration/src/main/java/androidx/room/migration/bundle/DatabaseBundle.kt
@@ -56,6 +56,11 @@
         entities.associateBy { it.tableName }
     }
 
+    @delegate:Transient
+    public val viewsByName: Map<String, DatabaseViewBundle> by lazy {
+        views.associateBy { it.viewName }
+    }
+
     /**
      * @return List of SQL queries to build this database from scratch.
      */
@@ -73,10 +78,8 @@
 
     @Override
     override fun isSchemaEqual(other: DatabaseBundle): Boolean {
-        return checkSchemaEquality(
-            entitiesByTableName,
-            other.entitiesByTableName
-        )
+        return checkSchemaEquality(entitiesByTableName, other.entitiesByTableName) &&
+            checkSchemaEquality(viewsByName, other.viewsByName)
     }
 
     // Comparator to sort FTS entities after their declared external content entity so that the
diff --git a/room/room-migration/src/test/java/androidx/room/migration/bundle/DatabaseBundleTest.kt b/room/room-migration/src/test/java/androidx/room/migration/bundle/DatabaseBundleTest.kt
index 8da656e..7726687 100644
--- a/room/room-migration/src/test/java/androidx/room/migration/bundle/DatabaseBundleTest.kt
+++ b/room/room-migration/src/test/java/androidx/room/migration/bundle/DatabaseBundleTest.kt
@@ -98,6 +98,23 @@
         assertThat(bundle.buildCreateQueries(), `is`(listOf("sq1", "sq3", "sq2", "e2_trig")))
     }
 
+    @Test
+    fun schemaEquality_missingView_notEqual() {
+        val entity = EntityBundle("e", "sq",
+            listOf(createFieldBundle("foo"), createFieldBundle("bar")),
+            PrimaryKeyBundle(false, listOf("foo")),
+            emptyList(),
+            emptyList())
+        val view = DatabaseViewBundle("bar", "sq")
+        val bundle1 = DatabaseBundle(1, "hash",
+            listOf(entity), emptyList(),
+            emptyList())
+        val bundle2 = DatabaseBundle(1, "hash",
+            listOf(entity), listOf(view),
+            emptyList())
+        assertThat(bundle1.isSchemaEqual(bundle2), `is`(false))
+    }
+
     private fun createFieldBundle(name: String): FieldBundle {
         return FieldBundle("foo", name, "text", false, null)
     }