0
votes

Recently trying to migrate to the latest phantom version 2.24.8. I created a dummy project, but running into a few issues that I can't figure out. Here's my code:

import com.outworkers.phantom.connectors.{CassandraConnection, ContactPoints}
import com.outworkers.phantom.database.Database

import scala.concurrent.Future
import com.outworkers.phantom.dsl._

case class Test(id: String, timestamp: String)

abstract class Tests extends Table[Tests, Test] {

  object id extends StringColumn with PartitionKey

  object timestamp extends StringColumn with ClusteringOrder
}

abstract class ConcreteTests extends Tests with RootConnector {
  def addTest(l: Test): Future[ResultSet] = {
    //    store(l).consistencyLevel_=(ConsistencyLevel.LOCAL_ONE).future
    insert.value(_.id, l.id)
      .value(_.timestamp, l.timestamp)
      .consistencyLevel_=(ConsistencyLevel.QUORUM).future
  }

}

class MyDB(override val connector: CassandraConnection) extends Database[MyDB](connector) {

  object tests extends ConcreteTests with connector.Connector

  def init(): Unit = {
    tests.create
  }
}

object Test{
  def main(args: Array[String]): Unit = {
    val db = new MyDB(ContactPoints(Seq("127.0.0.1")).keySpace("tests"))
    db.init
    db.tests.addTest(Test("1", "1323234234"))
    println("Done")
  }
}

It compiled and ran in IntelliJ and print out 'Done'. However, no table is ever created. Also no exceptions or warnings. It did nothing. I tried to stop the local cassandra database. The code throws the NoHostAvailableException. So it does try to connect the local database. What is the problem?

Another weird thing is that "com.typesafe.play" %% "play-json" % "2.6.9" is in my build.sbt. If I remove the library, the same code throws the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/reflect/runtime/package$
    at com.outworkers.phantom.column.AbstractColumn.com$outworkers$phantom$column$AbstractColumn$$_name(AbstractColumn.scala:55)
    at com.outworkers.phantom.column.AbstractColumn.com$outworkers$phantom$column$AbstractColumn$$_name$(AbstractColumn.scala:54)
    at com.outworkers.phantom.column.Column.com$outworkers$phantom$column$AbstractColumn$$_name$lzycompute(Column.scala:22)
    at com.outworkers.phantom.column.Column.com$outworkers$phantom$column$AbstractColumn$$_name(Column.scala:22)
    at com.outworkers.phantom.column.AbstractColumn.name(AbstractColumn.scala:58)
    at com.outworkers.phantom.column.AbstractColumn.name$(AbstractColumn.scala:58)
    at com.outworkers.phantom.column.Column.name(Column.scala:22)
    at com.outworkers.phantom.builder.query.InsertQuery.value(InsertQuery.scala:107)

Really cannot figure what's going on. Any help?

BTW, I'm using scala 2.12.6 and JVM 1.8.181.

1

1 Answers

0
votes

You're not using the correct DSL method for table creation, have a look at the official guide. All that table.create does is to create an empty CreateQuery, and you're coercing the return type to Unit manually.

The automated blocking create method is on Database, not on table, so what you want is:

class MyDB(override val connector: CassandraConnection) extends Database[MyDB](connector) {

  object tests extends ConcreteTests with connector.Connector

  def init(): Unit = {
    this.create
  }
}

If you want to achieve the same thing using table, you need:

class MyDB(override val connector: CassandraConnection) extends Database[MyDB](connector) {

  object tests extends ConcreteTests with connector.Connector

  def init(): Unit = {
    import scala.concurrent.duration._
    import scala.concurrent.Await

    Await.result(tests.create.future(), 10.seconds)
  }
}

It's only the call to future() method that will trigger any kind of action to the database, otherwise you're just building a query without executing it. The method name can be confusing, and we will improve the docs and future releases to make it more obvious.

The conflict with play 2.6.9 looks very weird, it's entirely possible there's an incompatible dependency behind the scenes to do with macro compilation. Raise that as a separate issue and we can definitely have a look at it.