Created
February 17, 2017 19:45
-
-
Save totallymike/d969ecd61c3e134471e7c5492f722cc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pretend.myforklift | |
import com.liyaos.forklift.slick.{ SlickCodegen, SlickMigrationCommandLineTool, SlickMigrationCommands, SlickMigrationManager } | |
import scala.io.StdIn | |
trait MyMigrationCommands extends SlickMigrationCommands | |
with MyMigrationFilesHandler { | |
this: SlickMigrationManager with SlickCodegen => | |
override def migrateOp(options: Seq[String]): Unit = { | |
val prompt = options.contains("-p") | |
val prod = options.contains("-prod") | |
// If the production flag is set, don't do codegen | |
val applyOpsOp = if (prod) { | |
List(() => applyOp) | |
} else { | |
List(() => applyOp, () => codegenOp) | |
} | |
if (!notYetAppliedMigrations.isEmpty) { | |
for (op <- previewOps) op() | |
if (prompt) { | |
if (StdIn.readLine("Do you wish to continue? [Y/N]") != "Y") return | |
} | |
for (op <- applyOpsOp) op() | |
} | |
if (!prod) updateOp | |
} | |
def productionStatusOp: Unit = { | |
val ny = notYetAppliedMigrations | |
if (ny.size == 0) { | |
println("Your database is up to date") | |
} else { | |
println(s"Your database is outdated, not yet applied migrations: ${notYetAppliedMigrations.map(_.id).mkString(", ")}") | |
} | |
} | |
def initDatabaseCommand: Unit = { | |
init | |
} | |
} | |
trait MyMigrationCommandLineTool extends SlickMigrationCommandLineTool { | |
this: MyMigrationCommands => | |
override def execCommands(args: List[String]): Unit = args match { | |
case "init" :: "-prod" :: Nil => initDatabaseCommand | |
case "status" :: "-prod" :: Nil => productionStatusOp | |
case _ => super.execCommands(args) | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pretend.myforklift | |
import java.io.{ BufferedWriter, File, FileWriter } | |
import java.nio.file.{ Files, Path } | |
import com.liyaos.forklift.slick.SlickMigrationFilesHandler | |
trait MyMigrationFilesHandler extends SlickMigrationFilesHandler { | |
protected lazy val migrationsPackage: Option[String] = { | |
if (config.hasPath("migrations.package")) { | |
Some(config.getString("migrations.package")) | |
} else { | |
None | |
} | |
} | |
override def handleMigrationFile(file: File): Unit = { | |
val newFile = new File(handledLoc + "/" + file.getName) | |
val oldFile = new File( | |
unhandledLoc + "/" + file.getName).getAbsoluteFile.toPath | |
if (!newFile.exists) { | |
val newPath: Path = Files.copy(oldFile, newFile.toPath) | |
println(s"Create copy of ${oldFile} in ${newPath}") | |
} | |
} | |
override protected def writeSummary(ids: Seq[String]): Unit = { | |
var code: String = migrationsPackage match { | |
case Some(packageName) => s"package $packageName\n\n" | |
case None => "" | |
} | |
code += "object MigrationSummary {\n" + ids.sortWith(_.toInt < _.toInt) | |
.map(n => "M" + n).mkString("\n") + "\n}\n" | |
val sumFile = new File(handledLoc + "/Summary.scala") | |
if (!sumFile.exists) sumFile.createNewFile() | |
val fw = new FileWriter(sumFile.getAbsoluteFile()) | |
val bw = new BufferedWriter(fw) | |
bw.write(code) | |
bw.close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment