Skip to content

Instantly share code, notes, and snippets.

@JCDClark
Created May 27, 2016 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JCDClark/b089f758743a22260346f276f76e759a to your computer and use it in GitHub Desktop.
Save JCDClark/b089f758743a22260346f276f76e759a to your computer and use it in GitHub Desktop.
Scala IDE "updating occurrence annotations" crashing file
package services
import scala.concurrent.Await
import scala.concurrent.Future
import javax.inject.Inject
import javax.inject.Singleton
import models.Environment
import play.api.db.slick.DatabaseConfigProvider
import play.api.db.slick.HasDatabaseConfigProvider
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.db.Database
import slick.driver.H2Driver.api._
import slick.driver.JdbcProfile
import slick.lifted.ForeignKeyQuery
import slick.lifted.ProvenShape
import scala.concurrent.duration._
import slick.jdbc.meta.MTable
import com.ibm.couchdb.CouchDb
import com.ibm.couchdb.TypeMapping
import com.ibm.couchdb.CouchDbApi
import com.ibm.couchdb.CouchView
import com.ibm.couchdb.CouchDesign
trait EnvironmentFetcher {
def getAll(): Future[Seq[Environment]]
}
@Singleton
class CannedEnvironmentFetcher extends EnvironmentFetcher {
val cannedEnvironmentsList = List(Environment("dev"), Environment("qa"), Environment("sit"))
override def getAll(): Future[Seq[Environment]] = {
return Future { cannedEnvironmentsList }
}
}
@Singleton
class SlickH2DatabaseEnvironmentFetcher @Inject() (protected val dbConfigProvider: DatabaseConfigProvider) extends EnvironmentFetcher with HasDatabaseConfigProvider[JdbcProfile] {
private val environments = TableQuery[EnvironmentsTable]
// Automatically create the schemas in the database when the Fetcher is
// first called to make sure the in-memory h2 database is ready for use.
Await.result(createTableIfNotExists(environments), Duration.Inf)
private def createTableIfNotExists(tables: TableQuery[_ <: Table[_]]*): Future[Seq[Unit]] = {
Future.sequence(
tables map { table =>
db.run(MTable.getTables(table.baseTableRow.tableName)).flatMap { result =>
if (result.isEmpty) {
db.run(table.schema.create)
} else {
Future.successful(())
}
}
})
}
override def getAll(): Future[Seq[Environment]] = {
db.run(environments.result)
}
}
// The Environments table for the H2 Database
class EnvironmentsTable(tag: Tag)
extends Table[Environment](tag, "ENVIRONMENTS") {
// This is the primary key column:
def id: Rep[Int] = column[Int]("ID", O.PrimaryKey)
def name: Rep[String] = column[String]("NAME")
// Every table needs a * projection with the same type as the table's type parameter
def * = (name) <> (Environment.apply, Environment.unapply)
}
@Singleton
class CouchDbEnvironmentFetcher extends EnvironmentFetcher {
private val dbName = "environments"
private val typeMappings = TypeMapping(classOf[Environment] -> "Environment");
private def getConnection(): CouchDbApi = {
val dbConnection = CouchDb("REDACTED", 80, false, "REDACTED", "REDACTED")
dbConnection.db("environments", typeMappings)
}
private def getView(viewName: String) = {
???
}
override def getAll(): Future[Seq[Environment]] = {
val dbConnection = getConnection()
val allEnvsQuery = dbConnection.docs.getMany.includeDocs[Environment].byType[String]("all-environments", "environments", typeMappings.get(classOf[Environment]).get).build.query.unsafePerformSync
Future { allEnvsQuery.getDocsData }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment