Search code examples
androidandroid-room

Detect if Room Destructive Migration is Performed


If I run a Room migration with destructive fallback enabled:

Room.databaseBuilder(applicationContext, MyDb::class.java, "database-name")
        .fallbackToDestructiveMigration()
        .build()

Is there a way to determine if the destructive migration was triggered vs if the migration completed successfully? I'd like to be able to log some information for tracking.


Solution

  • You can use use Room Callback and add it to your database, it has below methods: where you can use onDestructiveMigration

    Here is a sample example

    Java:

    @Database(entities = {...}, version = 1)
    
    public abstract class MyDatabase extends RoomDatabase {
    
        private static final String DATABASE_NAME = "myDatabaseName.db";
    
        private static volatile MyDatabase INSTANCE;
    
        public static MyDatabase getDatabase(final Context context) {
            if (INSTANCE == null) {
                synchronized (MyDatabase.class) {
                    if (INSTANCE == null) {
                        // Create database here.
                        INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                                        MyDatabase.class, DATABASE_NAME)
                                .fallbackToDestructiveMigration()
                                .addCallback(mCallback) // Trigger the callback
                                .build();
                    }
                }
            }
            return INSTANCE;
        }
    
    
        /*
         * Callback when database is created & when it’s opened
         * */
        static RoomDatabase.Callback mCallback = new Callback() {
    
    
             /*
             * Callback when database is first-time created or recreated after destruction
             * */
            @Override
            public void onCreate(@NonNull SupportSQLiteDatabase db) { // 
                super.onCreate(db);
                Log.i("LOG_TAG", "Database is created");
            }
    
            /*
             * callback when the database is opened
             * */
            @Override
            public void onOpen(@NonNull SupportSQLiteDatabase db) { 
                super.onOpen(db);
                Log.i("LOG_TAG", "Database is opened");
            }
    
            /*
             * Called after the database was destructively migrated
             * */
            @Override
            public void onDestructiveMigration(@NonNull SupportSQLiteDatabase db) {
                super.onDestructiveMigration(db);
                Log.i("LOG_TAG", "Database is destructed");
            }
        };
    
    }
    

    Kotlin:

    @Database(entities = [...], version = 1)
    abstract class MyDatabase : RoomDatabase() {
    
        companion object {
    
            private const val DATABASE_NAME = "myDatabaseName.db"
            private var INSTANCE: MyDatabase? = null
    
            fun getInstance(context: Context): MyDatabase {
                return INSTANCE ?: synchronized(this) {
                    val instance = Room.databaseBuilder(
                        context,
                        MyDatabase::class.java,
                        DATABASE_NAME
                    ).fallbackToDestructiveMigration()
                        .addCallback(mCallback) // Trigger the callback
                        .build()
                    INSTANCE = instance
                    instance
                }
            }
    
            /*
             * Callback when database is created & when it’s opened
             * */
            var mCallback: Callback = object : Callback() {
            
                 /*
                 * Callback when database is first-time created or recreated after destruction
                 * */       
                override fun onCreate(db: SupportSQLiteDatabase) {
                    super.onCreate(db)
                    Log.i("LOG_TAG", "Database is created")
                }
                
                /*
                 * callback when the database is opened
                 * */
                override fun onOpen(db: SupportSQLiteDatabase) {  
                    super.onOpen(db)
                    Log.i("LOG_TAG", "Database is opened")
                }
    
                /*
                 * Called after the database was destructively migrated
                 * */
                override fun onDestructiveMigration(db: SupportSQLiteDatabase) {
                    super.onDestructiveMigration(db)
                    Log.i("LOG_TAG", "Database is destructed")
                }
                
            }
        }
        
    }