diff options
author | vlad <vlad@drivergrp.com> | 2016-11-09 13:51:44 -0800 |
---|---|---|
committer | vlad <vlad@drivergrp.com> | 2016-11-09 13:51:44 -0800 |
commit | e57e93f1c36adca193f682107c1b465c32253312 (patch) | |
tree | 84f29669655667fe13c164b966699d199c6c0c37 /src/main | |
parent | 3c6b272033341c38b2cb1852a7b48ea7befb7002 (diff) | |
download | driver-core-0.9.13.tar.gz driver-core-0.9.13.tar.bz2 driver-core-0.9.13.zip |
Database standard table creation methodv0.9.13
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/scala/xyz/driver/core/database.scala | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/main/scala/xyz/driver/core/database.scala b/src/main/scala/xyz/driver/core/database.scala index 45d1b6d..0ebcd0d 100644 --- a/src/main/scala/xyz/driver/core/database.scala +++ b/src/main/scala/xyz/driver/core/database.scala @@ -1,10 +1,12 @@ package xyz.driver.core import slick.backend.DatabaseConfig +import slick.dbio.{DBIOAction, NoStream} import slick.driver.JdbcProfile import xyz.driver.core.time.Time -import scala.concurrent.Future +import scala.concurrent.{ExecutionContext, Future} +import scala.language.reflectiveCalls object database { @@ -25,6 +27,11 @@ object database { } } + type Schema = { + def create: DBIOAction[Unit, NoStream, slick.dbio.Effect.Schema] + def drop: DBIOAction[Unit, NoStream, slick.dbio.Effect.Schema] + } + trait IdColumnTypes { val database: Database @@ -41,8 +48,20 @@ object database { trait DatabaseObject extends IdColumnTypes { + implicit val exec: ExecutionContext + def createTables(): Future[Unit] def disconnect(): Unit + + def ensureTableExist(schemas: Seq[Schema]): Future[Unit] = + for { + dropping <- Future.sequence(schemas.map { schema => + database.database.run(schema.drop).recover { case _: Throwable => () } + }) + creation <- Future.sequence(schemas.map { schema => + database.database.run(schema.create).recover { case _: Throwable => () } + }) + } yield () } abstract class DatabaseObjectAdapter extends DatabaseObject { |