Skip to content

Commit

Permalink
_slick30:0.3.0: Revert API design
Browse files Browse the repository at this point in the history
  • Loading branch information
nafg committed Aug 3, 2016
1 parent f497407 commit b35dd04
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 113 deletions.
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -2,11 +2,11 @@
[![Coverage Status](https://img.shields.io/coveralls/nafg/slick-migration-api.svg)](https://coveralls.io/r/nafg/slick-migration-api?branch=master)
[![Download](https://api.bintray.com/packages/naftoligug/maven/slick-migration-api/images/download.svg) ](https://bintray.com/naftoligug/maven/slick-migration-api/_latestVersion)

| Slick version | SBT dependency |
|---------------|-------------------------------------------------------|
| 3.1.1 | `"io.github.nafg" %% "slick-migration-api" % "0.3.0"` |
| 3.0.3 | `"io.github.nafg" %% "slick-migration-api" % "0.2.0"` |
| 2.1.0 | `"io.github.nafg" %% "slick-migration-api" % "0.1.1"` |
| Slick version | SBT dependency |
|---------------|---------------------------------------------------------------|
| 3.1.1 | `"io.github.nafg" %% "slick-migration-api" % "0.3.0"` |
| 3.0.3 | `"io.github.nafg" %% "slick-migration-api_slick30" % "0.3.0"` |
| 2.1.0 | `"io.github.nafg" %% "slick-migration-api" % "0.1.1"` |

A library for defining database migrations, for use with Slick,
including a DSL to define type safe and typo safe table migrations
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Expand Up @@ -2,7 +2,7 @@ crossScalaVersions := Seq("2.11.6", "2.10.5")

organization := "io.github.nafg"

name := "slick-migration-api"
name := "slick-migration-api_slick30"

libraryDependencies += "com.typesafe.slick" %% "slick" % "3.0.3"

Expand Down
62 changes: 22 additions & 40 deletions src/main/scala/slick/migration/api/Migration.scala
@@ -1,19 +1,15 @@
package slick
package migration.api

import slick.dbio.{ FailureAction, SuccessAction, DBIO }
import slick.jdbc.JdbcBackend
import slick.profile.SqlAction

import scala.concurrent.{ ExecutionContext, Future }
import slick.dbio._
import slick.jdbc.SimpleJdbcAction

/**
* The base of the migration type hierarchy.
* Can contain any operation that can use an implicit `Session`.
* Produces a DBIO that runs the migration
*/
trait Migration {
def run(db: JdbcBackend#Database)(implicit ec: ExecutionContext): Future[Unit]
def statements: Seq[String] = Seq("-- No-SQL action --")
def apply(): DBIO[Unit]
}

object Migration {
Expand All @@ -35,12 +31,8 @@ object Migration {
def &[N <: Migration, O](n: N)(implicit ccm: CanConcatMigrations[M, N, O]): O = ccm.f(m, n)
}

def failure: Migration = new SqlMigration {
override def actions: Seq[DBIO[_]] = Seq(FailureAction(new Exception("Aborted.")))
}

def successful: Migration = new SqlMigration {
override val actions: Seq[DBIO[_]] = Seq(SuccessAction(()))
def empty: Migration = new Migration {
override def apply() = DBIO.successful(())
}
}

Expand Down Expand Up @@ -79,13 +71,7 @@ object CanConcatMigrations extends CanConcatMigrationsLow {
* Holds a sequence of [[Migration]]s and performs them one after the other.
*/
case class MigrationSeq(migrations: Migration*) extends Migration {

final override def run(db: JdbcBackend#Database)(implicit ec: ExecutionContext): Future[Unit] =
(migrations :+ Migration.successful).foldLeft(Future.successful(())){ case(f, m) =>
f.flatMap(_ => m.run(db))
}

final override def statements: Seq[String] = migrations.flatMap(_.statements)
final def apply() = DBIO.seq(migrations.map(_()): _*)
}

/**
Expand All @@ -101,27 +87,23 @@ class ReversibleMigrationSeq(override val migrations: ReversibleMigration*) exte

/**
* A [[Migration]] defined in terms of SQL commands.
* This trait implements `run` and instead defines an
* abstract `actions` method.
* This trait implements `apply` and instead defines an
* abstract [[sql]] method.
*/
trait SqlMigration extends Migration {
/**
* The SQL statements to run
*/
def actions: Seq[DBIO[_]]

def seq: DBIO[Unit] = DBIO.seq(actions: _*)

override def statements: Seq[String] = actions.collect {
case sql: SqlAction[_, _, _] => sql.statements
case _ => super.statements
}.flatten

/**
* Runs all the SQL statements in a single transaction
*/
override def run(db: JdbcBackend#Database)(implicit ec: ExecutionContext): Future[Unit] =
db.run(DBIO.seq(actions: _*))
def sql: Seq[String]

def apply() = SimpleJdbcAction { ctx =>
for(str <- sql)
try ctx.session.withPreparedStatement(str)(_.execute())
catch {
case e: java.sql.SQLException =>
throw MigrationException(s"Could not execute sql: '$str'", e)
}
}
}

//TODO mechanism other than exceptions?
Expand All @@ -132,10 +114,10 @@ case class MigrationException(message: String, cause: Throwable) extends Runtime
* @example {{{ SqlMigration("drop table t1", "update t2 set x=10 where y=20") }}}
*/
object SqlMigration {
def apply(actions: DBIO[_]*) = {
def actions0 = actions
def apply(sql: String*) = {
def sql0 = sql
new SqlMigration {
override val actions: Seq[DBIO[_]] = actions0
override val sql = sql0
}
}
}
15 changes: 3 additions & 12 deletions src/main/scala/slick/migration/api/TableMigration.scala
Expand Up @@ -2,13 +2,9 @@ package slick
package migration.api

import slick.ast.FieldSymbol
import slick.dbio.DBIO
import slick.driver.JdbcDriver
import slick.jdbc.{ SQLActionBuilder, SetParameter }
import slick.jdbc.GetResult._
import slick.lifted.{ AbstractTable, Rep, ForeignKey, ForeignKeyQuery, Index, PrimaryKey, TableQuery }

import AstHelpers._
import slick.lifted._
import slick.migration.api.AstHelpers._

/**
* Internal data structure that stores schema manipulation operations to be performed on a table
Expand Down Expand Up @@ -128,12 +124,7 @@ sealed abstract class TableMigration[T <: JdbcDriver#Table[_]](table: T)(implici

def tableInfo = TableInfo(table.schemaName, table.tableName)

/**
* The SQL statements to run
*/
override def actions: Seq[DBIO[_]] = dialect.migrateTable(tableInfo, data).map { statement =>
new SQLActionBuilder(Seq(statement), SetParameter.SetUnit).as[Int]
}
def sql = dialect.migrateTable(tableInfo, data)

protected[api] def data: TableMigrationData

Expand Down

0 comments on commit b35dd04

Please sign in to comment.