有可能命名播放evolution sql脚本吗?

时间:2017-03-29 12:24:00

标签: sql playframework-2.4

evolution sql脚本应命名为

  • 1.sql,
  • 2.sql,
  • XXX.sql

根据文件。 https://www.playframework.com/documentation/2.5.x/Evolutions#Running-evolutions-using-compile-time-DI

是否有可能将命名约定更改为

  • 1_create__customers.sql
  • 2_create__orders.sql
  • 3_alter__customers__add__home_address.sql

喜欢它RoR: http://edgeguides.rubyonrails.org/active_record_migrations.html#creating-a-standalone-migration

是的,我评估了https://github.com/flyway/flyway-play 我们可以切换到它。

现在我正在寻找默认播放功能。

2 个答案:

答案 0 :(得分:4)

是的,这是可能的。

在此路径中创建新文件:your/modules/folder/EvolutionsModule.scala

然后,添加以下内容:

package your.modules.folder

import javax.inject._

import play.api.db.evolutions._
import play.api.inject.{ Module }
import play.api.libs.Collections
import play.api.{ Configuration, Environment }
import org.apache.commons.io.FileUtils

class EvolutionsModule extends Module {
  def bindings(environment: Environment, configuration: Configuration) = {
    Seq(
      bind[EvolutionsConfig].toProvider[DefaultEvolutionsConfigParser],
      bind[EvolutionsReader].to[YourEvolutionsReader],
      bind[EvolutionsApi].to[DefaultEvolutionsApi],
      bind[ApplicationEvolutions].toProvider[ApplicationEvolutionsProvider].eagerly
    )
  }
}

@Singleton
class YourEvolutionsReader @Inject()(environment: Environment) extends EvolutionsReader {

  def evolutions(db: String): Seq[Evolution] = {

    val upsMarker = """^#.*!Ups.*$""".r
    val downsMarker = """^#.*!Downs.*$""".r

    val UPS = "UPS"
    val DOWNS = "DOWNS"
    val UNKNOWN = "UNKNOWN"

    val mapUpsAndDowns: PartialFunction[String, String] = {
      case upsMarker() => UPS
      case downsMarker() => DOWNS
      case _ => UNKNOWN
    }

    val isMarker: PartialFunction[String, Boolean] = {
      case upsMarker() => true
      case downsMarker() => true
      case _ => false
    }

    val folder = environment.getFile(Evolutions.directoryName(db))
    val sqlFiles = folder.listFiles()
      .filter(file => file.getName.indexOf(".sql") > -1)
      .sortBy(file => {
        val fileName = file.getName
        val nameAfterSqlNumber = fileName.split("\\.")(0).split("_").drop(1).mkString("") + ".sql"
        val sqlNumber = fileName.split("\\.")(0).split("_")(0).toInt
        val newPrefix = "%07d".format(sqlNumber)
        newPrefix + nameAfterSqlNumber
      })
      .toSeq
    sqlFiles.zip(1 to sqlFiles.size)
      .map {
        case (file, revision) => {
          val script = FileUtils.readFileToString(file)
          val parsed = Collections.unfoldLeft(("", script.split('\n').toList.map(_.trim))) {
            case (_, Nil) => None
            case (context, lines) => {
              val (some, next) = lines.span(l => !isMarker(l))
              Some((next.headOption.map(c => (mapUpsAndDowns(c), next.tail)).getOrElse("" -> Nil),
                context -> some.mkString("\n")))
            }
          }.reverse.drop(1).groupBy(i => i._1).mapValues { _.map(_._2).mkString("\n").trim }
          Evolution(
            revision,
            parsed.getOrElse(UPS, ""),
            parsed.getOrElse(DOWNS, "")
          )
        }
      }
  }
}

转到applications.conf,然后添加这些行。这是用我们自己的替换默认的EvolutionsModule

play.modules {
    enabled += "your.modules.folder.EvolutionsModule"
    disabled += "play.api.db.evolutions.EvolutionsModule"
}

第一个文本块的说明:

  • 根据源代码,仅根据单个类和方法添加演变 - EvolutionsReader和方法evolutions
  • 运行evolutions方法后,evolutions模块将依赖于生成的数据库条目。所以,我们只需要用自己的方法覆盖这个方法。
  • 尽管存在巨大的代码块,但实际上它只是源代码的一个小编辑。我将loadResource函数的功能替换为evolutions sql files文件夹中的文件列表。其余部分是evolutions
  • EvolutionsApi.scala方法的直接副本

引用by this answer的源代码很可能是实用功能 - Evolutions模块实际上并不依赖它。我无法找到fileNameresourceName用于运行evolutions的重要用法。相反,他们都引用了play_evolutions表。

参考:https://github.com/playframework/playframework/issues/6919

答案 1 :(得分:0)

不,这是不可能的,源代码:

def fileName(db: String, revision: Int): String = s"${directoryName(db)}/${revision}.sql"

https://github.com/playframework/playframework/blob/2.5.x/framework/src/play-jdbc-evolutions/src/main/scala/play/api/db/evolutions/Evolutions.scala#L103

def resourceName(db: String, revision: Int): String = s"evolutions/${db}/${revision}.sql"

https://github.com/playframework/playframework/blob/2.5.x/framework/src/play-jdbc-evolutions/src/main/scala/play/api/db/evolutions/Evolutions.scala#L108

@Singleton
class EnvironmentEvolutionsReader @Inject() (environment: Environment) extends ResourceEvolutionsReader {

  def loadResource(db: String, revision: Int) = {
    environment.getExistingFile(Evolutions.fileName(db, revision)).map(new FileInputStream(_)).orElse {
      environment.resourceAsStream(Evolutions.resourceName(db, revision))
    }
  }
}

https://github.com/playframework/playframework/blob/2.5.x/framework/src/play-jdbc-evolutions/src/main/scala/play/api/db/evolutions/EvolutionsApi.scala#L471-L479

因此,正如文档中描述的那样,您只能使用数字{revision}.sql