-
Notifications
You must be signed in to change notification settings - Fork 150
Migrate database access from Magnum to parlance #2938
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
Open
adamw
wants to merge
8
commits into
master
Choose a base branch
from
migrate-magnum-to-parlance
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
51b3569
task: Resolve parlance API and swap the build dependency
adamw b1b539b
task: Migrate the infrastructure layer (DB.scala and Magnum.scala) to…
adamw 60ac122
task: Migrate the four model classes to parlance
adamw bbb22a6
task: Migrate the DbTx import in service and auth classes
adamw b96fb85
task: Compile and run the backend test suite, fixing migration regres…
adamw c04f82d
task: Update documentation and code comments referencing Magnum
adamw b56ef2b
task: Remove the parlance migration scratch notes
adamw 2c0fdc7
task: Reduce parlance API friction with a Tx alias and typed query DSL
adamw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 10 additions & 9 deletions
19
backend/src/main/scala/com/softwaremill/bootzooka/email/EmailModel.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 4 additions & 5 deletions
9
...ill/bootzooka/infrastructure/Magnum.scala → ...ill/bootzooka/infrastructure/Codecs.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,15 @@ | ||
| package com.softwaremill.bootzooka.infrastructure | ||
|
|
||
| import com.augustnagro.magnum.DbCodec | ||
| import com.softwaremill.bootzooka.logging.Logging | ||
| import ma.chinespirit.parlance.DbCodec | ||
| import com.softwaremill.bootzooka.util.Strings.* | ||
|
|
||
| import java.time.{Instant, OffsetDateTime, ZoneOffset} | ||
|
|
||
| /** Magnum codecs for custom types, useful when writing SQL queries. */ | ||
| object Magnum extends Logging: | ||
| /** parlance [[DbCodec]]s for custom types, useful when writing SQL queries. */ | ||
| object Codecs: | ||
| given DbCodec[Instant] = summon[DbCodec[OffsetDateTime]].biMap(_.toInstant, _.atOffset(ZoneOffset.UTC)) | ||
|
|
||
| given idCodec[T]: DbCodec[Id[T]] = DbCodec.StringCodec.biMap(_.asId[T], _.toString) | ||
| given DbCodec[Hashed] = DbCodec.StringCodec.biMap(_.asHashed, _.toString) | ||
| given DbCodec[LowerCased] = DbCodec.StringCodec.biMap(_.toLowerCased, _.toString) | ||
| end Magnum | ||
| end Codecs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
backend/src/main/scala/com/softwaremill/bootzooka/infrastructure/Tx.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.softwaremill.bootzooka.infrastructure | ||
|
|
||
| import ma.chinespirit.parlance.{DbTx, Postgres} | ||
|
|
||
| /** A transaction context for the application's single (Postgres) database. Use as a `(using Tx)` parameter on methods that must run within a | ||
| * transaction; an instance is provided by [[DB.transact]] / [[DB.transactEither]]. Centralises the choice of database type so call sites | ||
| * don't repeat `DbTx[Postgres]`. | ||
| */ | ||
| type Tx = DbTx[Postgres] |
21 changes: 11 additions & 10 deletions
21
backend/src/main/scala/com/softwaremill/bootzooka/passwordreset/PasswordResetCodeModel.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 12 additions & 11 deletions
23
backend/src/main/scala/com/softwaremill/bootzooka/security/ApiKeyModel.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,32 @@ | ||
| package com.softwaremill.bootzooka.security | ||
|
|
||
| import com.augustnagro.magnum.{DbTx, PostgresDbType, Repo, SqlName, SqlNameMapper, Table, TableInfo, sql} | ||
| import com.softwaremill.bootzooka.infrastructure.Magnum.given | ||
| import ma.chinespirit.parlance.{EntityMeta, Repo, SqlName, SqlNameMapper, Table, TableInfo, sql} | ||
| import com.softwaremill.bootzooka.infrastructure.Codecs.given | ||
| import com.softwaremill.bootzooka.infrastructure.Tx | ||
| import com.softwaremill.bootzooka.user.User | ||
| import com.softwaremill.bootzooka.util.Strings.Id | ||
| import ox.discard | ||
|
|
||
| import java.time.Instant | ||
|
|
||
| class ApiKeyModel: | ||
| private val apiKeyRepo = Repo[ApiKey, ApiKey, Id[ApiKey]] | ||
| private val apiKeyRepo = Repo[ApiKey, ApiKey, Id[ApiKey]]() | ||
| private val a = TableInfo[ApiKey, ApiKey, Id[ApiKey]] | ||
|
|
||
| def insert(apiKey: ApiKey)(using DbTx): Unit = apiKeyRepo.insert(apiKey) | ||
| def findById(id: Id[ApiKey])(using DbTx): Option[ApiKey] = apiKeyRepo.findById(id) | ||
| def deleteAllForUser(id: Id[User])(using DbTx): Unit = sql"""DELETE FROM $a WHERE ${a.userId} = $id""".update.run().discard | ||
| def delete(id: Id[ApiKey])(using DbTx): Unit = apiKeyRepo.deleteById(id) | ||
| def insert(apiKey: ApiKey)(using Tx): Unit = apiKeyRepo.rawInsert(apiKey) | ||
| def findById(id: Id[ApiKey])(using Tx): Option[ApiKey] = apiKeyRepo.findById(id) | ||
| def deleteAllForUser(id: Id[User])(using Tx): Unit = sql"""DELETE FROM $a WHERE ${a.userId} = $id""".update.run().discard | ||
| def delete(id: Id[ApiKey])(using Tx): Unit = apiKeyRepo.deleteById(id) | ||
| end ApiKeyModel | ||
|
|
||
| @Table(PostgresDbType, SqlNameMapper.CamelToSnakeCase) | ||
| @Table(SqlNameMapper.CamelToSnakeCase) | ||
| @SqlName("api_keys") | ||
| case class ApiKey(id: Id[ApiKey], userId: Id[User], createdOn: Instant, validUntil: Instant) | ||
| case class ApiKey(id: Id[ApiKey], userId: Id[User], createdOn: Instant, validUntil: Instant) derives EntityMeta | ||
|
|
||
| class ApiKeyAuthToken(apiKeyModel: ApiKeyModel) extends AuthTokenOps[ApiKey]: | ||
| override def tokenName: String = "ApiKey" | ||
| override def findById: DbTx ?=> Id[ApiKey] => Option[ApiKey] = apiKeyModel.findById | ||
| override def delete: DbTx ?=> ApiKey => Unit = ak => apiKeyModel.delete(ak.id) | ||
| override def findById: Tx ?=> Id[ApiKey] => Option[ApiKey] = apiKeyModel.findById | ||
| override def delete: Tx ?=> ApiKey => Unit = ak => apiKeyModel.delete(ak.id) | ||
| override def userId: ApiKey => Id[User] = _.userId | ||
| override def validUntil: ApiKey => Instant = _.validUntil | ||
| override def deleteWhenValid: Boolean = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Transactor[Postgres](Postgres, ds).connect(sql"SELECT 1".query[Int].run()).discard combines transactor construction, nested query execution and an IO connect/discard on one line; extract parts (build transactor, prepare query, then connect) to improve readability.
Show fix
Details
✨ AI Reasoning
The testConnection helper was changed to construct a Transactor and on a single line call connect with a nested query expression and then discard the result. This packs transactor construction, a nested sql.query.run call, and an IO-producing connect/discard sequence into one line, increasing cognitive load when reasoning about side effects and resource usage.
Reply
@AikidoSec feedback: [FEEDBACK]to get better review comments in the future.Reply
@AikidoSec ignore: [REASON]to ignore this issue.More info