Skip to content

Commit

Permalink
Database setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCLewis committed Mar 28, 2020
1 parent 442747b commit 60239e6
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 4 deletions.
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ lazy val server = (project in file("server")).settings(commonSettings).settings(
"org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0" % Test,
"com.typesafe.play" %% "play-slick" % "5.0.0",
"com.typesafe.slick" %% "slick-codegen" % "3.3.2",
"com.typesafe.play" %% "play-json" % "2.8.1",
"com.typesafe.play" %% "play-json" % "2.8.1",
"org.postgresql" % "postgresql" % "42.2.11",
"com.typesafe.slick" %% "slick-hikaricp" % "3.3.2",
"org.mindrot" % "jbcrypt" % "0.4",
specs2 % Test
),
// Compile the project before generating Eclipse files, so that generated .scala or .class files for views and routes are present
Expand Down
2 changes: 1 addition & 1 deletion project/metals.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// DO NOT EDIT! This file is auto-generated.
// This file enables sbt-bloop to create bloop config files.

addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.4.0-RC1")
addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.4.0-RC1-105-118a551b")
24 changes: 24 additions & 0 deletions server/app/controllers/TaskList5.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package controllers

import javax.inject._

import play.api.mvc._
import play.api.i18n._
import models.TaskListInMemoryModel
import play.api.libs.json._
import models._

import play.api.db.slick.DatabaseConfigProvider
import scala.concurrent.ExecutionContext
import play.api.db.slick.HasDatabaseConfigProvider
import slick.jdbc.JdbcProfile
import slick.jdbc.PostgresProfile.api._

@Singleton
class TaskList5 @Inject() (protected val dbConfigProvider: DatabaseConfigProvider, cc: ControllerComponents)(implicit ec: ExecutionContext)
extends AbstractController(cc) with HasDatabaseConfigProvider[JdbcProfile] {

def load = Action { implicit request =>
Ok(views.html.version4Main())
}
}
11 changes: 11 additions & 0 deletions server/app/models/CodeGen.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

object CodeGen extends App {
slick.codegen.SourceCodeGenerator.run(
"slick.jdbc.PostgresProfile",
"org.postgresql.Driver",
"jdbc:postgresql://localhost/tasklist?user=mlewis&password=password",
"/home/mlewis/PlayVideos/play-videos/server/app/",
"models", None, None, true, false
)
}
75 changes: 75 additions & 0 deletions server/app/models/Tables.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package models
// AUTO-GENERATED Slick data model
/** Stand-alone Slick data model for immediate use */
object Tables extends {
val profile = slick.jdbc.PostgresProfile
} with Tables

