Commit 3636b5ac authored by Arturo Mejia's avatar Arturo Mejia
Browse files

Closes #6075:Create a migration for SitePermissionsDatabase to introduce

autoplay fields
parent 0803234c
Loading
Loading
Loading
Loading
+3 −16
Original line number Diff line number Diff line
@@ -2,11 +2,11 @@
  "formatVersion": 1,
  "database": {
    "version": 2,
    "identityHash": "f5350dbda12da0f415e9d09d00c36f49",
    "identityHash": "279719818fbc84cac905ddf942282eae",
    "entities": [
      {
        "tableName": "site_permissions",
        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`origin` TEXT NOT NULL, `location` INTEGER NOT NULL, `notification` INTEGER NOT NULL, `microphone` INTEGER NOT NULL, `camera` INTEGER NOT NULL, `bluetooth` INTEGER NOT NULL, `local_storage` INTEGER NOT NULL, `autoplay_audible` INTEGER NOT NULL, `autoplay_inaudible` INTEGER NOT NULL, `saved_at` INTEGER NOT NULL, PRIMARY KEY(`origin`))",
        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`origin` TEXT NOT NULL, `location` INTEGER NOT NULL, `notification` INTEGER NOT NULL, `microphone` INTEGER NOT NULL, `camera` INTEGER NOT NULL, `bluetooth` INTEGER NOT NULL, `local_storage` INTEGER NOT NULL, `saved_at` INTEGER NOT NULL, PRIMARY KEY(`origin`))",
        "fields": [
          {
            "fieldPath": "origin",
@@ -50,18 +50,6 @@
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "autoplayAudible",
            "columnName": "autoplay_audible",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "autoplayInaudible",
            "columnName": "autoplay_inaudible",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "savedAt",
            "columnName": "saved_at",
@@ -79,10 +67,9 @@
        "foreignKeys": []
      }
    ],
    "views": [],
    "setupQueries": [
      "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'f5350dbda12da0f415e9d09d00c36f49')"
      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"279719818fbc84cac905ddf942282eae\")"
    ]
  }
}
 No newline at end of file
+88 −0
Original line number Diff line number Diff line
{
  "formatVersion": 1,
  "database": {
    "version": 3,
    "identityHash": "f5350dbda12da0f415e9d09d00c36f49",
    "entities": [
      {
        "tableName": "site_permissions",
        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`origin` TEXT NOT NULL, `location` INTEGER NOT NULL, `notification` INTEGER NOT NULL, `microphone` INTEGER NOT NULL, `camera` INTEGER NOT NULL, `bluetooth` INTEGER NOT NULL, `local_storage` INTEGER NOT NULL, `autoplay_audible` INTEGER NOT NULL, `autoplay_inaudible` INTEGER NOT NULL, `saved_at` INTEGER NOT NULL, PRIMARY KEY(`origin`))",
        "fields": [
          {
            "fieldPath": "origin",
            "columnName": "origin",
            "affinity": "TEXT",
            "notNull": true
          },
          {
            "fieldPath": "location",
            "columnName": "location",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "notification",
            "columnName": "notification",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "microphone",
            "columnName": "microphone",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "camera",
            "columnName": "camera",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "bluetooth",
            "columnName": "bluetooth",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "localStorage",
            "columnName": "local_storage",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "autoplayAudible",
            "columnName": "autoplay_audible",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "autoplayInaudible",
            "columnName": "autoplay_inaudible",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "savedAt",
            "columnName": "saved_at",
            "affinity": "INTEGER",
            "notNull": true
          }
        ],
        "primaryKey": {
          "columnNames": [
            "origin"
          ],
          "autoGenerate": false
        },
        "indices": [],
        "foreignKeys": []
      }
    ],
    "views": [],
    "setupQueries": [
      "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'f5350dbda12da0f415e9d09d00c36f49')"
    ]
  }
}
 No newline at end of file
