Add comments on foreign key constraint enforcement

This commit is contained in:
Sunik Kupfer
2025-08-18 15:05:54 +02:00
parent e5b01d4f7b
commit 0fb33e3216
2 changed files with 8 additions and 1 deletions

View File

@@ -61,6 +61,11 @@ abstract class DatabaseMigrationTest(
// Prepare the database with the initial version.
val dbName = "test"
helper.createDatabase(dbName, version = toVersion - 1).apply {
// Foreign key constraint enforcement is disabled by default in SQLite and needs to
// be enabled per connection. For production code room does that for us - in tests we
// need to enable it ourselves. We could enable foreign key constraint enforcement here,
// but it's more practical for the tests to not do so.
// execSQL("PRAGMA foreign_keys=ON;");
prepare(this)
close()
}

View File

@@ -91,7 +91,9 @@ class Migration18Test : DatabaseMigrationTest(toVersion = 18) {
arrayOf<Any?>(1, 1, "com.android.contacts", 1234)
)
// Two orphan syncstats rows (collectionId not in collection)
// Insert two orphan syncstats rows (collectionId not in collection)
// We can insert them here because SQLite foreign key constraint enforcement is not active.
// See `testMigration()` for details.
db.execSQL(
"INSERT INTO syncstats (id, collectionId, authority, lastSync) VALUES (?, ?, ?, ?)",
arrayOf<Any?>(2, 99, "com.android.calendar", 1660521600)