Created
July 31, 2018 09:06
-
-
Save ivantopo/5a21691e8af4b7a4d3f915e0b8833790 to your computer and use it in GitHub Desktop.
Creating a schema with Slick + Kamon
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
import kamon.Kamon | |
import slick.jdbc.PostgresProfile.api._ | |
import scala.concurrent.ExecutionContext | |
object SlickExample extends App { | |
// Definition of the SUPPLIERS table | |
class Suppliers(tag: Tag) extends Table[(Int, String, String, String, String, String)](tag, "SUPPLIERS") { | |
def id = column[Int]("SUP_ID", O.PrimaryKey) // This is the primary key column | |
def name = column[String]("SUP_NAME") | |
def street = column[String]("STREET") | |
def city = column[String]("CITY") | |
def state = column[String]("STATE") | |
def zip = column[String]("ZIP") | |
// Every table needs a * projection with the same type as the table's type parameter | |
def * = (id, name, street, city, state, zip) | |
} | |
val suppliers = TableQuery[Suppliers] | |
// Definition of the COFFEES table | |
class Coffees(tag: Tag) extends Table[(String, Int, Double, Int, Int)](tag, "COFFEES") { | |
def name = column[String]("COF_NAME", O.PrimaryKey) | |
def supID = column[Int]("SUP_ID") | |
def price = column[Double]("PRICE") | |
def sales = column[Int]("SALES") | |
def total = column[Int]("TOTAL") | |
def * = (name, supID, price, sales, total) | |
// A reified foreign key relation that can be navigated to create a join | |
def supplier = foreignKey("SUP_FK", supID, suppliers)(_.id) | |
} | |
val coffees = TableQuery[Coffees] | |
class AsyncExecutorContextAware(underlying: AsyncExecutor) extends AsyncExecutor { | |
override def executionContext: ExecutionContext = new ExecutionContextAware(underlying.executionContext) | |
override def close(): Unit = underlying.close() | |
} | |
class ExecutionContextAware(underlying: ExecutionContext) extends ExecutionContext { | |
override def execute(runnable: Runnable): Unit = underlying.execute(new RunnableContextAware(runnable)) | |
override def reportFailure(cause: Throwable): Unit = underlying.reportFailure(cause) | |
} | |
class RunnableContextAware(underlying: Runnable) extends Runnable { | |
private val traceContext = Kamon.currentContext | |
override def run(): Unit = | |
Kamon.withContext(traceContext) { | |
underlying.run() | |
} | |
} | |
val db: Database = Database.forURL( | |
url = "jdbc:postgresql://localhost/postgres?user=postgres&password=mysecretpassword", | |
user = "postgres", | |
password = "mysecretpassword", | |
driver = "org.postgresql.Driver", | |
executor = new AsyncExecutorContextAware(AsyncExecutor.default()) | |
) | |
db.run((suppliers.schema ++ coffees.schema).create) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment