Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug when trying to clean Local Database data with Barista Rule #381

Open
durbon opened this issue Jan 20, 2021 · 1 comment
Open

Bug when trying to clean Local Database data with Barista Rule #381

durbon opened this issue Jan 20, 2021 · 1 comment
Assignees
Labels

Comments

@durbon
Copy link

durbon commented Jan 20, 2021

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
Copy link
Member

Sloy commented Jan 22, 2021

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?

@Sloy Sloy added the bug label Jan 22, 2021
@alorma alorma self-assigned this Jul 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants