Play 2.6 evolutions DB change not applied - postgresql

My project was recently updated from Play 2.5 to 2.6.13.
I added a new script 16.sql but the change were not applied in the table play_evolutions
According to the documentation 2.6, the EvolutionsComponents have to be injected if you use compile time DI. But Guice is runtime DI, so I should not have to inject any components.
I enabled the evolutions in the build.sbt
libraryDependencies ++= Seq(evolutions, jdbc)
In application.conf
play.evolutions.enabled=true
play.evolutions.autoApply=true
What is my project missing ? Any git examples are welcome

I solved it by following the documentation since upgrading to Play 2.6
Here is my code
import play.api.ApplicationLoader.Context
import play.api.{Application, ApplicationLoader, BuiltInComponentsFromContext}
import play.api.db.{DBComponents, HikariCPComponents}
import play.api.db.evolutions.EvolutionsComponents
import play.api.routing.Router
import play.filters.HttpFiltersComponents
import router.Routes
class MyApplicationLoader extends ApplicationLoader {
def load(context: ApplicationLoader.Context): Application = {
new AppComponents(context).application
}
}
class AppComponents(context: Context)
extends BuiltInComponentsFromContext(context)
with DBComponents
with EvolutionsComponents
with HikariCPComponents
with HttpFiltersComponents {
// this will actually run the database migrations on startup
applicationEvolutions
// val prefix: String = "/"
lazy val router = Router.empty
// lazy val router: Router = bind(classOf[Routes]).to(classOf[Routes])
}
And in the conf/application.conf, add this line
play.application.loader=MyApplicationLoader

Related

How do I load configuration file on startup

I have application.conf file for my Scala program in main/resources. I load the configuration file using Config.load(). It is all working. But my config loading code is inside my service class. I want something like when program starts then first thing it doing is loading configuration. Then I want to use DI to pass this around.
This is easy in Scala Play because I just write Module class and specify in config file. Play then loads my config at start up. But how can I do this with non-Play project - just plain scala program.
Play uses Guice out-of-the-box for dependency injection, so you could consider using the same.
Here is an example of how guice can be used to inject configuration into services:
package example
import com.google.inject.{AbstractModule, Guice, Inject, Injector, Provides, Singleton}
import com.typesafe.config.ConfigFactory
import scala.jdk.CollectionConverters._
case class Config(foo: String, bar: String)
class Module extends AbstractModule {
#Provides
#Singleton
def getConfig: Config = {
val conf = ConfigFactory.load()
Config(
conf.getString("foo"),
conf.getString("bar")
)
}
}
class QuxService #Inject()(config: Config) {
println(config)
}
object ConfigInjectionExample extends App {
val injector: Injector = Guice.createInjector(List(new Module).asJava)
injector.getInstance(classOf[QuxService])
}
which outputs
Config(hello,world)
given the following resources/application.conf
foo="hello"
bar="world"
and the following dependencies
libraryDependencies += "net.codingwell" %% "scala-guice" % "4.2.6",
libraryDependencies += "com.typesafe" % "config" % "1.3.4"

Failed assertion with automated initialisation of database in Cassandra

Completely new to Cassandra. Tried to initialize a database in Cassandra using phantom-dsl. I received this error message.
*** RUN ABORTED ***
java.lang.AssertionError: assertion failed: no symbol could be loaded from class com.datastax.driver.core.Cluster in package core with name Cluster and classloader sun.misc.Launcher$AppClassLoader#279f2327
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$classToScala1(JavaMirrors.scala:1021)
at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToScala$1.apply(JavaMirrors.scala:980)
at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToScala$1.apply(JavaMirrors.scala:980)
at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$toScala$1.apply(JavaMirrors.scala:97)
at scala.reflect.runtime.TwoWayCaches$TwoWayCache$$anonfun$toScala$1.apply(TwoWayCaches.scala:39)
at scala.reflect.runtime.Gil$class.gilSynchronized(Gil.scala:19)
at scala.reflect.runtime.JavaUniverse.gilSynchronized(JavaUniverse.scala:16)
at scala.reflect.runtime.TwoWayCaches$TwoWayCache.toScala(TwoWayCaches.scala:34)
at scala.reflect.runtime.JavaMirrors$JavaMirror.toScala(JavaMirrors.scala:95)
at scala.reflect.runtime.JavaMirrors$JavaMirror.classToScala(JavaMirrors.scala:980)
I am not really sure whether it is an issue with the Connector in phantom-dsl or the ClusterBuilder in datastax-driver.
Connector.scala
package com.neruti.db
import com.neruti.db.models._
import com.websudos.phantom.database.Database
import com.websudos.phantom.connectors.ContactPoints
import com.websudos.phantom.dsl.KeySpaceDef
object Connector {
val host= Seq("localhost")
val port = 9160
val keySpace: String = "nrt_entities"
// val inet = InetAddress.getByName
lazy val connector = ContactPoints(host,port).withClusterBuilder(
_.withCredentials("cassandra", "cassandra")
).keySpace(keySpace)
}
CassandraSpec.scala
package com.neruti.db
import com.neruti.User
import com.neruti.db.models._
import com.neruti.db.databases._
import com.neruti.db.services._
import com.neruti.db.Connector._
import java.util.UUID
import com.datastax.driver.core.ResultSet
import org.scalatest._
import org.scalatest.{BeforeAndAfterAll,FlatSpec,Matchers,ShouldMatchers}
import org.scalatest.concurrent.ScalaFutures
import org.scalamock.scalatest.MockFactory
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
abstract class BaseCassandraSpec extends FlatSpec
with BeforeAndAfterAll
with Inspectors
with Matchers
with OptionValues
with ScalaFutures
class CassandraTest extends BaseCassandraSpec
with ProductionDatabase
with UserService
with Connector.connector.Connector{
val user = User(
Some("foobar"),
Some("foo#foobar.com"),
Some(UUID.randomUUID()),
)
override protected def beforeAll(): Unit = {
Await.result(database.userModel.create(user),10.seconds)
}
}
Looks there may be multiple issues you are looking at:
The latest version of phantom is 2.1.3, I'd strongly recommend using that, especially if you are just starting out. The migration guide is here in case you need it.
The entire reflection mechanism has been replaced in the latest version, so that error should magically go away. With respect to testing and generating objects, I would also look to include com.outworkers.util.testing, which is freely available on Maven Central
libraryDependencies ++= Seq(
//..,
"com.outworkers" %% "phantom-dsl" % "2.1.3",
"com.outworkers" %% "util-testing" % "0.30.1" % Test
)
This will offer you automated case class generation:
import com.outworkers.util.testing._
val sample = gen[User]

Cannot import class router.Routes in ApplicationLoader on Play 2.5

I'm having a problem with a migration to Play 2.5 with Scala. I had to start using DependencyInjection and after reading all the Play Framework 2.5 migration documentation and making all the corresponding implementations I arrived to an strange problem. Play indicates that class Routes should be automatically generated with the new DependencyInjection schema but when I tried to import the class in my custom ApplicationLoader, the compiler tells me that cannot resolve symbol "router". Below is part of my code, hope you can help me with this, thanks!
import controllers.Assets
import controllers.api.clients.ClientsController
import play.api.ApplicationLoader.Context
import play.api._
import play.api.libs.ws.ahc.AhcWSComponents
import router.Routes
class AppLoader extends ApplicationLoader {
def load(context: Context) = {
LoggerConfigurator(context.environment.classLoader).foreach {
_.configure(context.environment)
}
new AppComponents(context).application
}
}
class AppComponents(context: Context) extends BuiltInComponentsFromContext(context) with AhcWSComponents {
lazy val clientsController: ClientsController = new ClientsController(wsClient)
lazy val assets: Assets = new Assets(httpErrorHandler)
lazy val router = new Routes(
httpErrorHandler,
clientsController,
assets
)
}
Check the following:
Make sure your build.sbt contains routesGenerator := InjectedRoutesGenerator
Execute playCompileEverything in sbt and refresh your project in your IDE

scala Play Framework 2.4: sending email

