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.TypeConverters; 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 = 4) @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"); } }; /** * Migrate from: * version 3 - using Room where the {@link User#mId} is an int * to * version 4 - using Room where the {@link User#mId} is a String */ @VisibleForTesting static final Migration MIGRATION_3_4 = new Migration(3, 4) { @Override public void migrate(SupportSQLiteDatabase database) { // SQLite supports a limited operations for ALTER. // Changing the type of a column is not directly supported, so this is what we need // to do: // Create the new table database.execSQL( "CREATE TABLE users_new (userid TEXT NOT NULL," + "username TEXT," + "last_update INTEGER," + "PRIMARY KEY(userid))"); // Copy the data database.execSQL("INSERT INTO users_new (userid, username, last_update) " + "SELECT userid, username, last_update " + "FROM users"); // Remove the old table database.execSQL("DROP TABLE users"); // Change the table name to the correct one database.execSQL("ALTER TABLE users_new RENAME TO users"); } }; /** * Migrate from * version 1 - using the SQLiteDatabase API * to * version 4 - using Room where {@link User} has a new field: {@link User#mDate} and * {@link User#mId} is a String */ @VisibleForTesting static final Migration MIGRATION_1_4 = new Migration(1, 4) { @Override public void migrate(SupportSQLiteDatabase database) { // Create the new table database.execSQL( "CREATE TABLE users_new (userid TEXT, username TEXT, last_update INTEGER," + " PRIMARY KEY(userid))"); // Copy the data database.execSQL("INSERT INTO users_new (userid, username, last_update) " + "SELECT userid, username, 0 " + "FROM users"); // Remove the old table database.execSQL("DROP TABLE users"); // Change the table name to the correct one database.execSQL("ALTER TABLE users_new RENAME TO users"); } }; 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, MIGRATION_3_4, MIGRATION_1_4) .build(); } return INSTANCE; } } }