-
Notifications
You must be signed in to change notification settings - Fork 31
/
Commands.scala
300 lines (258 loc) · 7.82 KB
/
Commands.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package com.liyaos.forklift.slick
import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import scala.util.{ Failure, Success }
import com.liyaos.forklift.core.MigrationsConfig
import com.liyaos.forklift.core.MigrationFilesHandler
import com.liyaos.forklift.core.RescueCommands
import com.liyaos.forklift.core.RescueCommandLineTool
import com.liyaos.forklift.core.MigrationCommands
import com.liyaos.forklift.core.MigrationCommandLineTool
object CommandExceptions {
case class WrongNumberOfArgumentsException(expected: Int, given: Int)
extends Exception(s"We expect $expected arguments, but we find $given.")
case class WrongArgumentException(arg: String)
extends Exception(s"We do not understand the argument you give: $arg.")
}
object MigrationType extends Enumeration {
import CommandExceptions._
type MigrationType = Value
val SQL, DBIO, API = Value
val nameMap = Map("sql" -> SQL, "s" -> SQL, "dbio" -> DBIO, "d" -> DBIO, "api" -> API, "a" -> API)
def getType(s: String) = nameMap.get(s.toLowerCase)
}
trait SlickMigrationFilesHandler extends MigrationFilesHandler[Int] {
def nameIsId(name: String) =
name forall Character.isDigit
def nameToId(name: String): Int =
name.toInt
def idShouldBeHandled(id: String, appliedIds: Seq[Int]) =
if (appliedIds.isEmpty) id.toInt == 1
else id.toInt <= appliedIds.max + 1
def nextId = {
val unhandled = new File(unhandledLoc)
val ids = for {
file <- unhandled.listFiles
name <- getId(file.getName)
if nameIsId(name)
} yield nameToId(name)
if (!ids.isEmpty) ids.max + 1 else 1
}
}
trait SlickRescueCommands extends RescueCommands[Int] with SlickMigrationFilesHandler {
this: SlickCodegen =>
private def deleteRecursively(f: File) : Unit = {
if (f.isDirectory) {
for {
files <- Option(f.listFiles)
file <- files
} deleteRecursively(file)
}
f.delete
}
override def rescueCommand : Unit = {
super.rescueCommand
deleteRecursively(new File(generatedDir))
}
}
trait SlickRescueCommandLineTool extends RescueCommandLineTool[Int] {
this: SlickRescueCommands =>
}
trait SlickMigrationCommands
extends MigrationCommands[Int, slick.dbio.DBIO[Unit]]
with SlickMigrationFilesHandler {
this: SlickMigrationManager with SlickCodegen =>
import MigrationType._
import OptionToTry._
override def applyOps: Seq[() => Unit] = List(() => applyOp, () => codegenOp)
override def statusOp : Unit = {
val mf = migrationFiles(alreadyAppliedIds)
if (!mf.isEmpty) {
println("you still have unhandled migrations")
println("use mg update to fetch these migrations")
} else {
val ny = notYetAppliedMigrations
if (ny.size == 0) {
println("your database is up-to-date")
} else {
println(
"your database is outdated, not yet applied migrations: " + notYetAppliedMigrations
.map(_.id)
.mkString(", ")
)
}
}
}
override def statusCommand : Unit = {
try {
super.statusCommand
} finally {
db.close()
}
}
override def previewOp : Unit = {
println("-" * 80)
println("NOT YET APPLIED MIGRATIONS PREVIEW:")
println("")
notYetAppliedMigrations.map { migration =>
migration match {
case m: SqlMigrationInterface[_] =>
println(migration.id + " SqlMigration:")
println("\t" + m.queries.map(_.getDumpInfo.mainInfo).mkString("\n\t"))
case m: SqlResourceMigrationInterface[_] =>
println(migration.id + " SqlResourceMigration:")
println(m.sqlQueries)
case m: DBIOMigration[_] =>
println(migration.id + " DBIOMigration:")
println("\t" + m.code)
case m: APIMigration[_] =>
println(migration.id + " APIMigration:")
println("\t" + m.migration.sql)
}
println("")
}
println("-" * 80)
}
override def previewCommand : Unit = {
try {
super.previewCommand
} finally {
db.close()
}
}
override def applyOp : Unit = {
val ids = notYetAppliedMigrations.map(_.id)
println("applying migrations: " + ids.mkString(", "))
up()
}
override def applyCommand : Unit = {
try {
super.applyCommand
} finally {
db.close()
}
}
override def migrateCommand(options: Seq[String]) : Unit = {
try {
super.migrateCommand(options)
} finally {
db.close()
}
}
override def initOp : Unit = {
super.initOp
init
}
override def initCommand : Unit = {
try {
super.initCommand
} finally {
db.close()
}
}
override def resetOp : Unit = {
super.resetOp
reset
remove()
}
override def resetCommand : Unit = {
try {
super.resetCommand
} finally {
db.close()
}
}
override def updateCommand(options: Seq[String]) : Unit = {
try super.updateCommand(options)
finally {
db.close()
}
}
// def dbdumpCommand {
// import scala.slick.driver.H2Driver.simple._
// import Database.dynamicSession
// import scala.slick.jdbc.StaticQuery._
// db.withDynSession{
// println( queryNA[String]("SCRIPT").list.mkString("\n") )
// }
// }
def addMigrationOp(tpe: MigrationType, version: Int) : Unit = {
val migrationObject = config.getString("migrations.migration_object")
val driverName = dbConfig.profileName
val dbName = driverName.substring("slick.jdbc.".length, driverName.length - "Profile".length)
val imports =
if (version > 1)
s"""import ${pkgName("v" + (version - 1))}.tables._"""
else ""
val content = tpe match {
case SQL => s"""import ${driverName}.api._
import com.liyaos.forklift.slick.SqlMigration
object M${version} {
${migrationObject}.migrations = ${migrationObject}.migrations :+ SqlMigration(${version})(List(
sqlu"" // your sql code goes here
))
}
"""
case DBIO =>
s"""import ${driverName}.api._
import com.liyaos.forklift.slick.DBIOMigration
${imports}
object M${version} {
${migrationObject}.migrations = ${migrationObject}.migrations :+ DBIOMigration(${version})(
DBIO.seq(
// write your dbio actions here
))
}
"""
case API =>
s"""import ${driverName}.api._
import slick.migration.api.TableMigration
import slick.migration.api.${dbName}Dialect
import com.liyaos.forklift.slick.APIMigration
${imports}
object M${version} {
implicit val dialect = new ${dbName}Dialect
${migrationObject}.migrations = ${migrationObject}.migrations :+ APIMigration(${version})(// write your migration here)
}"""
}
val file = new File(unhandledLoc + "/" + version + ".scala")
if (!file.exists) file.createNewFile()
val bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()))
bw.write(content)
bw.close()
}
def addMigrationCommand(options: Seq[String]) : Unit = {
val tpe = options.headOption.toTry(CommandExceptions.WrongNumberOfArgumentsException(1, 0)) flatMap {
s =>
MigrationType.getType(s).toTry(CommandExceptions.WrongArgumentException(s))
}
val t = tpe.get
addMigrationOp(t, nextId)
}
def codegenOp : Unit = {
genCode(this)
}
def codegenCommand : Unit = {
try {
codegenOp
} finally {
db.close()
}
}
}
trait SlickMigrationCommandLineTool extends MigrationCommandLineTool[Int, slick.dbio.DBIO[Unit]] {
this: SlickMigrationCommands =>
override def execCommands(args: List[String]) = args match {
case "codegen" :: Nil => codegenCommand
case "new" :: tpe => addMigrationCommand(tpe)
case _ => super.execCommands(args)
}
override def help =
super.help + """
codegen generate data model code (table objects, case classes) from the
database schema
new create a new migration file. please specify migration type with
"s" (sql) or "d" (dbio)
"""
}