I trying to send an email from scala Play framework 2.4 while using play-mailer, I have followed the instruction from their sample page but with no success.
I have added the dependency to build.sbt:
libraryDependencies ++= Seq(
"com.typesafe.play" %% "play-mailer" % "3.0.1"
)
In application.conf the I have added the following:
play.mailer {
host=smtp.gmail.com
port=465
ssl=true
tls=true
user="testme#gmail.com"
password=abracadabra
}
And finally, the Mailing Class:
package controllers
import java.io.File
import javax.inject.Inject
import org.apache.commons.mail.EmailAttachment
import play.api.Configuration
import play.api.Play.current
import play.api.libs.mailer._
class Mail(mailer: MailerClient) {
def send = {
val cid = "1234"
val email = Email(
"Simple email",
"Mister FROM <from#email.com>",
Seq("Miss TO <to#email.com>"),
bodyText = Some("A text message"),
bodyHtml = Some("some data....")
)
mailer.send(email)
}
}
So far without compilation errors, however I don't understand how to initialize this class.. how should I get the "MailerClient" instance?
In the documentation it is written "Then in your router definition, use the trait MailerComponents", with the following code example:
import play.api._
import play.api.ApplicationLoader.Context
import router.Routes
import play.api.libs.mailer._
class MyApplicationLoader extends ApplicationLoader {
def load(context: Context) = {
new ApplicationComponents(context).application
}
}
class ApplicationComponents(context: Context) extends BuiltInComponentsFromContext(context) with MailerComponents {
lazy val myComponent = new MyComponent(mailerClient)
// create your controllers here ...
lazy val router = new Routes(...) // inject your controllers here
}
(I have added "play.application.loader=SimpleApplicationLoader" in application.conf)
but I get the following compilation errors:
D:\myApp\app\SimpleApplicationLoader.scala:12: not found: type MailerComponents
[error] class ApplicationComponents(context: Context) extends BuiltInComponentsFromContext(context) with MailerComponents {
[error] ^
[error] D:\myApp\app\SimpleApplicationLoader.scala:13: not found: value mailerClient
[error] lazy val applicationController = new controllers.Mail(mailerClient)
[error] ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed
Any ideas?
You can go with the run-time dependency injection, as the other answer has suggested. But if you want to go with your current approach, read on...
The problem is, the MailerComponents trait doesn't exist in the 3.x branch. it does seem to exist in the master branch, but not in their next branch. I am not sure what they are doing there.
If you want to continue with the example, you'll need to do a bit of fiddling and figure out how to make it compile. For ex. with a bit of looking around, I came up with the following.
import play.api._
import play.api.ApplicationLoader.Context
import router.Routes
import play.api.libs.mailer._
class SimpleApplicationLoader extends ApplicationLoader {
def load(context: Context) = {
new ApplicationComponents(context).application
}
}
class ApplicationComponents(context: Context) extends BuiltInComponentsFromContext(context) {
val mailerClient = new CommonsMailer(configuration)
lazy val applicationController = new controllers.ApplicationScala(mailerClient)
lazy val assets = new controllers.Assets(httpErrorHandler)
lazy val router = new Routes(httpErrorHandler, applicationController)
}
Basically, instead of relying on the non-existent MailerComponent to create a mailerClient for me, I just did it myself.
If you have the following line in controllers.ApplicationScala
val id = mailer.configure(Configuration.from(Map("host" -> "typesafe.org", "port" -> 1234))).send(email)
Replace it with:
val id = mailer.send(email)
And it will compile. Meanwhile, I think I should raise an issue on github about this. And maybe you should.
I believe you could simply let play framework inject the mail client. Setting up the configuration and annotating your constructor with #Inject() should work.
Declaration of your Mail controller would look like this:
class Mail #Inject() (mailer: MailerClient) {

Where is GuiceApplicationBuilder?

I am currently trying to set up a Play2 application with dependency injection, with information from the official docs. However, my IDE cannot find GuiceApplicationBuilder. So which additional entry for libraryDependencies do I have to specify to get this builder?
EDIT: I made a screenshot of what happens when I try to import the things stated by #anquegi
EDIT 2: The problem was very simple: I used the wrong version of Play - 2.3.8 did in fact not have these, I had to use 2.4.0-RC2 to get it to work.
At the dependencies in the play docs /ScalaDependencyInjection you must import this
import play.api.ApplicationLoader
import play.api.Configuration
import play.api.inject._
import play.api.inject.guice._
This is the compile code in the section Advanced: Extending the GuiceApplicationLoader, so you need to use this extending in your class:
import play.api.ApplicationLoader
import play.api.Configuration
import play.api.inject._
import play.api.inject.guice._
class CustomApplicationLoader extends GuiceApplicationLoader() {
override def builder(context: ApplicationLoader.Context): GuiceApplicationBuilder = {
val extra = Configuration("a" -> 1)
initialBuilder
.in(context.environment)
.loadConfig(extra ++ context.initialConfiguration)
.overrides(overrides(context): _*)
}
}