Skip to content

Instantly share code, notes, and snippets.

@lucasrpb
Last active January 21, 2022 01:43
Show Gist options
  • Save lucasrpb/d47aeb8818346dbdd981dd83094a0397 to your computer and use it in GitHub Desktop.
Save lucasrpb/d47aeb8818346dbdd981dd83094a0397 to your computer and use it in GitHub Desktop.
package feed
import MyPostgresProfile.api._
import com.zaxxer.hikari.HikariConfig
import org.apache.pulsar.shade.org.glassfish.jersey.message.internal.DataSourceProvider
import org.postgresql.ds.PGSimpleDataSource
import org.slf4j.LoggerFactory
import slick.dbio.DBIO
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.{Duration, DurationInt}
import scala.language.postfixOps
import scala.util.{Failure, Success}
object SlickDemo {
import play.api.libs.json.{Format, Json}
import java.util.UUID
case class User(id: UUID = UUID.randomUUID(),
username: String,
password: String,
email: String)
object User {
implicit val userFormat = Json.using[Json.WithDefaultValues].format[User]
}
// Definition of the SUPPLIERS table
class UserTable(tag: Tag) extends Table[User](tag, "users") {
def id = column[UUID]("id", O.PrimaryKey) // This is the primary key column
def username = column[String]("username", O.Unique)
def password = column[String]("password")
def email = column[String]("email", O.Unique)
// Every table needs a * projection with the same type as the table's type parameter
def * = (id, username, password, email) <> ((User.apply _).tupled, User.unapply)
}
object UserTable {
val users = TableQuery[UserTable]
}
def main(args: Array[String]): Unit = {
val logger = LoggerFactory.getLogger(this.getClass)
val setup = DBIO.seq(
// Create the tables, including primary and foreign keys
UserTable.users.schema.dropIfExists,
UserTable.users.schema.createIfNotExists,
// Insert some suppliers
UserTable.users += User(UUID.randomUUID(), "lucasrpb", "1234", "[email protected]")
)
val sds = new PGSimpleDataSource()
sds.setURL("jdbc:postgresql://33fbc0f7-1c37-4a28-bd99-7e39e41ee965.gcp.ybdb.io:5433/postgres?ssl=true&sslmode=verify-full&sslrootcert=./root.crt")
sds.setUser("admin")
sds.setPassword("123")
val db = Database.forDataSource(sds, None)
val task = for {
res <- db.run(setup)
} yield Future.successful(db.close())
logger.debug(Await.result(task, Duration.Inf).toString)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment