Skip to content

Commit

Permalink
Fix database versioning issue on desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Sep 8, 2023
1 parent 9634325 commit 34efb7e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 17 deletions.
5 changes: 4 additions & 1 deletion desktopApp/src/jvmMain/kotlin/com/prof18/feedflow/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ fun main() = application {

val sentryDns = properties["sentry_dns"]
?.toString()

val version = properties["version"]
?.toString()

Expand Down Expand Up @@ -136,7 +137,9 @@ fun main() = application {
aboutDialogState = false
},
) {
AboutContent()
AboutContent(
versionLabel = "Version: ${version ?: "N/A"}",
)
}

FeedFlowMenuBar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollbarAdapter
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
Expand All @@ -21,6 +24,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.prof18.feedflow.MR
import com.prof18.feedflow.core.utils.Websites.FEED_FLOW_WEBSITE
Expand All @@ -35,7 +39,9 @@ import com.prof18.feedflow.ui.style.Spacing
import dev.icerock.moko.resources.compose.stringResource

@Composable
fun AboutContent() {
fun AboutContent(
versionLabel: String,
) {
FeedFlowTheme {
Scaffold { paddingValues ->

Expand All @@ -62,6 +68,7 @@ fun AboutContent() {
) {
SettingsItemList(
listState = listState,
versionLabel = versionLabel,
showLicensesScreen = {
showLicensesScreen = true
},
Expand Down Expand Up @@ -92,6 +99,7 @@ fun AboutContent() {
@Composable
private fun SettingsItemList(
listState: LazyListState,
versionLabel: String,
showLicensesScreen: () -> Unit,
) {
LazyColumn(
Expand Down Expand Up @@ -119,13 +127,24 @@ private fun SettingsItemList(
buttonText = stringResource(MR.strings.open_source_licenses),
)
}

item {
Text(
modifier = Modifier.fillMaxWidth(),
text = versionLabel,
style = MaterialTheme.typography.labelSmall,
textAlign = TextAlign.Center,
)
}
}
}

@Preview
@Composable
private fun AboutContentPreview() {
FeedFlowTheme {
AboutContent()
AboutContent(
versionLabel = "Version: 1.0.0",
)
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ lifecycle-runtime-compose = "2.6.1"
kotlinx-serialization = "1.5.1"
org-robolectric = "4.9"
sql-delight = "2.0.0"
rss-parser = "6.0.1"
rss-parser = "6.0.2"
kotlinx-date-time = "0.4.0"
accompanist = "0.28.0"
jsoup = "1.15.3"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.prof18.feedflow

import com.prof18.feedflow.utils.AppEnvironment
import java.io.File

internal object AppDataPathBuilder {
Expand All @@ -16,8 +17,12 @@ internal object AppDataPathBuilder {
}
}

fun getAppDataPath(): String {
val appPath = appDataPath
fun getAppDataPath(appEnvironment: AppEnvironment): String {
val appPath = if (appEnvironment.isDebug()) {
"$appDataPath-dev"
} else {
appDataPath
}
if (!File(appPath).exists()) {
File(appPath).mkdirs()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import co.touchlab.kermit.Logger
import com.prof18.feedflow.data.DatabaseHelper
import com.prof18.feedflow.db.FeedFlowDB
import com.prof18.feedflow.utils.AppEnvironment
import java.io.File
import java.util.Properties

internal fun initDatabase(logger: Logger): SqlDriver {
val appPath = AppDataPathBuilder.getAppDataPath()
internal fun initDatabase(
appEnvironment: AppEnvironment,
logger: Logger,
): SqlDriver {
val appPath = AppDataPathBuilder.getAppDataPath(appEnvironment)

val databasePath = File(appPath, "/${DatabaseHelper.DB_FILE_NAME_WITH_EXTENSION}")

Expand All @@ -29,12 +33,12 @@ internal fun initDatabase(logger: Logger): SqlDriver {

if (currentVer == 0L) {
FeedFlowDB.Schema.create(driver)
setVersion(driver, 1)
logger.d("init: created tables, setVersion to 1")
setVersion(driver, FeedFlowDB.Schema.version)
logger.d("init: created tables, setVersion to ${FeedFlowDB.Schema.version}")
} else {
val schemaVer = FeedFlowDB.Schema.version
if (schemaVer > currentVer) {
FeedFlowDB.Schema.migrate(driver, currentVer, schemaVer)
FeedFlowDB.Schema.migrate(driver, oldVersion = currentVer, newVersion = schemaVer)
setVersion(driver, schemaVer)
logger.d("init: migrated from $currentVer to $schemaVer")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ fun initKoinDesktop(
modules: List<Module>,
): KoinApplication = initKoin(
appEnvironment = appEnvironment,
modules = modules,
modules = modules + getDatabaseModule(appEnvironment),
)

internal actual inline fun <reified T : BaseViewModel> Module.viewModel(
qualifier: Qualifier?,
noinline definition: Definition<T>,
): KoinDefinition<T> = factory(qualifier, definition)

internal actual val platformModule: Module = module {
single<SqlDriver> {
initDatabase(
logger = getWith("initDatabase"),
)
private fun getDatabaseModule(appEnvironment: AppEnvironment): Module =
module {
single<SqlDriver> {
initDatabase(
appEnvironment = appEnvironment,
logger = getWith("initDatabase"),
)
}
}

internal actual val platformModule: Module = module {
factory {
OpmlFeedHandler(
dispatcherProvider = get(),
Expand Down

0 comments on commit 34efb7e

Please sign in to comment.