Download File
Download Project
Settings
Line Wrap
Themes
default
ambiance
bespin
dracula
eclipse
material
mbo
mdn-like
neat
solarized dark
ttcn
zenburn
UsersDatabase.java
/* * 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 android.arch.persistence.db.SupportSQLiteDatabase; import android.arch.persistence.room.Database; import android.arch.persistence.room.Room; import android.arch.persistence.room.RoomDatabase; import android.arch.persistence.room.migration.Migration; import android.content.Context; import android.support.annotation.VisibleForTesting; /** * The Room database that contains the Users table */ @Database(entities = {User.class}, version = 2) 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. } }; public static UsersDatabase getInstance(Context context) { synchronized (sLock) { if (INSTANCE == null) { INSTANCE = Room.databaseBuilder(context.getApplicationContext(), UsersDatabase.class, "Sample.db") .addMigrations(MIGRATION_1_2) .build(); } return INSTANCE; } } }