From 15b0916cc11b82492e559d6441a719cb0306cd6b Mon Sep 17 00:00:00 2001 From: Naftoli Gugenheim <98384+nafg@users.noreply.github.com> Date: Sun, 29 Sep 2024 04:49:50 -0400 Subject: [PATCH] Upgrade Scala to 2.13 with -Xsource:3 and add scalafmt --- .github/workflows/ci.yml | 2 +- .mergify.yml | 2 +- .scalafmt.conf | 4 ++ build.sbt | 18 ++++++--- runAll.sbt | 6 +-- src/main/scala/Transfer.scala | 15 -------- .../examples/plainsql}/Interpolation.scala | 35 +++++++++++------- .../examples/plainsql}/PlainSQL.scala | 16 +++++--- .../slick/examples/plainsql/Transfer.scala | 37 +++++++++++++++++++ 9 files changed, 89 insertions(+), 46 deletions(-) create mode 100644 .scalafmt.conf delete mode 100644 src/main/scala/Transfer.scala rename src/main/scala/{ => slick/examples/plainsql}/Interpolation.scala (85%) rename src/main/scala/{ => slick/examples/plainsql}/PlainSQL.scala (80%) create mode 100644 src/main/scala/slick/examples/plainsql/Transfer.scala diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6ee35bd..334b95f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - scala: [2.12.19] + scala: [2.13.15] java: [temurin@11] runs-on: ${{ matrix.os }} steps: diff --git a/.mergify.yml b/.mergify.yml index 9236fe1..88aa524 100644 --- a/.mergify.yml +++ b/.mergify.yml @@ -9,7 +9,7 @@ pull_request_rules: - author=scala-steward - author=slick-scala-steward[bot] - author=renovate[bot] - - check-success=Build and Test (ubuntu-latest, 2.12.19, temurin@11) + - check-success=Build and Test (ubuntu-latest, 2.13.15, temurin@11) actions: queue: name: default diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..41b050e --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,4 @@ +version = 3.8.3 + +runner.dialect = Scala213Source3 +align.preset = most diff --git a/build.sbt b/build.sbt index b7c3e50..4199488 100644 --- a/build.sbt +++ b/build.sbt @@ -1,5 +1,11 @@ import _root_.io.github.nafg.mergify.dsl.* +inThisBuild( + List( + scalaVersion := "2.13.15", + scalacOptions += "-Xsource:3" + ) +) mergifyExtraConditions := Seq( (Attr.Author :== "scala-steward") || @@ -7,8 +13,8 @@ mergifyExtraConditions := Seq( (Attr.Author :== "renovate[bot]") ) libraryDependencies ++= List( - "org.slf4j" % "slf4j-nop" % "2.0.16", - "com.h2database" % "h2" % "2.3.232" + "org.slf4j" % "slf4j-nop" % "2.0.16", + "com.h2database" % "h2" % "2.3.232" ) scalacOptions += "-deprecation" @@ -20,8 +26,8 @@ libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value Compile / unmanagedClasspath ++= (Compile / unmanagedResources).value libraryDependencies += "com.typesafe.slick" %% "slick" % "3.5.1" -ThisBuild / githubWorkflowJavaVersions := Seq(JavaSpec.temurin("11")) - -ThisBuild / githubWorkflowBuild := Seq(WorkflowStep.Sbt(List("runAll"), name = Some(s"Run all main classes"))) - +ThisBuild / githubWorkflowJavaVersions := Seq(JavaSpec.temurin("11")) ThisBuild / githubWorkflowPublishTargetBranches := Seq() +ThisBuild / githubWorkflowBuild := Seq( + WorkflowStep.Sbt(List("runAll"), name = Some(s"Run all main classes")) +) diff --git a/runAll.sbt b/runAll.sbt index 005f97d..64c90f9 100644 --- a/runAll.sbt +++ b/runAll.sbt @@ -2,9 +2,9 @@ lazy val runAll = taskKey[Unit]("Run all main classes") def runAllIn(config: Configuration) = Def.task { - val s = streams.value - val cp = (config / fullClasspath).value - val r = (config / run / runner).value + val s = streams.value + val cp = (config / fullClasspath).value + val r = (config / run / runner).value val classes = (config / discoveredMainClasses).value classes.foreach { className => r.run(className, cp.files, Seq(), s.log).get diff --git a/src/main/scala/Transfer.scala b/src/main/scala/Transfer.scala deleted file mode 100644 index 07a5c6b..0000000 --- a/src/main/scala/Transfer.scala +++ /dev/null @@ -1,15 +0,0 @@ -import slick.jdbc.H2Profile.api._ -import slick.jdbc.GetResult - -/** The Data Transfer Objects for the PlainSQL app */ -trait Transfer { this: PlainSQL.type => - - // Case classes for our data - case class Supplier(id: Int, name: String, street: String, city: String, state: String, zip: String) - case class Coffee(name: String, supID: Int, price: Double, sales: Int, total: Int) - - // Result set getters - implicit val getSupplierResult = GetResult(r => Supplier(r.nextInt(), r.nextString(), r.nextString(), - r.nextString(), r.nextString(), r.nextString())) - implicit val getCoffeeResult = GetResult(r => Coffee(r.<<, r.<<, r.<<, r.<<, r.<<)) -} diff --git a/src/main/scala/Interpolation.scala b/src/main/scala/slick/examples/plainsql/Interpolation.scala similarity index 85% rename from src/main/scala/Interpolation.scala rename to src/main/scala/slick/examples/plainsql/Interpolation.scala index 03a653e..d199690 100644 --- a/src/main/scala/Interpolation.scala +++ b/src/main/scala/slick/examples/plainsql/Interpolation.scala @@ -1,17 +1,11 @@ -import slick.jdbc.H2Profile.api._ +package slick.examples.plainsql import scala.concurrent.ExecutionContext.Implicits.global -trait Interpolation { this: PlainSQL.type => +import slick.jdbc.H2Profile.api.* - def createCoffees: DBIO[Int] = - sqlu"""create table coffees( - name varchar not null, - sup_id int not null, - price double not null, - sales int not null, - total int not null, - foreign key(sup_id) references suppliers(id))""" +//noinspection MutatorLikeMethodIsParameterless +trait Interpolation { this: PlainSQL.type => def createSuppliers: DBIO[Int] = sqlu"""create table suppliers( @@ -22,6 +16,15 @@ trait Interpolation { this: PlainSQL.type => state varchar not null, zip varchar not null)""" + def createCoffees: DBIO[Int] = + sqlu"""create table coffees( + name varchar not null, + sup_id int not null, + price double not null, + sales int not null, + total int not null, + foreign key(sup_id) references suppliers(id))""" + def insertSuppliers: DBIO[Unit] = DBIO.seq( // Insert some suppliers sqlu"insert into suppliers values(101, 'Acme, Inc.', '99 Market Street', 'Groundsville', 'CA', '95199')", @@ -51,16 +54,20 @@ trait Interpolation { this: PlainSQL.type => // Iterate through all coffees and output them sql"select * from coffees".as[Coffee].map { cs => println("Coffees:") - for(c <- cs) - println("* " + c.name + "\t" + c.supID + "\t" + c.price + "\t" + c.sales + "\t" + c.total) + for (c <- cs) + println( + "* " + c.name + "\t" + c.supID + "\t" + c.price + "\t" + c.sales + "\t" + c.total + ) } - def namesByPrice(price: Double): DBIO[Seq[(String, String)]] = sql""" + // noinspection SameParameterValue + private def namesByPrice(price: Double): DBIO[Seq[(String, String)]] = sql""" select c.name, s.name from coffees c, suppliers s where c.price < $price and s.id = c.sup_id""".as[(String, String)] - def supplierById(id: Int): DBIO[Seq[Supplier]] = + // noinspection SameParameterValue + private def supplierById(id: Int): DBIO[Seq[Supplier]] = sql"select * from suppliers where id = $id".as[Supplier] def printParameterized: DBIO[Unit] = { diff --git a/src/main/scala/PlainSQL.scala b/src/main/scala/slick/examples/plainsql/PlainSQL.scala similarity index 80% rename from src/main/scala/PlainSQL.scala rename to src/main/scala/slick/examples/plainsql/PlainSQL.scala index 5ecfddf..b37cf12 100644 --- a/src/main/scala/PlainSQL.scala +++ b/src/main/scala/slick/examples/plainsql/PlainSQL.scala @@ -1,15 +1,19 @@ -import scala.concurrent.{Future, Await} +package slick.examples.plainsql + import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration -import slick.jdbc.H2Profile.api._ +import scala.concurrent.{Await, Future} + +import slick.jdbc.H2Profile.api.* -/** A simple example that uses plain SQL queries against an in-memory - * H2 database. The example data comes from Oracle's JDBC tutorial at - * http://download.oracle.com/javase/tutorial/jdbc/basics/tables.html. */ +/** A simple example that uses plain SQL queries against an in-memory H2 + * database. The example data comes from Oracle's JDBC tutorial at + * http://download.oracle.com/javase/tutorial/jdbc/basics/tables.html. + */ object PlainSQL extends App with Interpolation with Transfer { val db = Database.forConfig("h2mem1") try { - val f: Future[_] = { + val f: Future[?] = { val a: DBIO[Unit] = DBIO.seq( createSuppliers, diff --git a/src/main/scala/slick/examples/plainsql/Transfer.scala b/src/main/scala/slick/examples/plainsql/Transfer.scala new file mode 100644 index 0000000..f02861b --- /dev/null +++ b/src/main/scala/slick/examples/plainsql/Transfer.scala @@ -0,0 +1,37 @@ +package slick.examples.plainsql +import slick.jdbc.GetResult + +/** The Data Transfer Objects for the PlainSQL app */ +trait Transfer { this: PlainSQL.type => + + // Case classes for our data + case class Supplier( + id: Int, + name: String, + street: String, + city: String, + state: String, + zip: String + ) + case class Coffee( + name: String, + supID: Int, + price: Double, + sales: Int, + total: Int + ) + + // Result set getters + implicit val getSupplierResult: GetResult[Supplier] = GetResult(r => + Supplier( + r.nextInt(), + r.nextString(), + r.nextString(), + r.nextString(), + r.nextString(), + r.nextString() + ) + ) + implicit val getCoffeeResult: GetResult[Coffee] = + GetResult(r => Coffee(r.<<, r.<<, r.<<, r.<<, r.<<)) +}