This repository has been archived by the owner on May 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ProductionAppDatabase.kt
196 lines (176 loc) · 8.18 KB
/
ProductionAppDatabase.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright 2019 Marc Donald
*
* 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.marcdonald.hibi.data.database
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.marcdonald.hibi.data.entity.*
import com.marcdonald.hibi.internal.PRODUCTION_DATABASE_NAME
import com.marcdonald.hibi.internal.PRODUCTION_DATABASE_VERSION
import timber.log.Timber
@Database(entities = [Entry::class, Tag::class, TagEntryRelation::class, NewWord::class, Book::class, BookEntryRelation::class, EntryImage::class],
version = PRODUCTION_DATABASE_VERSION,
exportSchema = false)
abstract class ProductionAppDatabase : RoomDatabase(), AppDatabase {
abstract override fun dao(): DAO
override fun closeDB() {
if(instance != null) {
instance?.close()
} else {
Timber.e("Log: closeDB: instance is null")
}
}
companion object {
@Volatile private var instance: ProductionAppDatabase? = null
private val LOCK = Any()
operator fun invoke(context: Context) = instance
?: synchronized(LOCK) {
instance
?: buildDatabase(context).also { instance = it }
}
private fun buildDatabase(context: Context) =
Room.databaseBuilder(context.applicationContext,
ProductionAppDatabase::class.java,
PRODUCTION_DATABASE_NAME)
.setJournalMode(JournalMode.TRUNCATE)
.addMigrations(MIGRATION_3_TO_5())
.addMigrations(MIGRATION_5_TO_6())
.addMigrations(MIGRATION_6_TO_7())
.addMigrations(MIGRATION_7_TO_8())
.addMigrations(MIGRATION_8_TO_9())
.addMigrations(MIGRATION_9_TO_10())
.addMigrations(MIGRATION_10_TO_11())
.addMigrations(MIGRATION_11_TO_12())
.addMigrations(MIGRATION_12_TO_13())
.build()
class MIGRATION_3_TO_5 : Migration(3, 5) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE entries RENAME TO Entry")
database.execSQL("CREATE TABLE Tag('name' TEXT NOT NULL, PRIMARY KEY (name))")
database.execSQL("CREATE TABLE TagEntryRelation('tag' TEXT NOT NULL, 'entryId' INTEGER NOT NULL, PRIMARY KEY (tag, entryId))")
}
}
class MIGRATION_5_TO_6 : Migration(5, 6) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE NewWord('word' TEXT NOT NULL, 'reading' TEXT NOT NULL, 'partOfSpeech' TEXT NOT NULL, 'english' TEXT NOT NULL, 'notes' TEXT NOT NULL, 'entryId' INTEGER NOT NULL, 'id' INTEGER NOT NULL PRIMARY KEY)")
}
}
class MIGRATION_6_TO_7 : Migration(6, 7) {
override fun migrate(database: SupportSQLiteDatabase) {
// Fix nullable primary key in Entry
database.execSQL("ALTER TABLE Entry RENAME TO EntryOLD")
database.execSQL("CREATE TABLE Entry(id INTEGER PRIMARY KEY NOT NULL, " +
"day INTEGER NOT NULL," +
"month INTEGER NOT NULL," +
"year INTEGER NOT NULL," +
"hour INTEGER NOT NULL," +
"minute INTEGER NOT NULL," +
"content TEXT NOT NULL)")
database.execSQL("INSERT INTO Entry SELECT * FROM EntryOLD")
database.execSQL("DROP TABLE EntryOLD")
// Add foreign key to NewWord
database.execSQL("ALTER TABLE NewWord RENAME TO NewWordOLD")
database.execSQL("CREATE TABLE NewWord(" +
"word TEXT NOT NULL," +
"reading TEXT NOT NULL," +
"partOfSpeech TEXT NOT NULL," +
"english TEXT NOT NULL," +
"notes TEXT NOT NULL," +
"entryId INTEGER NOT NULL, " +
"id INTEGER PRIMARY KEY NOT NULL," +
"FOREIGN KEY (entryId) REFERENCES Entry(id) ON DELETE CASCADE ON UPDATE CASCADE)")
database.execSQL("INSERT INTO NewWord SELECT * FROM NewWordOLD")
database.execSQL("DROP TABLE NewWordOLD")
// Add foreign key to TagEntryRelation
database.execSQL("ALTER TABLE TagEntryRelation RENAME TO TagEntryRelationOLD")
database.execSQL("CREATE TABLE TagEntryRelation(tag TEXT NOT NULL, " +
"entryId INTEGER NOT NULL, " +
"PRIMARY KEY(tag, entryId), " +
"FOREIGN KEY(entryId) REFERENCES Entry(id) ON DELETE CASCADE ON UPDATE CASCADE, " +
"FOREIGN KEY(tag) REFERENCES Tag(name) ON DELETE CASCADE ON UPDATE CASCADE)")
database.execSQL("INSERT INTO TagEntryRelation SELECT * FROM TagEntryRelationOLD")
database.execSQL("DROP TABLE TagEntryRelationOLD")
}
}
class MIGRATION_7_TO_8 : Migration(7, 8) {
override fun migrate(database: SupportSQLiteDatabase) {
// This is destructive and will delete all tags and tag entry relations
database.execSQL("DROP TABLE Tag")
database.execSQL("DROP TABLE TagEntryRelation")
database.execSQL("CREATE TABLE Tag('name' TEXT NOT NULL, 'id' INTEGER NOT NULL, PRIMARY KEY(id))")
database.execSQL("CREATE TABLE TagEntryRelation(tagId INTEGER NOT NULL, " +
"entryId INTEGER NOT NULL, " +
"PRIMARY KEY(tagId, entryId), " +
"FOREIGN KEY(entryId) REFERENCES Entry(id) ON DELETE CASCADE ON UPDATE CASCADE, " +
"FOREIGN KEY(tagId) REFERENCES Tag(id) ON DELETE CASCADE ON UPDATE CASCADE)")
}
}
class MIGRATION_8_TO_9 : Migration(8, 9) {
override fun migrate(database: SupportSQLiteDatabase) {
// Create Book table
database.execSQL("CREATE TABLE Book('name' TEXT NOT NULL, 'id' INTEGER NOT NULL, PRIMARY KEY(id))")
// Create BookEntryRelation table
database.execSQL("CREATE TABLE BookEntryRelation(bookId INTEGER NOT NULL, " +
"entryId INTEGER NOT NULL, " +
"PRIMARY KEY(bookId, entryId), " +
"FOREIGN KEY(entryId) REFERENCES Entry(id) ON DELETE CASCADE ON UPDATE CASCADE, " +
"FOREIGN KEY(bookId) REFERENCES Book(id) ON DELETE CASCADE ON UPDATE CASCADE)")
}
}
class MIGRATION_9_TO_10 : Migration(9, 10) {
override fun migrate(database: SupportSQLiteDatabase) {
// Add locationId to Entry table
database.execSQL("ALTER TABLE Entry RENAME TO EntryOLD")
database.execSQL("CREATE TABLE Entry(id INTEGER PRIMARY KEY NOT NULL, " +
"day INTEGER NOT NULL," +
"month INTEGER NOT NULL," +
"year INTEGER NOT NULL," +
"hour INTEGER NOT NULL," +
"minute INTEGER NOT NULL," +
"content TEXT NOT NULL," +
"location TEXT NOT NULL DEFAULT '')")
database.execSQL("INSERT INTO Entry(id, day, month, year, hour, minute, content) " +
"SELECT id, day, month, year, hour, minute, content " +
"FROM EntryOLD")
database.execSQL("DROP TABLE EntryOLD")
}
}
class MIGRATION_10_TO_11 : Migration(10, 11) {
override fun migrate(database: SupportSQLiteDatabase) {
// Add EntryImage table
database.execSQL("CREATE TABLE EntryImage(imageName TEXT NOT NULL, " +
"entryId INTEGER NOT NULL, " +
"PRIMARY KEY(imageName, entryId), " +
"FOREIGN KEY(entryId) REFERENCES Entry(id) ON DELETE CASCADE ON UPDATE CASCADE)")
}
}
class MIGRATION_11_TO_12 : Migration(11, 12) {
// This is here because I forgot to add the EntryImage entity to the database in the last version
override fun migrate(database: SupportSQLiteDatabase) {}
}
class MIGRATION_12_TO_13 : Migration(12, 13) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE INDEX EntryImage_EntryId_Index ON EntryImage (entryId)")
database.execSQL("CREATE INDEX BookEntryRelation_EntryId_Index ON BookEntryRelation (entryId)")
database.execSQL("CREATE INDEX NewWord_EntryId_Index ON NewWord (entryId)")
database.execSQL("CREATE INDEX TagEntryRelation_EntryId_Index ON TagEntryRelation (entryId)")
}
}
}
}