Search in sources :

Example 1 with SupportSQLiteDatabase

use of androidx.sqlite.db.SupportSQLiteDatabase in project SeriesGuide by UweTrottmann.

the class MigrationTest method migrationFrom44To45_containsCorrectData.

@Test
public void migrationFrom44To45_containsCorrectData() throws IOException {
    SupportSQLiteDatabase db = migrationTestHelper.createDatabase(TEST_DB_NAME, SgRoomDatabase.VERSION_44_RECREATE_SERIES_EPISODES);
    RoomDatabaseTestHelper.insertShow(SHOW, db);
    RoomDatabaseTestHelper.insertSeason(SEASON, db);
    RoomDatabaseTestHelper.insertEpisode(EPISODE, SHOW.getTvdbId(), SEASON.getTvdbId(), SEASON.getNumber(), db);
    db.close();
    assertTestData_series_seasons_episodes(getMigratedDatabase(SgRoomDatabase.VERSION_45_RECREATE_SEASONS));
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) Test(org.junit.Test)

Example 2 with SupportSQLiteDatabase

use of androidx.sqlite.db.SupportSQLiteDatabase in project SeriesGuide by UweTrottmann.

the class MigrationTest method migrationFrom48To49_containsCorrectData.

@Test
public void migrationFrom48To49_containsCorrectData() throws IOException {
    SupportSQLiteDatabase dbOld = migrationTestHelper.createDatabase(TEST_DB_NAME, SgRoomDatabase.VERSION_48_EPISODE_PLAYS);
    RoomDatabaseTestHelper.insertShow(SHOW, dbOld);
    RoomDatabaseTestHelper.insertSeason(SEASON, dbOld);
    RoomDatabaseTestHelper.insertEpisode(dbOld, EPISODE, SHOW.getTvdbId(), SEASON.getTvdbId(), SEASON.getNumber(), true);
    // Insert activity
    dbOld.execSQL("INSERT INTO activity (activity_episode, activity_show, activity_time) VALUES (21, 42, 123456789)");
    dbOld.close();
    final SupportSQLiteDatabase db = getMigratedDatabase(SgRoomDatabase.VERSION_49_AUTO_ID_MIGRATION);
    // Old tables should still exist, data should remain.
    assertTestData_series_seasons_episodes(db);
    // New tables have different structure.
    queryAndAssert(db, "SELECT _id, series_tvdb_id, series_tmdb_id, series_title, series_runtime, series_poster, series_next FROM sg_show", dbShow -> {
        // Row id should be auto-generated.
        assertThat(dbShow.getLong(0)).isNotEqualTo(SHOW.getTvdbId());
        // TVDB id should be in new column.
        assertThat(dbShow.getInt(1)).isEqualTo(SHOW.getTvdbId());
        // TMDB id should not be set, but exist.
        assertThat(dbShow.isNull(2)).isTrue();
        // Some other values that should have moved to other columns.
        assertThat(dbShow.getString(3)).isEqualTo(SHOW.getTitle());
        assertThat(dbShow.getInt(4)).isEqualTo(SHOW.getRuntime());
        assertThat(dbShow.getString(5)).isEqualTo(SHOW.getPoster());
        // Next episode changed from TVDB to row ID, so reset to default value.
        assertThat(dbShow.getString(6)).isEmpty();
    });
    Cursor showIdQuery = db.query("SELECT _id FROM sg_show");
    showIdQuery.moveToFirst();
    long showId = showIdQuery.getLong(0);
    showIdQuery.close();
    queryAndAssert(db, "SELECT _id, series_id, season_tmdb_id, season_tvdb_id, season_number, season_order FROM sg_season", dbSeason -> {
        // Row id should be auto-generated.
        assertThat(dbSeason.getLong(0)).isNotEqualTo(SEASON.getTvdbId());
        // Show ID should now be internal ID, not TVDB ID.
        assertThat(dbSeason.getInt(1)).isEqualTo(showId);
        // TMDB id should not be set, but exist.
        assertThat(dbSeason.isNull(2)).isTrue();
        // TVDB ID should be in new column.
        assertThat(dbSeason.getInt(3)).isEqualTo(SEASON.getTvdbId());
        assertThat(dbSeason.getInt(4)).isEqualTo(SEASON.getNumber());
        // order is new, should be the number.
        assertThat(dbSeason.getInt(5)).isEqualTo(SEASON.getNumber());
    });
    Cursor seasonIdQuery = db.query("SELECT _id FROM sg_season");
    seasonIdQuery.moveToFirst();
    long seasonId = seasonIdQuery.getLong(0);
    seasonIdQuery.close();
    queryAndAssert(db, "SELECT _id, series_id, season_id, episode_tmdb_id, episode_tvdb_id, episode_title, episode_number, episode_order, episode_season_number FROM sg_episode", dbEpisode -> {
        // Row id should be auto-generated.
        assertThat(dbEpisode.getLong(0)).isNotEqualTo(EPISODE.getTvdbId());
        // Show and season ID should now be internal ID, not TVDB ID.
        assertThat(dbEpisode.getLong(1)).isEqualTo(showId);
        assertThat(dbEpisode.getLong(2)).isEqualTo(seasonId);
        // TMDB id should not be set, but exist.
        assertThat(dbEpisode.isNull(3)).isTrue();
        assertThat(dbEpisode.getInt(4)).isEqualTo(EPISODE.getTvdbId());
        assertThat(dbEpisode.getString(5)).isEqualTo(EPISODE.getName());
        assertThat(dbEpisode.getInt(6)).isEqualTo(EPISODE.getNumber());
        // order is new, should be the number.
        assertThat(dbEpisode.getInt(7)).isEqualTo(EPISODE.getNumber());
        assertThat(dbEpisode.getInt(8)).isEqualTo(SEASON.getNumber());
    });
    // Ensure new type column was populated.
    queryAndAssert(db, "SELECT * FROM activity", cursor -> {
        int activity_type = cursor.getColumnIndex("activity_type");
        assertThat(cursor.getInt(activity_type)).isEqualTo(ActivityType.TVDB_ID);
    });
    // Ensure unique index now includes type column by inserting same IDs, but different type.
    String activityStmt = "INSERT INTO activity (activity_episode, activity_show, activity_time, activity_type)" + " VALUES (21, 42, 123456789, " + ActivityType.TMDB_ID + ")";
    db.execSQL(activityStmt);
    SQLiteConstraintException constraintException = assertThrows(SQLiteConstraintException.class, () -> db.execSQL(activityStmt));
    assertThat(constraintException).hasMessageThat().contains("UNIQUE constraint");
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) Cursor(android.database.Cursor) Test(org.junit.Test)

Example 3 with SupportSQLiteDatabase

use of androidx.sqlite.db.SupportSQLiteDatabase in project SeriesGuide by UweTrottmann.

the class MigrationTest method migrationFrom46To47_containsCorrectData.

@Test
public void migrationFrom46To47_containsCorrectData() throws IOException {
    SupportSQLiteDatabase db = migrationTestHelper.createDatabase(TEST_DB_NAME, SgRoomDatabase.VERSION_46_SERIES_SLUG);
    RoomDatabaseTestHelper.insertShow(SHOW, db);
    RoomDatabaseTestHelper.insertSeason(SEASON, db);
    RoomDatabaseTestHelper.insertEpisode(EPISODE, SHOW.getTvdbId(), SEASON.getTvdbId(), SEASON.getNumber(), db);
    db.close();
    db = getMigratedDatabase(SgRoomDatabase.VERSION_47_SERIES_POSTER_THUMB);
    assertTestData_series_seasons_episodes(db);
    queryAndAssert(db, "SELECT series_poster_small, poster FROM series", series -> assertThat(series.getString(0)).isEqualTo(ImageTools.TVDB_LEGACY_CACHE_PREFIX + series.getString(1)));
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) Test(org.junit.Test)

Example 4 with SupportSQLiteDatabase

use of androidx.sqlite.db.SupportSQLiteDatabase in project SeriesGuide by UweTrottmann.

the class MigrationTest method migrationFrom47To48_containsCorrectData.

