Search code examples
scalaactiverecordconfigdatabase-schematypesafe

scala-activerecord: how to pass custom configuration to Tables.initialize?


I want to create tables with scala-activerecord:

Tables.initialize(ConfigFactory.load(env))

This does not work, because initialize accepts only Map[String, Any]. My second try was:

Tables.initialize(ConfigFactory.load(env).root())

where root() returns ConfigObject:

public interface ConfigObject extends ConfigValue, Map<String, ConfigValue>

I still get:

Error:(15, 49) type mismatch;
 found   : com.typesafe.config.ConfigObject
 required: Map[String,Any]
  Tables.initialize(ConfigFactory.load(env).root())
                                                ^

I don't get it, Any should accept any value, why it does not accept ConfigValue ?

How can I pass my config to the Tables.initialize method?


Solution

  • How can I pass my config to the Tables.initialize method?

    This might be a solution:

    import scala.collection.JavaConversions._
    Tables.initialize(ConfigFactory.load(env).root.unwrapped.toMap)
    

    For ActiveRecordTables#initialize method, it is assumed that you give the override settings and values as follows:

    Tables.initialize(Map(
      "driver" -> "org.postgresql.Driver",
      "jdbcurl" -> "jdbc:postgresql://hostname:5432/dbname"
    ))
    

    This feature is supposed to be used for applications such as temporarily overrides the value set (e.g. Coding tests).

    https://github.com/aselab/scala-activerecord/blob/281ae4073d1d2f4b04b31520c961f7210c5408d4/activerecord/src/test/scala/samples/AutoDdlSpec.scala#L34

    For database settings, please refer to the following:

    https://github.com/aselab/scala-activerecord/wiki/Database-Settings