Skip to content

Commit 721c7df

Browse files
authored
Merge branch 'master' into update/s3-2.29.15
2 parents 33eb803 + 16579b8 commit 721c7df

File tree

11 files changed

+250
-20
lines changed

11 files changed

+250
-20
lines changed

build.sbt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ val shapelessVersion = "2.3.12"
351351
val scalazVersion = "7.3.8"
352352
val fs2Version = "3.11.0"
353353
val reactiveMongo = "1.1.0-RC14"
354+
val slickPgVersion = "0.22.2"
355+
val scalaTestContainersVersion = "0.41.4"
356+
val postgresqlVersion = "42.7.4"
357+
val json4sVersion = "4.0.7"
354358

355359
lazy val scala2_libraries =
356360
(project in file("scala-2-modules/scala2-libraries"))
@@ -403,7 +407,7 @@ lazy val scala2_libraries =
403407
val circeVersion = "0.14.10"
404408
val monixVersion = "3.4.1"
405409
val sparkVersion = "3.5.3"
406-
val elastic4sVersion = "8.15.3"
410+
val elastic4sVersion = "8.15.4"
407411

408412
val sparkCoreDep = "org.apache.spark" %% "spark-core" % sparkVersion
409413
val sparkSqlDep = "org.apache.spark" %% "spark-sql" % sparkVersion
@@ -432,7 +436,7 @@ lazy val scala_libraries = (project in file("scala-libraries"))
432436
),
433437
libraryDependencies ++= Seq(
434438
"org.playframework" %% "play-slick" % LibraryVersions.playSlickVersion,
435-
"org.postgresql" % "postgresql" % "42.7.4"
439+
"org.postgresql" % "postgresql" % postgresqlVersion
436440
),
437441
dependencyOverrides := Seq(
438442
"com.typesafe.akka" %% "akka-protobuf-v3" % AkkaVersion,
@@ -574,9 +578,10 @@ lazy val scala_libraries_testing = (project in file("scala-libraries-testing"))
574578
"com.lihaoyi" %% "utest" % "0.8.4" % "test",
575579
munitDep,
576580
"com.amazonaws" % "aws-java-sdk-s3" % "1.12.777" % IntegrationTest,
577-
"com.dimafeng" %% "testcontainers-scala-scalatest" % "0.41.3" % IntegrationTest,
578-
"com.dimafeng" %% "testcontainers-scala-localstack-v2" % "0.41.3" % IntegrationTest,
579-
"software.amazon.awssdk" % "s3" % "2.29.15"
581+
"com.dimafeng" %% "testcontainers-scala-scalatest" % scalaTestContainersVersion % IntegrationTest,
582+
"com.dimafeng" %% "testcontainers-scala-localstack-v2" % scalaTestContainersVersion % IntegrationTest,
583+
"software.amazon.awssdk" % "s3" % "2.29.9"
584+
580585
),
581586
Defaults.itSettings,
582587
IntegrationTest / fork := true
@@ -598,9 +603,16 @@ lazy val scala_libraries_persistence =
598603
doobieCore,
599604
doobiePGDep,
600605
"org.reactivemongo" %% "reactivemongo" % reactiveMongo,
601-
"org.reactivemongo" %% "reactivemongo-akkastream" % reactiveMongo,
606+
"org.reactivemongo" %% "reactivemongo-akkastream" % reactiveMongo exclude("org.scala-lang.modules", "scala-parser-combinators_2.13"),
602607
"de.flapdoodle.embed" % "de.flapdoodle.embed.mongo" % embedMongoVersion % IntegrationTest,
603-
logback
608+
logback,
609+
"com.typesafe.slick" %% "slick-hikaricp" % slickVersion,
610+
"org.postgresql" % "postgresql" % postgresqlVersion,
611+
"com.github.tminglei" %% "slick-pg" % slickPgVersion,
612+
"org.json4s" %% "json4s-native" % json4sVersion,
613+
"com.github.tminglei" %% "slick-pg_json4s" % slickPgVersion,
614+
"com.dimafeng" %% "testcontainers-scala-scalatest" % scalaTestContainersVersion % IntegrationTest,
615+
"com.dimafeng" %% "testcontainers-scala-postgresql" % scalaTestContainersVersion % IntegrationTest
604616
)
605617
)
606618

