Chapter 14

  1. What is a database migration?

The database migration  (or schema migration) is the automatic management of updates to the database schema. The changes to the schema are incremental, usually reversible, and applied in the moment the database schema needs to be changed in order to reflect changes in the application code.

  1. Describe what could be an alternative approach to discarding an order completely, in the case of insufficient stock for some articles? 

One of the alternatives could be to satisfy orders for all articles for which there are sufficient stock. This could be implemented by running each inventory update in a separate transaction and combining the results of all of them that succeeded.

Yet another alternative would be to satisfy orders as fully as possible. This approach would require selecting rows for update, calculating new possible states, and applying them in the same transaction.

  1. Describe the conceptual difference between http4s and Akka HTTP in regard to defining routes.

http4s defines routes as a partial function that pattern matches over the request. Akka HTTP route definition is constructed from nested directives. The requests follow the path through matching directives top-down.

  1. Can you name a reason why event-sourced data storage can scale better than a traditional relational database?

Concurrent updates require much more locking and synchronization than append-only operations.

  1. Implement a GET /articles/:name call with http4s and doobie.

1. Add new route definition:

 case GET -> Root / "articles" / name => renderInventory(repo.getArticle(name))

2. Extend the repository getArticle method:

def getArticle(name: String): Stream[IO, Inventory] =
sql"SELECT name, count FROM article where name = $name"
.query[(String, Int)].stream.transact(transactor)
.fold(Map.empty[String, Int])(_ + _)

See the source code in GitHub for the refactored version, which reuses the parameterless definition of getInventory.

  1. Implement the GET /articles/:name call with Akka HTTP and Akka Persistence.

1. Add a new query definition:

final case class GetArticle(name: String) extends Query

2. Add a query handler in InventoryActor:

case GetArticle(name) =>
sender() ! Inventory(inventory.state.filter(_._1 == name))

3. Add the route definition:

  pathPrefix("articles") {
path(Segment) { name =>
get {
complete((inventory ? GetArticle(name)).mapTo[Inventory])
}
}
}

The GitHub repository contains this route definition embedded in the previously defined lazy val articlesRoutes: Route

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.104.153