'Get callback of onUpgrade() of Room Persistence Library Android

How to get callback when database is upgraded while using Room Persistence Library? I have provided Migration class to add/drop some columns in tables while upgrading Database.



Solution 1:[1]

// customize SupportSQLiteOpenHelper.Factory 
public class DecoratedOpenHelperFactory implements SupportSQLiteOpenHelper.Factory {
    @NonNull
    private final SupportSQLiteOpenHelper.Factory delegate;
    @Nullable
    private final SupportSQLiteOpenHelper.Callback customListener;

    public DecoratedOpenHelperFactory(@NonNull SupportSQLiteOpenHelper.Factory factory, @Nullable SupportSQLiteOpenHelper.Callback customListener) {
        this.delegate = factory;
        this.customListener = customListener;
    }

    @Override
    public SupportSQLiteOpenHelper create(@NonNull SupportSQLiteOpenHelper.Configuration configuration) {
        final SupportSQLiteOpenHelper.Configuration sqliteConfig = SupportSQLiteOpenHelper.Configuration.builder(configuration.context)
                .name(configuration.name)
                .callback(new DecoratedCallback(configuration.callback, customListener))
                .build();
        return delegate.create(sqliteConfig);
    }
}

// customize SupportSQLiteOpenHelper.Callback
public class DecoratedCallback extends SupportSQLiteOpenHelper.Callback {
    @NonNull
    private final SupportSQLiteOpenHelper.Callback delegate;
    @Nullable
    private final SupportSQLiteOpenHelper.Callback customListener;

    public DecoratedCallback(@NonNull SupportSQLiteOpenHelper.Callback supportSqLiteOpenHelperCallback, @Nullable SupportSQLiteOpenHelper.Callback customListener) {
        super(supportSqLiteOpenHelperCallback.version);
        this.delegate = supportSqLiteOpenHelperCallback;
        this.customListener = customListener;
    }

    @Override
    public void onCreate(@Nullable SupportSQLiteDatabase db) {
        delegate.onCreate(db);
        Optional.ofNullable(customListener).ifPresent(customListener -> customListener.onCreate(db));
    }

    @Override
    public void onUpgrade(SupportSQLiteDatabase db, int oldVersion, int newVersion) {
        delegate.onUpgrade(db, oldVersion, newVersion);
        Optional.ofNullable(customListener).ifPresent(customListener -> customListener.onUpgrade(db, oldVersion, newVersion));
    }

    @Override
    public void onDowngrade(SupportSQLiteDatabase db, int oldVersion, int newVersion) {
        delegate.onDowngrade(db, oldVersion, newVersion);
        Optional.ofNullable(customListener).ifPresent(customListener -> customListener.onDowngrade(db, oldVersion, newVersion));
    }

    @Override
    public void onOpen(SupportSQLiteDatabase db) {
        delegate.onOpen(db);
        Optional.ofNullable(customListener).ifPresent(customListener -> customListener.onOpen(db));
    }

    @Override
    public void onCorruption(SupportSQLiteDatabase db) {
        Optional.ofNullable(customListener).ifPresent(customListener -> customListener.onCorruption(db));
    }
}

Finally, you can use Room like this?

Room.databaseBuilder(context, databaseName)
    .openHelperFactory(new DecoratedOpenHelperFactory(
        new FrameworkSQLiteOpenHelperFactory(), new SupportSQLiteOpenHelper.Callback() {
            @Override
            public void onCreate(SupportSQLiteDatabase db) {

            }

            @Override
            public void onUpgrade(SupportSQLiteDatabase db, int oldVersion, int newVersion) {

            }

            // etc
        }))
    .build();

Solution 2:[2]

You need to provide Room Database callback as mentioned below.

return Room.databaseBuilder(application, WalletDatabase.class, "wallet.db")
            .addMigrations(MIGRATION_1_3)
            .addMigrations(MIGRATION_2_3)
            .addCallback(callback)
            .build();

Declare callback as mentioned below.

public static RoomDatabase.Callback callback = new RoomDatabase.Callback() {
    @Override
    public void onCreate(@NonNull SupportSQLiteDatabase db) {
        super.onCreate(db);
        //DO AS NEEDED
    }

    @Override
    public void onOpen(@NonNull SupportSQLiteDatabase db) {
        super.onOpen(db);
         //DO AS NEEDED
    }
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zyzhang
Solution 2 Pinakin Kansara