Home | History | Annotate | Download | only in migration
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package androidx.room.integration.testapp.migration;
     18 
     19 import android.content.ContentValues;
     20 import android.database.sqlite.SQLiteDatabase;
     21 
     22 import androidx.room.ColumnInfo;
     23 import androidx.room.Dao;
     24 import androidx.room.Database;
     25 import androidx.room.Entity;
     26 import androidx.room.ForeignKey;
     27 import androidx.room.Ignore;
     28 import androidx.room.Index;
     29 import androidx.room.Insert;
     30 import androidx.room.PrimaryKey;
     31 import androidx.room.Query;
     32 import androidx.room.RoomDatabase;
     33 import androidx.sqlite.db.SupportSQLiteDatabase;
     34 
     35 import java.util.List;
     36 
     37 @SuppressWarnings("WeakerAccess")
     38 @Database(version = MigrationDb.LATEST_VERSION,
     39         entities = {MigrationDb.Entity1.class, MigrationDb.Entity2.class,
     40                 MigrationDb.Entity4.class})
     41 public abstract class MigrationDb extends RoomDatabase {
     42     static final int LATEST_VERSION = 7;
     43     abstract MigrationDao dao();
     44     @Entity(indices = {@Index(value = "name", unique = true)})
     45     static class Entity1 {
     46         public static final String TABLE_NAME = "Entity1";
     47         @PrimaryKey
     48         public int id;
     49         public String name;
     50     }
     51 
     52     @Entity
     53     static class Entity2 {
     54         public static final String TABLE_NAME = "Entity2";
     55         @PrimaryKey(autoGenerate = true)
     56         public int id;
     57         public String addedInV3;
     58         public String name;
     59     }
     60 
     61     @Entity
     62     static class Entity3 { // added in version 4, removed at 6
     63         public static final String TABLE_NAME = "Entity3";
     64         @PrimaryKey
     65         public int id;
     66         @Ignore //removed at 5
     67         public String removedInV5;
     68         public String name;
     69     }
     70 
     71     @Entity(foreignKeys = {
     72             @ForeignKey(entity = Entity1.class,
     73             parentColumns = "name",
     74             childColumns = "name",
     75             deferred = true)})
     76     static class Entity4 {
     77         public static final String TABLE_NAME = "Entity4";
     78         @PrimaryKey
     79         public int id;
     80         @ColumnInfo(collate = ColumnInfo.NOCASE)
     81         public String name;
     82     }
     83 
     84     @Dao
     85     interface MigrationDao {
     86         @Query("SELECT * from Entity1 ORDER BY id ASC")
     87         List<Entity1> loadAllEntity1s();
     88         @Query("SELECT * from Entity2 ORDER BY id ASC")
     89         List<Entity2> loadAllEntity2s();
     90         @Query("SELECT * from Entity2 ORDER BY id ASC")
     91         List<Entity2Pojo> loadAllEntity2sAsPojo();
     92         @Insert
     93         void insert(Entity2... entity2);
     94     }
     95 
     96     static class Entity2Pojo extends Entity2 {
     97     }
     98 
     99     /**
    100      * not a real dao because database will change.
    101      */
    102     static class Dao_V1 {
    103         final SupportSQLiteDatabase mDb;
    104 
    105         Dao_V1(SupportSQLiteDatabase db) {
    106             mDb = db;
    107         }
    108 
    109         public void insertIntoEntity1(int id, String name) {
    110             ContentValues values = new ContentValues();
    111             values.put("id", id);
    112             values.put("name", name);
    113             long insertionId = mDb.insert(Entity1.TABLE_NAME,
    114                     SQLiteDatabase.CONFLICT_REPLACE, values);
    115             if (insertionId == -1) {
    116                 throw new RuntimeException("test sanity failure");
    117             }
    118         }
    119     }
    120 
    121     /**
    122      * not a real dao because database will change.
    123      */
    124     static class Dao_V2 {
    125         final SupportSQLiteDatabase mDb;
    126 
    127         Dao_V2(SupportSQLiteDatabase db) {
    128             mDb = db;
    129         }
    130 
    131         public void insertIntoEntity2(int id, String name) {
    132             ContentValues values = new ContentValues();
    133             values.put("id", id);
    134             values.put("name", name);
    135             long insertionId = mDb.insert(Entity2.TABLE_NAME,
    136                     SQLiteDatabase.CONFLICT_REPLACE, values);
    137             if (insertionId == -1) {
    138                 throw new RuntimeException("test sanity failure");
    139             }
    140         }
    141     }
    142 }
    143