+29 −2
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
import mozilla.components.feature.sitepermissions.SitePermissions
import mozilla.components.feature.sitepermissions.SitePermissions.Status
import mozilla.components.feature.sitepermissions.SitePermissionsStorage
import org.junit.After
import org.junit.Assert.assertEquals
@@ -54,14 +55,14 @@ class OnDeviceSitePermissionsStorageTest {
        val origin = "https://www.mozilla.org".toUri().host!!
        val sitePermissions = SitePermissions(
            origin = origin,
            camera = SitePermissions.Status.BLOCKED,
            camera = Status.BLOCKED,
            savedAt = System.currentTimeMillis()
        )
        storage.save(sitePermissions)
        val sitePermissionsFromStorage = storage.findSitePermissionsBy(origin)!!

        assertEquals(origin, sitePermissionsFromStorage.origin)
        assertEquals(SitePermissions.Status.BLOCKED, sitePermissionsFromStorage.camera)
        assertEquals(Status.BLOCKED, sitePermissionsFromStorage.camera)
    }

    @Test
@@ -100,4 +101,30 @@ class OnDeviceSitePermissionsStorageTest {
            assertEquals(1, cursor.getInt(cursor.getColumnIndexOrThrow("camera")))
        }
    }

    @Test
    fun migrate2to3() {
        helper.createDatabase(MIGRATION_TEST_DB, 2).apply {
            query("SELECT * FROM site_permissions").use { cursor ->
                assertEquals(8, cursor.columnCount)
            }
            execSQL(
                    "INSERT INTO " +
                            "site_permissions " +
                            "(origin, location, notification, microphone,camera,bluetooth,local_storage,saved_at) " +
                            "VALUES " +
                            "('mozilla.org',1,1,1,1,1,1,1)"
            )
        }

        val dbVersion3 = helper.runMigrationsAndValidate(MIGRATION_TEST_DB, 3, true, Migrations.migration_2_3)

        dbVersion3.query("SELECT * FROM site_permissions").use { cursor ->
            assertEquals(10, cursor.columnCount)

            cursor.moveToFirst()
            assertEquals(Status.BLOCKED.id, cursor.getInt(cursor.getColumnIndexOrThrow("autoplay_audible")))
            assertEquals(Status.ALLOWED.id, cursor.getInt(cursor.getColumnIndexOrThrow("autoplay_inaudible")))
        }
    }
}
+22 −1
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ import mozilla.components.feature.sitepermissions.SitePermissions
/**
 * Internal database for saving site permissions.
 */
@Database(entities = [SitePermissionsEntity::class], version = 2)
@Database(entities = [SitePermissionsEntity::class], version = 3)
@TypeConverters(StatusConverter::class)
internal abstract class SitePermissionsDatabase : RoomDatabase() {
    abstract fun sitePermissionsDao(): SitePermissionsDao
@@ -36,6 +36,8 @@ internal abstract class SitePermissionsDatabase : RoomDatabase() {
                "site_permissions_database"
            ).addMigrations(
                Migrations.migration_1_2
            ).addMigrations(
                Migrations.migration_2_3
            ).build().also { instance = it }
        }
    }
@@ -77,4 +79,23 @@ internal object Migrations {
            )
        }
    }
    @Suppress("MagicNumber")
    val migration_2_3 = object : Migration(2, 3) {
        override fun migrate(database: SupportSQLiteDatabase) {
            val haveAutoPlayColumns = database.query("SELECT * FROM site_permissions").columnCount == 10
            // We just want to apply this migration for user that do not have
            // the new autoplay fields autoplay_audible and autoplay_inaudible
            if (!haveAutoPlayColumns) {
                database.execSQL(
                        "ALTER TABLE site_permissions ADD COLUMN autoplay_audible INTEGER NOT NULL DEFAULT ''")
                database.execSQL(
                        "ALTER TABLE site_permissions ADD COLUMN autoplay_inaudible INTEGER NOT NULL DEFAULT ''")

                database.execSQL(" UPDATE site_permissions" +
                        " SET autoplay_audible = -1, " + // BLOCKED by default
                        " `autoplay_inaudible` = 1" // ALLOWED by default
                )
            }
        }
    }
}