Extracted createSqliteDriver()

This commit is contained in:
dankito 2024-09-12 00:22:22 +02:00
parent 3a39c0e64f
commit ad4c78a380
1 changed files with 17 additions and 10 deletions

View File

@ -7,6 +7,7 @@ import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.* import androidx.compose.ui.window.*
import app.cash.sqldelight.async.coroutines.synchronous import app.cash.sqldelight.async.coroutines.synchronous
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import bankmeister.composeapp.generated.resources.AppIcon_svg import bankmeister.composeapp.generated.resources.AppIcon_svg
import bankmeister.composeapp.generated.resources.Res import bankmeister.composeapp.generated.resources.Res
@ -26,21 +27,27 @@ fun main() = application {
icon = painterResource(Res.drawable.AppIcon_svg), icon = painterResource(Res.drawable.AppIcon_svg),
state = WindowState(position = WindowPosition(Alignment.Center), size = DpSize(1000.dp, 800.dp)), state = WindowState(position = WindowPosition(Alignment.Center), size = DpSize(1000.dp, 800.dp)),
) { ) {
File("data/db").mkdirs() DI.setRepository(createSqlDriverDriver())
DI.setRepository(JdbcSqliteDriver("jdbc:sqlite:data/db/Bankmeister.db").also { driver ->
BankmeisterDb.Schema.synchronous().also { schema ->
if (File("data/db/Bankmeister.db").exists() == false) {
schema.create(driver)
}
schema.migrate(driver, schema.version, 1)
}
})
App() App()
} }
} }
private fun createSqlDriverDriver(): SqlDriver {
File("data/db").mkdirs()
return JdbcSqliteDriver("jdbc:sqlite:data/db/Bankmeister.db").also { driver ->
BankmeisterDb.Schema.synchronous().also { schema ->
if (File("data/db/Bankmeister.db").exists() == false) {
schema.create(driver)
}
schema.migrate(driver, schema.version, 1)
}
}
}
@Preview @Preview
@Composable @Composable
fun AppPreview() { fun AppPreview() {