AdevintaSpain/Barista

Bug when trying to clean Local Database data with Barista Rule

durbon opened this issue · 1 comments

Library Version:

3.7.0

Describe the Bug:

Using the BaristaRule. We have detected a problem when deleting the data from the app's databases. The problem comes from trying to delete SQLlite tables and view data. The views cannot be deleted and we received the following error

android.database.sqlite.SQLiteException: cannot modify iam_view because it is a view (code 1): , while compiling: DELETE FROM iam_view
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1496)
at com.schibsted.spain.barista.rule.cleardata.internal.DatabaseOperations.deleteTableContent(DatabaseOperations.kt:38)
at com.schibsted.spain.barista.rule.cleardata.ClearDatabaseRule.clearDatabases(ClearDatabaseRule.kt:48)
at com.schibsted.spain.barista.rule.cleardata.ClearDatabaseRule.access$clearDatabases(ClearDatabaseRule.kt:12)
at com.schibsted.spain.barista.rule.cleardata.ClearDatabaseRule$apply$1.evaluate(ClearDatabaseRule.kt:30)
at com.schibsted.spain.barista.rule.cleardata.ClearPreferencesRule$1.evaluate(ClearPreferencesRule.java:26)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:549)
at com.schibsted.spain.barista.rule.flaky.internal.AllowFlakyStatement.evaluate(AllowFlakyStatement.java:27)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)```

Researching the responsible code seems to be for because of the methods of this class DatabaseOperations

open fun getTableNames(sqLiteDatabase: SQLiteDatabase): List<String> {
    sqLiteDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type IN (?, ?)", arrayOf("table", "view"))
        .use { cursor ->
          val tableNames = ArrayList<String>()
          while (cursor.moveToNext()) {
            tableNames.add(cursor.getString(0))
          }
          return tableNames
        }
  }

  open fun deleteTableContent(sqLiteDatabase: SQLiteDatabase, tableName: String) {
    sqLiteDatabase.delete(tableName, null, null)
  }

The method sqLiteDatabase.delete(tableName, null, null) cannot be used with Views. The registers cannot be modified, since it is only the representation from other tables.

So we think that the argument of 'View' should be eliminated in the following fragment

sqLiteDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type IN (?, ?)", arrayOf("table", "view"))

Steps to reproduce the bug:

  • Create a view in SQlite with records and try to delete it
  • In our case the error has occurred after adding a tracking SDK that generates table and view in the local database. The error has been verified by checking that Barista was trying to delete a View.

Expected Behavior:
No need to delete data from the views, only from the tables to have an environment ready for testing

Sloy commented

Hi @durbon! Thank you for the perfectly descriptive report!
I think you're totally right, it doesn't make sense to try to delete views, especially if it causes an exception.

It's definitely a bug, probably caused by a copy&paste technical problem.
Would you like to open a PR removing the Views from the rule?