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
17package androidx.room.integration.testapp.migration;
18
19import android.content.ContentValues;
20import android.database.sqlite.SQLiteDatabase;
21
22import androidx.room.ColumnInfo;
23import androidx.room.Dao;
24import androidx.room.Database;
25import androidx.room.Entity;
26import androidx.room.ForeignKey;
27import androidx.room.Ignore;
28import androidx.room.Index;
29import androidx.room.Insert;
30import androidx.room.PrimaryKey;
31import androidx.room.Query;
32import androidx.room.RoomDatabase;
33import androidx.sqlite.db.SupportSQLiteDatabase;
34
35import 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})
41public 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