forked from typelevel/skunk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransaction.scala
More file actions
37 lines (30 loc) · 975 Bytes
/
Copy pathTransaction.scala
File metadata and controls
37 lines (30 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (c) 2018 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package example
import cats.effect._
import cats.implicits._
import skunk._, skunk.implicits._, skunk.codec.all.int4
import natchez.Trace.Implicits.noop
object Transaction extends IOApp {
def session[F[_]: Concurrent: ContextShift]: Resource[F, Session[F]] =
Session.single(
host = "localhost",
port = 5432,
user = "postgres",
database = "world",
)
def runS[F[_]: Concurrent: ContextShift]: F[_] =
session[F].use { s =>
s.transaction.use { t =>
for {
p <- t.savepoint
_ <- s.execute(sql"blah".command).attempt
_ <- t.rollback(p)
n <- s.execute(sql"select 1".query(int4))
} yield n
}
}
def run(args: List[String]): IO[ExitCode] =
runS[IO].attempt.as(ExitCode.Success)
}