Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce mandatory session verification only for new logins #2811

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ import javax.inject.Inject
@SingleIn(AppScope::class)
class MigrationPresenter @Inject constructor(
private val migrationStore: MigrationStore,
private val migrations: Set<@JvmSuppressWildcards AppMigration>,
migrations: Set<@JvmSuppressWildcards AppMigration>,
) : Presenter<MigrationState> {
private val orderedMigrations = migrations.sortedBy { it.order }
private val lastMigration: Int = orderedMigrations.lastOrNull()?.order ?: 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


@Composable
override fun present(): MigrationState {
val migrationStoreVersion by migrationStore.applicationMigrationVersion().collectAsState(initial = null)
Expand All @@ -48,11 +51,9 @@ class MigrationPresenter @Inject constructor(
// migrationStore.setApplicationMigrationVersion(0)
// }

val orderedMigrations = migrations.sortedBy { it.order }

LaunchedEffect(migrationStoreVersion) {
val migrationValue = migrationStoreVersion ?: return@LaunchedEffect
if (migrationValue == MIGRATION_VERSION) {
if (migrationValue == lastMigration) {
Timber.d("Current app migration version: $migrationValue. No migration needed.")
migrationAction = AsyncData.Success(Unit)
return@LaunchedEffect
Expand All @@ -70,10 +71,4 @@ class MigrationPresenter @Inject constructor(
migrationAction = migrationAction,
)
}

companion object {
// Increment this value when you need to run the migration again, and
// add step in the LaunchedEffect above
const val MIGRATION_VERSION = 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ class MigrationPresenterTest {

@Test
fun `present - no migration should occurs if ApplicationMigrationVersion is the last one`() = runTest {
val store = InMemoryMigrationStore(MigrationPresenter.MIGRATION_VERSION)
val migrations = (1..10).map { FakeMigration(it) }
val store = InMemoryMigrationStore(migrations.maxOf { it.order })
val presenter = createPresenter(
migrationStore = store,
migrations = migrations.toSet(),
)
moleculeFlow(RecompositionMode.Immediate) {
presenter.present()
Expand All @@ -55,7 +57,7 @@ class MigrationPresenterTest {
@Test
fun `present - testing all migrations`() = runTest {
val store = InMemoryMigrationStore(0)
val migrations = (1..MigrationPresenter.MIGRATION_VERSION).map { FakeMigration(it) }
val migrations = (1..10).map { FakeMigration(it) }
val presenter = createPresenter(
migrationStore = store,
migrations = migrations.toSet(),
Expand All @@ -69,7 +71,7 @@ class MigrationPresenterTest {
assertThat(state.migrationAction).isEqualTo(AsyncData.Loading(Unit))
}
consumeItemsUntilPredicate { it.migrationAction is AsyncData.Success }
assertThat(store.applicationMigrationVersion().first()).isEqualTo(MigrationPresenter.MIGRATION_VERSION)
assertThat(store.applicationMigrationVersion().first()).isEqualTo(migrations.maxOf { it.order })
for (migration in migrations) {
migration.migrateLambda.assertions().isCalledOnce()
}
Expand Down
Loading