/** Slick data model trait for extension, choice of backend or usage in the cake pattern. (Make sure to initialize this late.) */
trait Tables {
val profile: slick.jdbc.JdbcProfile
import profile.api._
import slick.model.ForeignKeyAction
// NOTE: GetResult mappers for plain SQL are only generated for tables where Slick knows how to map the types of all columns.
import slick.jdbc.{GetResult => GR}

/** DDL for all tables. Call .create to execute. */
lazy val schema: profile.SchemaDescription = Items.schema ++ Users.schema
@deprecated("Use .schema instead of .ddl", "3.0")
def ddl = schema

/** Entity class storing rows of table Items
* @param itemId Database column item_id SqlType(serial), AutoInc, PrimaryKey
* @param userId Database column user_id SqlType(int4), Default(None)
* @param text Database column text SqlType(varchar), Length(2000,true), Default(None) */
case class ItemsRow(itemId: Int, userId: Option[Int] = None, text: Option[String] = None)
/** GetResult implicit for fetching ItemsRow objects using plain SQL queries */
implicit def GetResultItemsRow(implicit e0: GR[Int], e1: GR[Option[Int]], e2: GR[Option[String]]): GR[ItemsRow] = GR{
prs => import prs._
ItemsRow.tupled((<<[Int], <<?[Int], <<?[String]))
}
/** Table description of table items. Objects of this class serve as prototypes for rows in queries. */
class Items(_tableTag: Tag) extends profile.api.Table[ItemsRow](_tableTag, "items") {
def * = (itemId, userId, text) <> (ItemsRow.tupled, ItemsRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = ((Rep.Some(itemId), userId, text)).shaped.<>({r=>import r._; _1.map(_=> ItemsRow.tupled((_1.get, _2, _3)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))

/** Database column item_id SqlType(serial), AutoInc, PrimaryKey */
val itemId: Rep[Int] = column[Int]("item_id", O.AutoInc, O.PrimaryKey)
/** Database column user_id SqlType(int4), Default(None) */
val userId: Rep[Option[Int]] = column[Option[Int]]("user_id", O.Default(None))
/** Database column text SqlType(varchar), Length(2000,true), Default(None) */
val text: Rep[Option[String]] = column[Option[String]]("text", O.Length(2000,varying=true), O.Default(None))

/** Foreign key referencing Users (database name items_user_id_fkey) */
lazy val usersFk = foreignKey("items_user_id_fkey", userId, Users)(r => Rep.Some(r.id), onUpdate=ForeignKeyAction.NoAction, onDelete=ForeignKeyAction.Cascade)
}
/** Collection-like TableQuery object for table Items */
lazy val Items = new TableQuery(tag => new Items(tag))

/** Entity class storing rows of table Users
* @param id Database column id SqlType(serial), AutoInc, PrimaryKey
* @param username Database column username SqlType(varchar), Length(20,true)
* @param password Database column password SqlType(varchar), Length(200,true) */
case class UsersRow(id: Int, username: String, password: String)
/** GetResult implicit for fetching UsersRow objects using plain SQL queries */
implicit def GetResultUsersRow(implicit e0: GR[Int], e1: GR[String]): GR[UsersRow] = GR{
prs => import prs._
UsersRow.tupled((<<[Int], <<[String], <<[String]))
}
/** Table description of table users. Objects of this class serve as prototypes for rows in queries. */
class Users(_tableTag: Tag) extends profile.api.Table[UsersRow](_tableTag, "users") {
def * = (id, username, password) <> (UsersRow.tupled, UsersRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = ((Rep.Some(id), Rep.Some(username), Rep.Some(password))).shaped.<>({r=>import r._; _1.map(_=> UsersRow.tupled((_1.get, _2.get, _3.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))

/** Database column id SqlType(serial), AutoInc, PrimaryKey */
val id: Rep[Int] = column[Int]("id", O.AutoInc, O.PrimaryKey)
/** Database column username SqlType(varchar), Length(20,true) */
val username: Rep[String] = column[String]("username", O.Length(20,varying=true))
/** Database column password SqlType(varchar), Length(200,true) */
val password: Rep[String] = column[String]("password", O.Length(200,varying=true))
}
/** Collection-like TableQuery object for table Users */
lazy val Users = new TableQuery(tag => new Users(tag))
}
9 changes: 9 additions & 0 deletions server/conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,12 @@ play.assets {
urlPrefix = "/assets"
}

## Slick database configuration.
slick.dbs.default = {
db.url = "jdbc:postgresql://localhost/tasklist?user=mlewis&password=password"
db.url = ${?JDBC_DATABASE_URL}
db.driver = "org.postgresql.Driver"
profile = "slick.jdbc.PostgresProfile$"
# connectionPool = disabled
# keepAliveConnection = true
}
4 changes: 2 additions & 2 deletions sql/createTables.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
create table users (
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username varchar(20) NOT NULL,
password varchar(200) NOT NULL
);

create table items (
CREATE TABLE items (
item_id SERIAL PRIMARY KEY,
user_id int4 REFERENCES users(id) ON DELETE CASCADE,
text varchar(2000)
Expand Down
3 changes: 3 additions & 0 deletions sql/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE USER mlewis WITH PASSWORD 'password';

CREATE DATABASE tasklist WITH OWNER=mlewis;

0 comments on commit 60239e6

Please sign in to comment.