No-op refactor to support more than 2 dbs in DatabaseHelper

Currently, the DatabaseHelper assumes MediaProvider can only have 2
dbs, so if a db is NOT internal, it is external. For the photo picker,
we will introduce a separate db, picker.db which breaks this
assumption.

In preparation to introduce the picker.db, we refactor the code with
explicit methods to check if db is internal or external. In future cl,
we'll add an explicit check for picker db.

Test: presubmit
Bug: 190713331
Change-Id: Iab0fbe9a9047fc63a043685312a1e1fd394f9b48
Merged-In: Iab0fbe9a9047fc63a043685312a1e1fd394f9b48
(cherry picked from commit faa5427c1b511a0bf88192cf8e01d48321395524)
diff --git a/legacy/src/com/android/providers/media/LegacyMediaProvider.java b/legacy/src/com/android/providers/media/LegacyMediaProvider.java
index e921155..4c7e741 100644
--- a/legacy/src/com/android/providers/media/LegacyMediaProvider.java
+++ b/legacy/src/com/android/providers/media/LegacyMediaProvider.java
@@ -79,10 +79,10 @@
         final File persistentDir = context.getDir("logs", Context.MODE_PRIVATE);
         Logging.initPersistent(persistentDir);
 
-        mInternalDatabase = new DatabaseHelper(context, INTERNAL_DATABASE_NAME,
-                true, false, true, null, null, null, null, null);
-        mExternalDatabase = new DatabaseHelper(context, EXTERNAL_DATABASE_NAME,
-                false, false, true, null, null, null, null, null);
+        mInternalDatabase = new DatabaseHelper(context, INTERNAL_DATABASE_NAME, false, true, null,
+                null, null, null, null);
+        mExternalDatabase = new DatabaseHelper(context, EXTERNAL_DATABASE_NAME, false, true, null,
+                null, null, null, null);
 
         return true;
     }
diff --git a/src/com/android/providers/media/DatabaseHelper.java b/src/com/android/providers/media/DatabaseHelper.java
index 7f863bd..fb98054 100644
--- a/src/com/android/providers/media/DatabaseHelper.java
+++ b/src/com/android/providers/media/DatabaseHelper.java
@@ -94,6 +94,15 @@
  * on demand, create and upgrade the schema, etc.
  */
 public class DatabaseHelper extends SQLiteOpenHelper implements AutoCloseable {
+    @VisibleForTesting
+    static final String TEST_RECOMPUTE_DB = "test_recompute";
+    @VisibleForTesting
+    static final String TEST_UPGRADE_DB = "test_upgrade";
+    @VisibleForTesting
+    static final String TEST_DOWNGRADE_DB = "test_downgrade";
+    @VisibleForTesting
+    static final String TEST_CLEAN_DB = "test_clean";
+
     static final String INTERNAL_DATABASE_NAME = "internal.db";
     static final String EXTERNAL_DATABASE_NAME = "external.db";
 
@@ -110,7 +119,6 @@
     final String mName;
     final int mVersion;
     final String mVolumeName;
-    final boolean mInternal;  // True if this is the internal database
     final boolean mEarlyUpgrade;
     final boolean mLegacyProvider;
     final @Nullable Class<? extends Annotation> mColumnAnnotation;
@@ -163,18 +171,18 @@
     }
 
     public DatabaseHelper(Context context, String name,
-            boolean internal, boolean earlyUpgrade, boolean legacyProvider,
+            boolean earlyUpgrade, boolean legacyProvider,
             @Nullable Class<? extends Annotation> columnAnnotation,
             @Nullable OnSchemaChangeListener schemaListener,
             @Nullable OnFilesChangeListener filesListener,
             @NonNull OnLegacyMigrationListener migrationListener,
             @Nullable UnaryOperator<String> idGenerator) {
-        this(context, name, getDatabaseVersion(context), internal, earlyUpgrade, legacyProvider,
+        this(context, name, getDatabaseVersion(context), earlyUpgrade, legacyProvider,
                 columnAnnotation, schemaListener, filesListener, migrationListener, idGenerator);
     }
 
     public DatabaseHelper(Context context, String name, int version,
-            boolean internal, boolean earlyUpgrade, boolean legacyProvider,
+            boolean earlyUpgrade, boolean legacyProvider,
             @Nullable Class<? extends Annotation> columnAnnotation,
             @Nullable OnSchemaChangeListener schemaListener,
             @Nullable OnFilesChangeListener filesListener,
@@ -184,8 +192,13 @@
         mContext = context;
         mName = name;
         mVersion = version;
-        mVolumeName = internal ? MediaStore.VOLUME_INTERNAL : MediaStore.VOLUME_EXTERNAL;
-        mInternal = internal;
+        if (isInternal()) {
+            mVolumeName = MediaStore.VOLUME_INTERNAL;
+        } else if (isExternal()) {
+            mVolumeName = MediaStore.VOLUME_EXTERNAL;
+        } else {
+            throw new IllegalStateException("Db must be internal/external");
+        }
         mEarlyUpgrade = earlyUpgrade;
         mLegacyProvider = legacyProvider;
         mColumnAnnotation = columnAnnotation;
@@ -196,9 +209,9 @@
         mMigrationFileName = "." + mVolumeName;
 
         // Configure default filters until we hear differently
-        if (mInternal) {
+        if (isInternal()) {
             mFilterVolumeNames.add(MediaStore.VOLUME_INTERNAL);
-        } else {
+        } else if (isExternal()) {
             mFilterVolumeNames.add(MediaStore.VOLUME_EXTERNAL_PRIMARY);
         }
 
@@ -229,7 +242,7 @@
         mSchemaLock.writeLock().lock();
         try {
             db.beginTransaction();
-            createLatestViews(db, mInternal);
+            createLatestViews(db);
             db.setTransactionSuccessful();
         } finally {
             db.endTransaction();
@@ -373,10 +386,19 @@
     public void onOpen(final SQLiteDatabase db) {
         Log.v(TAG, "onOpen() for " + mName);
 
-        tryMigrateFromLegacy(db, mInternal ? sMigrationLockInternal : sMigrationLockExternal);
+        tryMigrateFromLegacy(db);
     }
 
-    private void tryMigrateFromLegacy(SQLiteDatabase db, Object migrationLock) {
+    private void tryMigrateFromLegacy(SQLiteDatabase db) {
+        final Object migrationLock;
+        if (isInternal()) {
+            migrationLock = sMigrationLockInternal;
+        } else if (isExternal()) {
+            migrationLock = sMigrationLockExternal;
+        } else {
+            throw new IllegalStateException("Db migration only supported for internal/external db");
+        }
+
         final File migration = new File(mContext.getFilesDir(), mMigrationFileName);
         // Another thread entering migration block will be blocked until the
         // migration is complete from current thread.
@@ -391,7 +413,7 @@
                 // Temporarily drop indexes to improve migration performance
                 makePristineIndexes(db);
                 migrateFromLegacy(db);
-                createLatestIndexes(db, mInternal);
+                createLatestIndexes(db);
             } finally {
                 mSchemaLock.writeLock().unlock();
                 // Clear flag, since we should only attempt once
@@ -824,15 +846,15 @@
                 + UserHandle.myUserId() + ")");
 
         db.execSQL("CREATE TABLE log (time DATETIME, message TEXT)");
-        if (!mInternal) {
+        if (isExternal()) {
             db.execSQL("CREATE TABLE audio_playlists_map (_id INTEGER PRIMARY KEY,"
                     + "audio_id INTEGER NOT NULL,playlist_id INTEGER NOT NULL,"
                     + "play_order INTEGER NOT NULL)");
         }
 
-        createLatestViews(db, mInternal);
-        createLatestTriggers(db, mInternal);
-        createLatestIndexes(db, mInternal);
+        createLatestViews(db);
+        createLatestTriggers(db);
+        createLatestIndexes(db);
 
         // Since this code is used by both the legacy and modern providers, we
         // only want to migrate when we're running as the modern provider
@@ -903,7 +925,7 @@
                     // Handle playlist files which may need special handling if
                     // there are no "real" playlist files.
                     final int mediaType = c.getInt(c.getColumnIndex(FileColumns.MEDIA_TYPE));
-                    if (!mInternal && volumePath != null &&
+                    if (isExternal() && volumePath != null &&
                             mediaType == FileColumns.MEDIA_TYPE_PLAYLIST) {
                         File playlistFile = new File(data);
 
@@ -1168,7 +1190,7 @@
         c.close();
     }
 
-    private void createLatestViews(SQLiteDatabase db, boolean internal) {
+    private void createLatestViews(SQLiteDatabase db) {
         makePristineViews(db);
 
         if (mColumnAnnotation == null) {
@@ -1181,7 +1203,7 @@
             filterVolumeNames = bindList(mFilterVolumeNames.toArray());
         }
 
-        if (!internal) {
+        if (isExternal()) {
             db.execSQL("CREATE VIEW audio_playlists AS SELECT "
                     + getColumnsForCollection(Audio.Playlists.class)
                     + " FROM files WHERE media_type=4");
@@ -1290,7 +1312,7 @@
         c.close();
     }
 
-    private static void createLatestTriggers(SQLiteDatabase db, boolean internal) {
+    private static void createLatestTriggers(SQLiteDatabase db) {
         makePristineTriggers(db);
 
         final String insertArg =
@@ -1323,7 +1345,7 @@
         c.close();
     }
 
-    private static void createLatestIndexes(SQLiteDatabase db, boolean internal) {
+    private static void createLatestIndexes(SQLiteDatabase db) {
         makePristineIndexes(db);
 
         db.execSQL("CREATE INDEX image_id_index on thumbnails(image_id)");
@@ -1361,7 +1383,7 @@
                 + " WHERE (is_alarm IS 1) OR (is_ringtone IS 1) OR (is_notification IS 1)");
     }
 
-    private static void updateAddOwnerPackageName(SQLiteDatabase db, boolean internal) {
+    private static void updateAddOwnerPackageName(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN owner_package_name TEXT DEFAULT NULL");
 
         // Derive new column value based on well-known paths
@@ -1394,22 +1416,22 @@
         db.execSQL("ALTER TABLE files ADD COLUMN color_range INTEGER;");
     }
 
-    private static void updateAddHashAndPending(SQLiteDatabase db, boolean internal) {
+    private static void updateAddHashAndPending(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN _hash BLOB DEFAULT NULL;");
         db.execSQL("ALTER TABLE files ADD COLUMN is_pending INTEGER DEFAULT 0;");
     }
 
-    private static void updateAddDownloadInfo(SQLiteDatabase db, boolean internal) {
+    private static void updateAddDownloadInfo(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN is_download INTEGER DEFAULT 0;");
         db.execSQL("ALTER TABLE files ADD COLUMN download_uri TEXT DEFAULT NULL;");
         db.execSQL("ALTER TABLE files ADD COLUMN referer_uri TEXT DEFAULT NULL;");
     }
 
-    private static void updateAddAudiobook(SQLiteDatabase db, boolean internal) {
+    private static void updateAddAudiobook(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN is_audiobook INTEGER DEFAULT 0;");
     }
 
-    private static void updateAddRecording(SQLiteDatabase db, boolean internal) {
+    private static void updateAddRecording(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN is_recording INTEGER DEFAULT 0;");
         // We add the column is_recording, rescan all music files
         db.execSQL("UPDATE files SET date_modified=0 WHERE is_music=1;");
@@ -1419,69 +1441,69 @@
         db.execSQL("ALTER TABLE files ADD COLUMN redacted_uri_id TEXT DEFAULT NULL;");
     }
 
-    private static void updateClearLocation(SQLiteDatabase db, boolean internal) {
+    private static void updateClearLocation(SQLiteDatabase db) {
         db.execSQL("UPDATE files SET latitude=NULL, longitude=NULL;");
     }
 
-    private static void updateSetIsDownload(SQLiteDatabase db, boolean internal) {
+    private static void updateSetIsDownload(SQLiteDatabase db) {
         db.execSQL("UPDATE files SET is_download=1 WHERE _data REGEXP '"
                 + FileUtils.PATTERN_DOWNLOADS_FILE + "'");
     }
 
-    private static void updateAddExpiresAndTrashed(SQLiteDatabase db, boolean internal) {
+    private static void updateAddExpiresAndTrashed(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN date_expires INTEGER DEFAULT NULL;");
         db.execSQL("ALTER TABLE files ADD COLUMN is_trashed INTEGER DEFAULT 0;");
     }
 
-    private static void updateAddGroupId(SQLiteDatabase db, boolean internal) {
+    private static void updateAddGroupId(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN group_id INTEGER DEFAULT NULL;");
     }
 
-    private static void updateAddDirectories(SQLiteDatabase db, boolean internal) {
+    private static void updateAddDirectories(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN primary_directory TEXT DEFAULT NULL;");
         db.execSQL("ALTER TABLE files ADD COLUMN secondary_directory TEXT DEFAULT NULL;");
     }
 
-    private static void updateAddXmpMm(SQLiteDatabase db, boolean internal) {
+    private static void updateAddXmpMm(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN document_id TEXT DEFAULT NULL;");
         db.execSQL("ALTER TABLE files ADD COLUMN instance_id TEXT DEFAULT NULL;");
         db.execSQL("ALTER TABLE files ADD COLUMN original_document_id TEXT DEFAULT NULL;");
     }
 
-    private static void updateAddPath(SQLiteDatabase db, boolean internal) {
+    private static void updateAddPath(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN relative_path TEXT DEFAULT NULL;");
     }
 
-    private static void updateAddVolumeName(SQLiteDatabase db, boolean internal) {
+    private static void updateAddVolumeName(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN volume_name TEXT DEFAULT NULL;");
     }
 
-    private static void updateDirsMimeType(SQLiteDatabase db, boolean internal) {
+    private static void updateDirsMimeType(SQLiteDatabase db) {
         db.execSQL("UPDATE files SET mime_type=NULL WHERE format="
                 + MtpConstants.FORMAT_ASSOCIATION);
     }
 
-    private static void updateRelativePath(SQLiteDatabase db, boolean internal) {
+    private static void updateRelativePath(SQLiteDatabase db) {
         db.execSQL("UPDATE files"
                 + " SET " + MediaColumns.RELATIVE_PATH + "=" + MediaColumns.RELATIVE_PATH + "||'/'"
                 + " WHERE " + MediaColumns.RELATIVE_PATH + " IS NOT NULL"
                 + " AND " + MediaColumns.RELATIVE_PATH + " NOT LIKE '%/';");
     }
 
-    private static void updateAddTranscodeSatus(SQLiteDatabase db, boolean internal) {
+    private static void updateAddTranscodeSatus(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN _transcode_status INTEGER DEFAULT 0;");
     }
 
 
-    private static void updateAddVideoCodecType(SQLiteDatabase db, boolean internal) {
+    private static void updateAddVideoCodecType(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN _video_codec_type TEXT DEFAULT NULL;");
     }
 
-    private static void updateClearDirectories(SQLiteDatabase db, boolean internal) {
+    private static void updateClearDirectories(SQLiteDatabase db) {
         db.execSQL("UPDATE files SET primary_directory=NULL, secondary_directory=NULL;");
     }
 
-    private static void updateRestructureAudio(SQLiteDatabase db, boolean internal) {
+    private static void updateRestructureAudio(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN artist_key TEXT DEFAULT NULL;");
         db.execSQL("ALTER TABLE files ADD COLUMN album_key TEXT DEFAULT NULL;");
         db.execSQL("ALTER TABLE files ADD COLUMN genre TEXT DEFAULT NULL;");
@@ -1505,7 +1527,7 @@
         db.execSQL("UPDATE files SET date_modified=0 WHERE media_type=2;");
     }
 
-    private static void updateAddMetadata(SQLiteDatabase db, boolean internal) {
+    private static void updateAddMetadata(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN author TEXT DEFAULT NULL;");
         db.execSQL("ALTER TABLE files ADD COLUMN bitrate INTEGER DEFAULT NULL;");
         db.execSQL("ALTER TABLE files ADD COLUMN capture_framerate REAL DEFAULT NULL;");
@@ -1520,11 +1542,11 @@
         db.execSQL("ALTER TABLE files ADD COLUMN iso INTEGER DEFAULT NULL;");
     }
 
-    private static void updateAddSceneCaptureType(SQLiteDatabase db, boolean internal) {
+    private static void updateAddSceneCaptureType(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN scene_capture_type INTEGER DEFAULT NULL;");
     }
 
-    private static void updateMigrateLogs(SQLiteDatabase db, boolean internal) {
+    private static void updateMigrateLogs(SQLiteDatabase db) {
         // Migrate any existing logs to new system
         try (Cursor c = db.query("log", new String[] { "time", "message" },
                 null, null, null, null, null)) {
@@ -1537,26 +1559,26 @@
         db.execSQL("DELETE FROM log;");
     }
 
-    private static void updateAddLocalMetadata(SQLiteDatabase db, boolean internal) {
+    private static void updateAddLocalMetadata(SQLiteDatabase db) {
         db.execSQL("CREATE TABLE local_metadata (generation INTEGER DEFAULT 0)");
         db.execSQL("INSERT INTO local_metadata VALUES (0)");
     }
 
-    private static void updateAddGeneration(SQLiteDatabase db, boolean internal) {
+    private static void updateAddGeneration(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN generation_added INTEGER DEFAULT 0;");
         db.execSQL("ALTER TABLE files ADD COLUMN generation_modified INTEGER DEFAULT 0;");
     }
 
-    private static void updateAddXmp(SQLiteDatabase db, boolean internal) {
+    private static void updateAddXmp(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN xmp BLOB DEFAULT NULL;");
     }
 
-    private static void updateAudioAlbumId(SQLiteDatabase db, boolean internal) {
+    private static void updateAudioAlbumId(SQLiteDatabase db) {
         // We change the logic for generating album id, rescan all audio files
         db.execSQL("UPDATE files SET date_modified=0 WHERE media_type=2;");
     }
 
-    private static void updateAddModifier(SQLiteDatabase db, boolean internal) {
+    private static void updateAddModifier(SQLiteDatabase db) {
         db.execSQL("ALTER TABLE files ADD COLUMN _modifier INTEGER DEFAULT 0;");
         // For existing files, set default value as _MODIFIER_MEDIA_SCAN
         db.execSQL("UPDATE files SET _modifier=3;");
@@ -1567,7 +1589,7 @@
                 UserHandle.myUserId()));
     }
 
-    private static void recomputeDataValues(SQLiteDatabase db, boolean internal) {
+    private static void recomputeDataValues(SQLiteDatabase db) {
         try (Cursor c = db.query("files", new String[] { FileColumns._ID, FileColumns.DATA },
                 null, null, null, null, null, null)) {
             Log.d(TAG, "Recomputing " + c.getCount() + " data values");
@@ -1641,7 +1663,6 @@
      */
     private void updateDatabase(SQLiteDatabase db, int fromVersion, int toVersion) {
         final long startTime = SystemClock.elapsedRealtime();
-        final boolean internal = mInternal;
 
         if (fromVersion < 700) {
             // Anything older than KK is recreated from scratch
@@ -1655,25 +1676,25 @@
                 updateAddTitleResource(db);
             }
             if (fromVersion < 1000) {
-                updateAddOwnerPackageName(db, internal);
+                updateAddOwnerPackageName(db);
             }
             if (fromVersion < 1003) {
                 updateAddColorSpaces(db);
             }
             if (fromVersion < 1004) {
-                updateAddHashAndPending(db, internal);
+                updateAddHashAndPending(db);
             }
             if (fromVersion < 1005) {
-                updateAddDownloadInfo(db, internal);
+                updateAddDownloadInfo(db);
             }
             if (fromVersion < 1006) {
-                updateAddAudiobook(db, internal);
+                updateAddAudiobook(db);
             }
             if (fromVersion < 1007) {
-                updateClearLocation(db, internal);
+                updateClearLocation(db);
             }
             if (fromVersion < 1008) {
-                updateSetIsDownload(db, internal);
+                updateSetIsDownload(db);
             }
             if (fromVersion < 1009) {
                 // This database version added "secondary_bucket_id", but that
@@ -1681,18 +1702,18 @@
                 // update step is no longer needed.
             }
             if (fromVersion < 1010) {
-                updateAddExpiresAndTrashed(db, internal);
+                updateAddExpiresAndTrashed(db);
             }
             if (fromVersion < 1012) {
                 recomputeDataValues = true;
             }
             if (fromVersion < 1013) {
-                updateAddGroupId(db, internal);
-                updateAddDirectories(db, internal);
+                updateAddGroupId(db);
+                updateAddDirectories(db);
                 recomputeDataValues = true;
             }
             if (fromVersion < 1014) {
-                updateAddXmpMm(db, internal);
+                updateAddXmpMm(db);
             }
             if (fromVersion < 1015) {
                 // Empty version bump to ensure views are recreated
@@ -1701,43 +1722,43 @@
                 // Empty version bump to ensure views are recreated
             }
             if (fromVersion < 1017) {
-                updateSetIsDownload(db, internal);
+                updateSetIsDownload(db);
                 recomputeDataValues = true;
             }
             if (fromVersion < 1018) {
-                updateAddPath(db, internal);
+                updateAddPath(db);
                 recomputeDataValues = true;
             }
             if (fromVersion < 1019) {
                 // Only trigger during "external", so that it runs only once.
-                if (!internal) {
+                if (isExternal()) {
                     deleteLegacyThumbnailData();
                 }
             }
             if (fromVersion < 1020) {
-                updateAddVolumeName(db, internal);
+                updateAddVolumeName(db);
                 recomputeDataValues = true;
             }
             if (fromVersion < 1021) {
                 // Empty version bump to ensure views are recreated
             }
             if (fromVersion < 1022) {
-                updateDirsMimeType(db, internal);
+                updateDirsMimeType(db);
             }
             if (fromVersion < 1023) {
-                updateRelativePath(db, internal);
+                updateRelativePath(db);
             }
             if (fromVersion < 1100) {
                 // Empty version bump to ensure triggers are recreated
             }
             if (fromVersion < 1101) {
-                updateClearDirectories(db, internal);
+                updateClearDirectories(db);
             }
             if (fromVersion < 1102) {
-                updateRestructureAudio(db, internal);
+                updateRestructureAudio(db);
             }
             if (fromVersion < 1103) {
-                updateAddMetadata(db, internal);
+                updateAddMetadata(db);
             }
             if (fromVersion < 1104) {
                 // Empty version bump to ensure views are recreated
@@ -1746,16 +1767,16 @@
                 recomputeDataValues = true;
             }
             if (fromVersion < 1106) {
-                updateMigrateLogs(db, internal);
+                updateMigrateLogs(db);
             }
             if (fromVersion < 1107) {
-                updateAddSceneCaptureType(db, internal);
+                updateAddSceneCaptureType(db);
             }
             if (fromVersion < 1108) {
-                updateAddLocalMetadata(db, internal);
+                updateAddLocalMetadata(db);
             }
             if (fromVersion < 1109) {
-                updateAddGeneration(db, internal);
+                updateAddGeneration(db);
             }
             if (fromVersion < 1110) {
                 // Empty version bump to ensure triggers are recreated
@@ -1764,7 +1785,7 @@
                 recomputeMediaTypeValues(db);
             }
             if (fromVersion < 1112) {
-                updateAddXmp(db, internal);
+                updateAddXmp(db);
             }
             if (fromVersion < 1113) {
                 // Empty version bump to ensure triggers are recreated
@@ -1773,16 +1794,16 @@
                 // Empty version bump to ensure triggers are recreated
             }
             if (fromVersion < 1115) {
-                updateAudioAlbumId(db, internal);
+                updateAudioAlbumId(db);
             }
             if (fromVersion < 1200) {
-                updateAddTranscodeSatus(db, internal);
+                updateAddTranscodeSatus(db);
             }
             if (fromVersion < 1201) {
-                updateAddVideoCodecType(db, internal);
+                updateAddVideoCodecType(db);
             }
             if (fromVersion < 1202) {
-                updateAddModifier(db, internal);
+                updateAddModifier(db);
             }
             if (fromVersion < 1203) {
                 // Empty version bump to ensure views are recreated
@@ -1791,7 +1812,7 @@
                 // Empty version bump to ensure views are recreated
             }
             if (fromVersion < 1205) {
-                updateAddRecording(db, internal);
+                updateAddRecording(db);
             }
             if (fromVersion < 1206) {
                 // Empty version bump to ensure views are recreated
@@ -1813,14 +1834,14 @@
             }
 
             if (recomputeDataValues) {
-                recomputeDataValues(db, internal);
+                recomputeDataValues(db);
             }
         }
 
         // Always recreate latest views and triggers during upgrade; they're
         // cheap and it's an easy way to ensure they're defined consistently
-        createLatestViews(db, internal);
-        createLatestTriggers(db, internal);
+        createLatestViews(db);
+        createLatestTriggers(db);
 
         getOrCreateUuid(db);
 
@@ -1947,7 +1968,25 @@
                 null);
     }
 
+    public boolean isInternal() {
+        return mName.equals(INTERNAL_DATABASE_NAME);
+    }
+
     public boolean isExternal() {
-        return !mInternal;
+        // Matches test dbs as external
+        switch (mName) {
+            case EXTERNAL_DATABASE_NAME:
+                return true;
+            case TEST_RECOMPUTE_DB:
+                return true;
+            case TEST_UPGRADE_DB:
+                return true;
+            case TEST_DOWNGRADE_DB:
+                return true;
+            case TEST_CLEAN_DB:
+                return true;
+            default:
+                return false;
+        }
     }
 }
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index e29ab4e..658b654 100644
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -919,12 +919,12 @@
 
         mMediaScanner = new ModernMediaScanner(context);
 
-        mInternalDatabase = new DatabaseHelper(context, INTERNAL_DATABASE_NAME,
-                true, false, false, Column.class,
-                Metrics::logSchemaChange, mFilesListener, MIGRATION_LISTENER, mIdGenerator);
-        mExternalDatabase = new DatabaseHelper(context, EXTERNAL_DATABASE_NAME,
-                false, false, false, Column.class,
-                Metrics::logSchemaChange, mFilesListener, MIGRATION_LISTENER, mIdGenerator);
+        mInternalDatabase = new DatabaseHelper(context, INTERNAL_DATABASE_NAME, false, false,
+                Column.class, Metrics::logSchemaChange, mFilesListener, MIGRATION_LISTENER,
+                mIdGenerator);
+        mExternalDatabase = new DatabaseHelper(context, EXTERNAL_DATABASE_NAME, false, false,
+                Column.class, Metrics::logSchemaChange, mFilesListener, MIGRATION_LISTENER,
+                mIdGenerator);
 
         if (SdkLevel.isAtLeastS()) {
             mTranscodeHelper = new TranscodeHelperImpl(context, this);
@@ -2883,7 +2883,7 @@
         }
 
         // Update locale if necessary.
-        if (helper == mInternalDatabase && !Locale.getDefault().equals(mLastLocale)) {
+        if (helper.isInternal() && !Locale.getDefault().equals(mLastLocale)) {
             Log.i(TAG, "Updating locale within queryInternal");
             onLocaleChanged(false);
         }
@@ -3925,7 +3925,7 @@
         // So we only set the user_id field in the database for external storage.
         qb.allowColumn(FileColumns._USER_ID);
         int ownerUserId = FileUtils.extractUserId(path);
-        if (!helper.mInternal) {
+        if (helper.isExternal()) {
             if (isAppCloneUserForFuse(ownerUserId)) {
                 values.put(FileColumns._USER_ID, ownerUserId);
             } else {
@@ -4310,7 +4310,7 @@
             }
 
             case IMAGES_THUMBNAILS: {
-                if (helper.mInternal) {
+                if (helper.isInternal()) {
                     throw new UnsupportedOperationException(
                             "Writing to internal storage is not supported.");
                 }
@@ -4332,7 +4332,7 @@
             }
 
             case VIDEO_THUMBNAILS: {
-                if (helper.mInternal) {
+                if (helper.isInternal()) {
                     throw new UnsupportedOperationException(
                             "Writing to internal storage is not supported.");
                 }
@@ -4411,7 +4411,7 @@
             }
 
             case AUDIO_ALBUMART: {
-                if (helper.mInternal) {
+                if (helper.isInternal()) {
                     throw new UnsupportedOperationException("no internal album art allowed");
                 }
 
@@ -9449,13 +9449,6 @@
         return false;
     }
 
-    static boolean isInternalMediaDatabaseName(String name) {
-        if (INTERNAL_DATABASE_NAME.equals(name)) {
-            return true;
-        }
-        return false;
-    }
-
     private @NonNull Uri getBaseContentUri(@NonNull String volumeName) {
         return MediaStore.AUTHORITY_URI.buildUpon().appendPath(volumeName).build();
     }
diff --git a/src/com/android/providers/media/MediaUpgradeReceiver.java b/src/com/android/providers/media/MediaUpgradeReceiver.java
index abb326a..ea72330 100644
--- a/src/com/android/providers/media/MediaUpgradeReceiver.java
+++ b/src/com/android/providers/media/MediaUpgradeReceiver.java
@@ -73,9 +73,8 @@
                     long startTime = System.currentTimeMillis();
                     Log.i(TAG, "---> Start upgrade of media database " + file);
                     try {
-                        DatabaseHelper helper = new DatabaseHelper(
-                                context, file, MediaProvider.isInternalMediaDatabaseName(file),
-                                false, false, Column.class, Metrics::logSchemaChange, null,
+                        DatabaseHelper helper = new DatabaseHelper(context, file, false, false,
+                                Column.class, Metrics::logSchemaChange, null,
                                 MediaProvider.MIGRATION_LISTENER, null);
                         helper.runWithTransaction((db) -> {
                             // Perform just enough to force database upgrade
diff --git a/tests/src/com/android/providers/media/DatabaseHelperTest.java b/tests/src/com/android/providers/media/DatabaseHelperTest.java
index eef8bbd..5235a93 100644
--- a/tests/src/com/android/providers/media/DatabaseHelperTest.java
+++ b/tests/src/com/android/providers/media/DatabaseHelperTest.java
@@ -20,6 +20,10 @@
 
 import static com.android.providers.media.DatabaseHelper.VERSION_LATEST;
 import static com.android.providers.media.DatabaseHelper.makePristineSchema;
+import static com.android.providers.media.DatabaseHelper.TEST_RECOMPUTE_DB;
+import static com.android.providers.media.DatabaseHelper.TEST_UPGRADE_DB;
+import static com.android.providers.media.DatabaseHelper.TEST_DOWNGRADE_DB;
+import static com.android.providers.media.DatabaseHelper.TEST_CLEAN_DB;
 
 import static com.google.common.truth.Truth.assertThat;
 import static com.google.common.truth.Truth.assertWithMessage;
@@ -59,12 +63,6 @@
 @RunWith(AndroidJUnit4.class)
 public class DatabaseHelperTest {
     private static final String TAG = "DatabaseHelperTest";
-
-    private static final String TEST_RECOMPUTE_DB = "test_recompute";
-    private static final String TEST_UPGRADE_DB = "test_upgrade";
-    private static final String TEST_DOWNGRADE_DB = "test_downgrade";
-    private static final String TEST_CLEAN_DB = "test_clean";
-
     private static final String SQLITE_MASTER_ORDER_BY = "type,name,tbl_name";
 
     private static Context sIsolatedContext;
@@ -595,7 +593,7 @@
     private static class DatabaseHelperO extends DatabaseHelper {
         public DatabaseHelperO(Context context, String name) {
             super(context, name, DatabaseHelper.VERSION_O,
-                    false, false, false, Column.class, null, null, null, null);
+                    false, false, Column.class, null, null, null, null);
         }
 
         @Override
@@ -607,7 +605,7 @@
     private static class DatabaseHelperP extends DatabaseHelper {
         public DatabaseHelperP(Context context, String name) {
             super(context, name, DatabaseHelper.VERSION_P,
-                    false, false, false, Column.class, null, null, null, null);
+                    false, false, Column.class, null, null, null, null);
         }
 
         @Override
@@ -619,7 +617,7 @@
     private static class DatabaseHelperQ extends DatabaseHelper {
         public DatabaseHelperQ(Context context, String name) {
             super(context, name, DatabaseHelper.VERSION_Q,
-                    false, false, false, Column.class, null, null, null, null);
+                    false, false, Column.class, null, null, null, null);
         }
 
         @Override
@@ -631,7 +629,7 @@
     private static class DatabaseHelperR extends DatabaseHelper {
         public DatabaseHelperR(Context context, String name) {
             super(context, name, DatabaseHelper.VERSION_R,
-                    false, false, false, Column.class, null, null,
+                    false, false, Column.class, null, null,
                     MediaProvider.MIGRATION_LISTENER, null);
         }
 
@@ -644,7 +642,7 @@
     private static class DatabaseHelperS extends DatabaseHelper {
         public DatabaseHelperS(Context context, String name) {
             super(context, name, DatabaseHelper.VERSION_S,
-                    false, false, false, Column.class, null, null,
+                    false, false, Column.class, null, null,
                     MediaProvider.MIGRATION_LISTENER, null);
         }
     }