content: /*
   * Copyright (C) 2017 The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.example.android.persistence.migrations;
  
  import androidx.sqlite.db.SupportSQLiteDatabase;
  import androidx.room.Database;
  import androidx.room.Room;
  import androidx.room.RoomDatabase;
  import androidx.room.TypeConverters;
  import androidx.room.migration.Migration;
  import android.content.Context;
  import androidx.annotation.VisibleForTesting;
  
  /**
   * The Room database that contains the Users table
   */
  @Database(entities = {User.class}, version = 3)
  @TypeConverters(DateConverter.class)
  public abstract class UsersDatabase extends RoomDatabase {
  
      private static UsersDatabase INSTANCE;
  
      public abstract UserDao userDao();
  
      private static final Object sLock = new Object();
  
      /**
       * Migrate from:
       * version 1 - using the SQLiteDatabase API
       * to
       * version 2 - using Room
       */
      @VisibleForTesting
      static final Migration MIGRATION_1_2 = new Migration(1, 2) {
          @Override
          public void migrate(SupportSQLiteDatabase database) {
              // Room uses an own database hash to uniquely identify the database
              // Since version 1 does not use Room, it doesn't have the database hash associated.
              // By implementing a Migration class, we're telling Room that it should use the data
              // from version 1 to version 2.
              // If no migration is provided, then the tables will be dropped and recreated.
              // Since we didn't alter the table, there's nothing else to do here.
          }
      };
  
      /**
       * Migrate from:
       * version 2 - using Room
       * to
       * version 3 - using Room where the {@link User} has an extra field: date
       */
      @VisibleForTesting
      static final Migration MIGRATION_2_3 = new Migration(2, 3) {
          @Override
          public void migrate(SupportSQLiteDatabase database) {
              database.execSQL("ALTER TABLE Users "
                      + " ADD COLUMN last_update INTEGER");
          }
      };
  
      public static UsersDatabase getInstance(Context context) {
          synchronized (sLock) {
              if (INSTANCE == null) {
                  INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                          UsersDatabase.class, "Sample.db")
                          .addMigrations(MIGRATION_1_2, MIGRATION_2_3)
                          .build();
              }
              return INSTANCE;
          }
      }
  
  }
  