61: Revamp database setup r=msfjarvis a=msfjarvis

An assortment of database related changes

- Moved the `data` module into `app`
- Enabled schema generation for `PostsDatabase`
- Added and enabled [Roomigrant](https://github.com/MatrixDev/Roomigrant)
- Removed the now unused `LobstersPost#isLiked` field
- Bumped the database version to let Roomigrant handle the upgrade

bors r+

Co-authored-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
bors[bot] 2020-11-01 20:22:48 +00:00 committed by GitHub
commit 442293215c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 411 additions and 23 deletions

1
.idea/gradle.xml generated
View file

@ -12,7 +12,6 @@
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" />
<option value="$PROJECT_DIR$/data" />
<option value="$PROJECT_DIR$/lobsters-api" />
<option value="$PROJECT_DIR$/model" />
</set>

View file

@ -21,5 +21,10 @@
<option name="name" value="Google" />
<option name="url" value="https://dl.google.com/dl/android/maven2/" />
</remote-repository>
<remote-repository>
<option name="id" value="maven" />
<option name="name" value="maven" />
<option name="url" value="https://jitpack.io" />
</remote-repository>
</component>
</project>

View file

@ -3,6 +3,10 @@ plugins {
id "dagger.hilt.android.plugin"
}
repositories {
maven { url 'https://jitpack.io' }
}
final def keystorePropertiesFile = rootProject.file("keystore.properties")
android {
adbOptions {
@ -11,6 +15,11 @@ android {
defaultConfig {
applicationId "dev.msfjarvis.lobsters"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
@ -60,9 +69,10 @@ tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
dependencies {
kapt "com.github.MatrixDev.Roomigrant:RoomigrantCompiler:$roomigrant_version"
kapt "com.google.dagger:hilt-compiler:$hilt_dagger_version"
kapt "androidx.hilt:hilt-compiler:$hilt_androidx_version"
implementation(project(":data"))
kapt "androidx.room:room-compiler:$room_version"
implementation(project(":lobsters-api"))
implementation(project(":model"))
implementation "androidx.core:core-ktx:$core_version"
@ -83,9 +93,13 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.navigation:navigation-compose:$nav_compose_version"
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
implementation "androidx.ui:ui-tooling:$compose_version"
implementation "com.github.MatrixDev.Roomigrant:RoomigrantLib:$roomigrant_version"
implementation "com.google.android.material:material:$material_version"
implementation "com.google.dagger:hilt-android:$hilt_dagger_version"
implementation "com.squareup.moshi:moshi:$moshi_version"
implementation "dev.chrisbanes.accompanist:accompanist-coil:$accompanist_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
implementation "saschpe.android:customtabs:$customtabs_version"

View file

@ -0,0 +1,198 @@
{
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "5146546ebef999689c82a1b89e667eb4",
"entities": [
{
"tableName": "lobsters_posts",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`shortId` TEXT NOT NULL, `shortIdUrl` TEXT NOT NULL, `createdAt` TEXT NOT NULL, `title` TEXT NOT NULL, `url` TEXT NOT NULL, `score` INTEGER NOT NULL, `flags` INTEGER NOT NULL, `commentCount` INTEGER NOT NULL, `description` TEXT NOT NULL, `commentsUrl` TEXT NOT NULL, `submitterUser` TEXT NOT NULL, `tags` TEXT NOT NULL, `isLiked` INTEGER NOT NULL, PRIMARY KEY(`shortId`))",
"fields": [
{
"fieldPath": "post.shortId",
"columnName": "shortId",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.shortIdUrl",
"columnName": "shortIdUrl",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.createdAt",
"columnName": "createdAt",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.url",
"columnName": "url",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.score",
"columnName": "score",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "post.flags",
"columnName": "flags",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "post.commentCount",
"columnName": "commentCount",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "post.description",
"columnName": "description",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.commentsUrl",
"columnName": "commentsUrl",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.submitterUser",
"columnName": "submitterUser",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.tags",
"columnName": "tags",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.isLiked",
"columnName": "isLiked",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"shortId"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "lobsters_saved_posts",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`shortId` TEXT NOT NULL, `shortIdUrl` TEXT NOT NULL, `createdAt` TEXT NOT NULL, `title` TEXT NOT NULL, `url` TEXT NOT NULL, `score` INTEGER NOT NULL, `flags` INTEGER NOT NULL, `commentCount` INTEGER NOT NULL, `description` TEXT NOT NULL, `commentsUrl` TEXT NOT NULL, `submitterUser` TEXT NOT NULL, `tags` TEXT NOT NULL, `isLiked` INTEGER NOT NULL, PRIMARY KEY(`shortId`))",
"fields": [
{
"fieldPath": "post.shortId",
"columnName": "shortId",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.shortIdUrl",
"columnName": "shortIdUrl",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.createdAt",
"columnName": "createdAt",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.url",
"columnName": "url",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.score",
"columnName": "score",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "post.flags",
"columnName": "flags",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "post.commentCount",
"columnName": "commentCount",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "post.description",
"columnName": "description",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.commentsUrl",
"columnName": "commentsUrl",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.submitterUser",
"columnName": "submitterUser",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.tags",
"columnName": "tags",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.isLiked",
"columnName": "isLiked",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"shortId"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '5146546ebef999689c82a1b89e667eb4')"
]
}
}

View file

@ -0,0 +1,186 @@
{
"formatVersion": 1,
"database": {
"version": 3,
"identityHash": "fb910a30af3f2c97fcd1f530c798e6e5",
"entities": [
{
"tableName": "lobsters_posts",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`shortId` TEXT NOT NULL, `shortIdUrl` TEXT NOT NULL, `createdAt` TEXT NOT NULL, `title` TEXT NOT NULL, `url` TEXT NOT NULL, `score` INTEGER NOT NULL, `flags` INTEGER NOT NULL, `commentCount` INTEGER NOT NULL, `description` TEXT NOT NULL, `commentsUrl` TEXT NOT NULL, `submitterUser` TEXT NOT NULL, `tags` TEXT NOT NULL, PRIMARY KEY(`shortId`))",
"fields": [
{
"fieldPath": "post.shortId",
"columnName": "shortId",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.shortIdUrl",
"columnName": "shortIdUrl",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.createdAt",
"columnName": "createdAt",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.url",
"columnName": "url",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.score",
"columnName": "score",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "post.flags",
"columnName": "flags",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "post.commentCount",
"columnName": "commentCount",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "post.description",
"columnName": "description",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.commentsUrl",
"columnName": "commentsUrl",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.submitterUser",
"columnName": "submitterUser",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.tags",
"columnName": "tags",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"shortId"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "lobsters_saved_posts",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`shortId` TEXT NOT NULL, `shortIdUrl` TEXT NOT NULL, `createdAt` TEXT NOT NULL, `title` TEXT NOT NULL, `url` TEXT NOT NULL, `score` INTEGER NOT NULL, `flags` INTEGER NOT NULL, `commentCount` INTEGER NOT NULL, `description` TEXT NOT NULL, `commentsUrl` TEXT NOT NULL, `submitterUser` TEXT NOT NULL, `tags` TEXT NOT NULL, PRIMARY KEY(`shortId`))",
"fields": [
{
"fieldPath": "post.shortId",
"columnName": "shortId",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.shortIdUrl",
"columnName": "shortIdUrl",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.createdAt",
"columnName": "createdAt",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.url",
"columnName": "url",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.score",
"columnName": "score",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "post.flags",
"columnName": "flags",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "post.commentCount",
"columnName": "commentCount",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "post.description",
"columnName": "description",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.commentsUrl",
"columnName": "commentsUrl",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.submitterUser",
"columnName": "submitterUser",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "post.tags",
"columnName": "tags",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"shortId"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'fb910a30af3f2c97fcd1f530c798e6e5')"
]
}
}

View file

@ -3,6 +3,7 @@ package dev.msfjarvis.lobsters.data.source
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import dev.matrix.roomigrant.GenerateRoomMigrations
import dev.msfjarvis.lobsters.data.model.LobstersEntity
import dev.msfjarvis.lobsters.data.model.SavedLobstersEntity
@ -11,12 +12,13 @@ import dev.msfjarvis.lobsters.data.model.SavedLobstersEntity
LobstersEntity::class,
SavedLobstersEntity::class
],
version = 2,
exportSchema = false,
version = 3,
exportSchema = true,
)
@TypeConverters(
LobstersApiTypeConverters::class,
)
@GenerateRoomMigrations
abstract class PostsDatabase : RoomDatabase() {
abstract fun postsDao(): PostsDao
abstract fun savedPostsDao(): SavedPostsDao

View file

@ -8,6 +8,7 @@ import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent
import dagger.hilt.android.qualifiers.ApplicationContext
import dev.msfjarvis.lobsters.data.source.PostsDatabase
import dev.msfjarvis.lobsters.data.source.PostsDatabase_Migrations
@Module
@InstallIn(ActivityComponent::class)
@ -16,7 +17,7 @@ object PersistenceModule {
@Provides
fun providePostsDatabase(@ApplicationContext context: Context): PostsDatabase {
return Room.databaseBuilder(context, PostsDatabase::class.java, "posts.db")
.fallbackToDestructiveMigration()
.addMigrations(*PostsDatabase_Migrations.build())
.build()
}
}

View file

@ -23,6 +23,7 @@ buildscript {
okhttp_version = "4.10.0-RC1"
retrofit_version = "2.9.0"
room_version = "2.3.0-alpha03"
roomigrant_version = "0.2.0"
}
repositories {
google()

1
data/.gitignore vendored
View file

@ -1 +0,0 @@
/build

View file

@ -1,12 +0,0 @@
plugins {
id 'kotlin-kapt'
}
dependencies {
implementation project(":model")
kapt "androidx.room:room-compiler:$room_version"
api "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
implementation "com.squareup.moshi:moshi:$moshi_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
}

View file

@ -1,2 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="dev.msfjarvis.todo.data" />

View file

@ -23,6 +23,4 @@ class LobstersPost(
@Json(name = "submitter_user")
val submitterUser: Submitter,
val tags: List<String>,
@Deprecated("Retained for database compatibility as I am too lazy to write migrations")
val isLiked: Boolean = false,
)

View file

@ -1,5 +1,4 @@
rootProject.name = "lobste.rs"
include ':app'
include ':data'
include ':lobsters-api'
include ':model'