@@ -677,7 +689,7 @@ lazy val zio = (project in file("zio"))
677689
libraryDependencies += "dev.zio" %% "zio" % zioVersion,
678690
libraryDependencies += "dev.zio" %% "zio-streams" % zioVersion,
679691
libraryDependencies += zioTestSbt,
680-
libraryDependencies += "dev.zio" %% "zio-kafka" % "2.8.3",
692+
libraryDependencies += "dev.zio" %% "zio-kafka" % "2.9.0",
681693
libraryDependencies += zioJsonDep,
682694
libraryDependencies += "dev.zio" %% "zio-prelude" % "1.0.0-RC23",
683695
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.19" % Test,

project/plugins.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
22

3-
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.5.5")
3+
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.5.6")
44
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.17.0")
55

66
addSbtPlugin("com.lightbend.akka.grpc" % "sbt-akka-grpc" % "2.2.1")

scala-libraries-persistence/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
- [Introduction to Slick](https://www.baeldung.com/scala/slick-intro)
44
- [Introduction to Skunk – Scala Driver for PostgreSQL](https://www.baeldung.com/scala/skunk-postgresql-driver)
55
- [Introduction to doobie – a JDBC Layer for Scala](https://www.baeldung.com/scala/doobie-intro)
6-
- [Introduction to Reactive Mongo](https://www.baeldung.com/scala/mongo-reactive-intro)
6+
- [Introduction to Reactive Mongo](https://www.baeldung.com/scala/mongo-reactive-intro)
7+
- [Postgres Specific Features in Slick Using Slick-PG](TBD)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
db:
3+
image: postgres:16.4
4+
environment:
5+
- POSTGRES_DB=baeldung
6+
- POSTGRES_USER=baeldung
7+
- POSTGRES_PASSWORD=password
8+
ports:
9+
- '5432:5432'
10+
volumes:
11+
- baeldung-postgresql-data:/var/lib/postgresql/data
12+
13+
volumes:
14+
baeldung-postgresql-data:
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.scala.slick_pg
2+
3+
import com.baeldung.scala.slick_pg.BaeldungPostgresProfile.api.*
4+
import com.baeldung.scala.slick_pg.entity.BaeldungEntity
5+
import com.baeldung.scala.slick_pg.table.baeldungEntityTable
6+
import com.dimafeng.testcontainers.PostgreSQLContainer
7+
import com.dimafeng.testcontainers.scalatest.TestContainerForAll
8+
import org.json4s.native.JsonMethods
9+
import org.scalatest.flatspec.AsyncFlatSpec
10+
import org.testcontainers.utility.DockerImageName
11+
import slick.dbio.DBIO
12+
13+
import java.time.OffsetDateTime
14+
import scala.concurrent.Future
15+
16+
class BaeldungPostgresProfileSpec
17+
extends AsyncFlatSpec
18+
with TestContainerForAll {
19+
20+
override val containerDef: PostgreSQLContainer.Def = PostgreSQLContainer.Def(
21+
dockerImageName = DockerImageName.parse("postgres:16.4"),
22+
databaseName = "baeldung",
23+
username = "baeldung",
24+
password = "baeldung"
25+
)
26+
27+
"BaeldungPostgresProfile" should "insert entity" in {
28+
withContainers { container =>
29+
30+
val db: BaeldungPostgresProfile.backend.Database =
31+
BaeldungPostgresProfile.backend.Database.forURL(
32+
containerDef.start().jdbcUrl,
33+
user = "baeldung",
34+
password = "baeldung",
35+
driver = "org.postgresql.Driver"
36+
)
37+
38+
val createdAt = OffsetDateTime.now()
39+
val res = db
40+
.run(
41+
DBIO.seq(
42+
baeldungEntityTable.schema.createIfNotExists,
43+
baeldungEntityTable += BaeldungEntity(
44+
1L,
45+
createdAt,
46+
List(1d, 5d, 2.24d),
47+
JsonMethods.parse("""{"field1": 5, "field2": "test"}""")
48+
)
49+
)
50+
)
51+
.flatMap(_ => db.run(baeldungEntityTable.result))
52+
53+
for (sequence <- res) yield {
54+
sequence.foreach(entity => {
55+
assert(entity.id.equals(1L))
56+
assert(entity.createdAt.equals(createdAt))
57+
assert(entity.prices.equals(List(1d, 5d, 2.24d)))
58+
assert(
59+
entity.metadata.equals(
60+
JsonMethods.parse("""{"field1": 5, "field2": "test"}""")
61+
)
62+
)
63+
})
64+
succeed
65+
}
66+
}
67+
}
68+
}

scala-libraries-persistence/src/main/resources/application.conf

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ h2mem {
55
connectionPool = disabled
66
}
77

8-
#postgres {
9-
# dataSourceClass = "org.postgresql.ds.PGSimpleDataSource"
10-
# properties = {
11-
# serverName = "localhost"
12-
# portNumber = "5432"
13-
# databaseName = "slick-tutorial"
14-
# user = "postgres"
15-
# password = "admin"
16-
# }
17-
#}
8+
postgres {
9+
connectionPool = "HikariCP"
10+
dataSourceClass = "org.postgresql.ds.PGSimpleDataSource"
11+
properties = {
12+
serverName = "localhost"
13+
portNumber = "5432"
14+
databaseName = "baeldung"
15+
user = "baeldung"
16+
password = "password"
17+
}
18+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.scala.slick_pg
2+
3+
import com.github.tminglei.slickpg.*
4+
import org.json4s.{JValue, JsonMethods}
5+
import slick.jdbc.JdbcType
6+
7+
trait BaeldungPostgresProfile
8+
extends ExPostgresProfile
9+
with PgDate2Support
10+
with TimestamptzPostgresProfile
11+
with PgArraySupport
12+
with PgJson4sSupport {
13+
14+
override protected def computeCapabilities: Set[slick.basic.Capability] =
15+
super.computeCapabilities + slick.jdbc.JdbcCapabilities.insertOrUpdate
16+
17+
override val api = BaeldungApi
18+
19+
override val pgjson = "jsonb"
20+
type DOCType = org.json4s.native.Document
21+
override val jsonMethods =
22+
org.json4s.native.JsonMethods.asInstanceOf[JsonMethods[DOCType]]
23+
24+
object BaeldungApi
25+
extends ExtPostgresAPI
26+
with Date2DateTimeImplicitsDuration
27+
with ArrayImplicits
28+
with JsonImplicits {
29+
30+
implicit val doubleListTypeMapper: JdbcType[List[Double]] =
31+
new SimpleArrayJdbcType[Double]("DOUBLE PRECISION").to(_.toList)
32+
}
33+
}
34+
35+
object BaeldungPostgresProfile extends BaeldungPostgresProfile
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.scala.slick_pg
2+
3+
import com.baeldung.scala.slick_pg
4+
import com.baeldung.scala.slick_pg.BaeldungPostgresProfile.api.*
5+
6+
object PostgresConnection {
7+
8+
val db: slick_pg.BaeldungPostgresProfile.backend.Database =
9+
Database.forConfig("postgres")
10+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.scala.slick_pg
2+
3+
import slick.jdbc.PostgresProfile
4+
5+
import java.sql.{PreparedStatement, ResultSet}
6+
import java.time.OffsetDateTime
7+
import java.time.format.DateTimeFormatterBuilder
8+
import java.time.format.DateTimeFormatter
9+
import java.time.temporal.ChronoField
10+
11+
trait TimestamptzPostgresProfile extends PostgresProfile {
12+
13+
override val columnTypes: PostgresJdbcTypes = new PostgresJdbcTypes {
14+
15+
override val offsetDateTimeType: OffsetDateTimeJdbcType =
16+
new OffsetDateTimeJdbcType {
17+
18+
override def sqlType: Int = {
19+
java.sql.Types.TIMESTAMP_WITH_TIMEZONE
20+
}
21+
22+
private val formatter = {
23+
new DateTimeFormatterBuilder()
24+
.append(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
25+
.optionalStart()
26+
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 6, true)
27+
.optionalEnd()
28+
.optionalStart()
29+
.appendOffset("+HH:mm:ss", "+00")
30+
.optionalEnd()
31+
.toFormatter()
32+
}
33+
34+
override def setValue(
35+
v: OffsetDateTime,
36+
p: PreparedStatement,
37+
idx: Int
38+
): Unit = {
39+
p.setObject(idx, v)
40+
}
41+
override def getValue(r: ResultSet, idx: Int): OffsetDateTime = {
42+
r.getString(idx) match {
43+
case null => null
44+
case date: String => OffsetDateTime.from(formatter.parse(date))
45+
}
46+
}
47+
override def updateValue(
48+
v: OffsetDateTime,
49+
r: ResultSet,
50+
idx: Int
51+
): Unit = {
52+
r.updateObject(idx, v)
53+
}
54+
}
55+
}
56+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.scala.slick_pg.entity
2+
3+
import org.json4s.JValue
4+
5+
import java.time.OffsetDateTime
6+
7+
case class BaeldungEntity(
8+
id: Long,
9+
createdAt: OffsetDateTime,
10+
prices: List[Double],
11+
metadata: JValue
12+
)

0 commit comments

Comments
 (0)