@Test
public void migrationFrom47To48_containsCorrectData() throws IOException {
    int v47 = SgRoomDatabase.VERSION_47_SERIES_POSTER_THUMB;
    SupportSQLiteDatabase db = migrationTestHelper.createDatabase(TEST_DB_NAME, v47);
    RoomDatabaseTestHelper.insertShow(SHOW, db);
    RoomDatabaseTestHelper.insertSeason(SEASON, db);
    TestEpisode testEpisode = getTestEpisode(21);
    RoomDatabaseTestHelper.insertEpisode(db, testEpisode, SHOW.getTvdbId(), SEASON.getTvdbId(), SEASON.getNumber(), true);
    testEpisode = getTestEpisode(22);
    RoomDatabaseTestHelper.insertEpisode(db, testEpisode, SHOW.getTvdbId(), SEASON.getTvdbId(), SEASON.getNumber(), false);
    MovieDetails testMovieDetails = getTestMovieDetails(12);
    testMovieDetails.setWatched(true);
    RoomDatabaseTestHelper.insertMovie(db, testMovieDetails);
    testMovieDetails = getTestMovieDetails(13);
    testMovieDetails.setWatched(false);
    RoomDatabaseTestHelper.insertMovie(db, testMovieDetails);
    db.close();
    db = getMigratedDatabase(SgRoomDatabase.VERSION_48_EPISODE_PLAYS);
    assertTestData_series_seasons_episodes(db);
    // Watched episode should have 1 play.
    queryAndAssert(db, "SELECT plays FROM episodes WHERE _id=21", episodeWatched -> assertThat(episodeWatched.getInt(0)).isEqualTo(1));
    queryAndAssert(db, "SELECT plays FROM episodes WHERE _id=22", episodeNotWatched -> assertThat(episodeNotWatched.getInt(0)).isEqualTo(0));
    // Watched movie should have 1 play.
    queryAndAssert(db, "SELECT movies_plays FROM movies WHERE movies_tmdbid=12", movieWatched -> assertThat(movieWatched.getInt(0)).isEqualTo(1));
    queryAndAssert(db, "SELECT movies_plays FROM movies WHERE movies_tmdbid=13", movieNotWatched -> assertThat(movieNotWatched.getInt(0)).isEqualTo(0));
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) TestEpisode(com.battlelancer.seriesguide.provider.RoomDatabaseTestHelper.TestEpisode) MovieDetails(com.battlelancer.seriesguide.ui.movies.MovieDetails) Test(org.junit.Test)

Example 5 with SupportSQLiteDatabase

use of androidx.sqlite.db.SupportSQLiteDatabase in project SeriesGuide by UweTrottmann.

the class SeriesGuideProvider method query.

@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    if (LOGV) {
        Timber.v("query(uri=%s, proj=%s)", uri, Arrays.toString(projection));
    }
    SupportSQLiteOpenHelper databaseHelper = SgRoomDatabase.getInstance(getContext()).getOpenHelper();
    final int match = sUriMatcher.match(uri);
    // support close op for legacy database import tool, will reopen on next op
    if (match == CLOSE) {
        databaseHelper.close();
        return null;
    }
    // always get writable database, might have to be upgraded
    final SupportSQLiteDatabase db = databaseHelper.getWritableDatabase();
    switch(match) {
        default:
            {
                // Most cases are handled with simple SelectionBuilder
                final SelectionBuilder builder = buildSelection(uri, match);
                Cursor query = null;
                try {
                    query = builder.map(BaseColumns._COUNT, // support count base column
                    "count(*)").where(selection, selectionArgs).query(db, projection, sortOrder);
                } catch (SQLiteException e) {
                    Timber.e(e, "Failed to query with uri=%s", uri);
                }
                if (query != null) {
                    // noinspection ConstantConditions
                    query.setNotificationUri(getContext().getContentResolver(), uri);
                }
                return query;
            }
    }
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) SupportSQLiteOpenHelper(androidx.sqlite.db.SupportSQLiteOpenHelper) SelectionBuilder(com.battlelancer.seriesguide.util.SelectionBuilder) Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException)

Aggregations

SupportSQLiteDatabase (androidx.sqlite.db.SupportSQLiteDatabase)11 Test (org.junit.Test)8 Cursor (android.database.Cursor)2 SupportSQLiteOpenHelper (androidx.sqlite.db.SupportSQLiteOpenHelper)2 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)1 SQLiteException (android.database.sqlite.SQLiteException)1 Uri (android.net.Uri)1 FrameworkSQLiteOpenHelperFactory (androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory)1 SqlDriver (app.cash.sqldelight.db.SqlDriver)1 AndroidSqliteDriver (app.cash.sqldelight.driver.android.AndroidSqliteDriver)1 TestEpisode (com.battlelancer.seriesguide.provider.RoomDatabaseTestHelper.TestEpisode)1 MovieDetails (com.battlelancer.seriesguide.ui.movies.MovieDetails)1 SelectionBuilder (com.battlelancer.seriesguide.util.SelectionBuilder)1