1
1
package org .elastic4play .services
2
2
3
- import javax .inject .{Inject , Singleton }
4
-
5
3
import scala .concurrent .{ExecutionContext , Future }
6
4
import scala .util .{Failure , Success }
7
5
8
- import play .api .{Configuration , Logger }
9
6
import play .api .libs .json .JsValue .jsValueToJsLookup
10
7
import play .api .libs .json .Json .toJsFieldJsValueWrapper
11
8
import play .api .libs .json ._
9
+ import play .api .{Configuration , Logger }
12
10
13
11
import akka .NotUsed
14
12
import akka .actor .ActorSystem
15
- import akka .stream .{ActorMaterializer , ActorMaterializerSettings , Materializer }
16
13
import akka .stream .scaladsl .{Sink , Source }
14
+ import akka .stream .{ActorMaterializer , ActorMaterializerSettings , Materializer }
17
15
import com .sksamuel .elastic4s .http .ElasticDsl ._
18
16
import com .typesafe .config .Config
17
+ import javax .inject .{Inject , Singleton }
19
18
20
19
import org .elastic4play .InternalError
21
20
import org .elastic4play .database ._
22
21
23
22
case class MigrationEvent (modelName : String , current : Long , total : Long ) extends EventMessage
24
- case object EndOfMigrationEvent extends EventMessage
23
+
24
+ case object EndOfMigrationEvent extends EventMessage
25
25
26
26
object IndexType extends Enumeration {
27
27
val indexWithMappingTypes, indexWithoutMappingTypes = Value
@@ -115,6 +115,7 @@ class MigrationSrv @Inject()(
115
115
(" _version" → JsNumber (hit.version))
116
116
}
117
117
}
118
+
118
119
override def count (tableName : String ): Future [Long ] =
119
120
db.execute {
120
121
search(db.indexName / tableName).matchAllQuery().size(0 )
@@ -123,6 +124,7 @@ class MigrationSrv @Inject()(
123
124
_.totalHits
124
125
}
125
126
.recover { case _ ⇒ 0L }
127
+
126
128
override def getEntity (tableName : String , entityId : String ): Future [JsObject ] = currentdbget(tableName, entityId)
127
129
}
128
130
@@ -170,7 +172,10 @@ class MigrationSrv @Inject()(
170
172
val r = entities
171
173
.zipWith(count) { (entity, current) ⇒
172
174
eventSrv.publish(MigrationEvent (modelName, current.toLong, total))
173
- entity
175
+ (entity \ " _type" ).asOpt[JsString ].fold(entity) { t ⇒
176
+ val relations = (entity \ " _parent" ).asOpt[JsString ].fold[JsValue ](t)(p ⇒ Json .obj(" name" → t, " parent" → p))
177
+ entity - " _type" - " _parent" + (" relations" → relations)
178
+ }
174
179
}
175
180
.runWith(dbcreate.sink())
176
181
r.onComplete { x ⇒
@@ -230,6 +235,7 @@ class MigrationSrv @Inject()(
230
235
def isMigrating : Boolean = ! migrationProcess.isCompleted
231
236
def isReady : Boolean = dbindex.indexStatus && migrationProcess.isCompleted
232
237
}
238
+
233
239
/* Operation applied to the previous state of the database to get next version */
234
240
trait Operation extends ((String ⇒ Source [JsObject , NotUsed ]) ⇒ (String ⇒ Source [JsObject , NotUsed ]))
235
241
@@ -259,7 +265,8 @@ object Operation {
259
265
})
260
266
261
267
def mapEntity (tables : String * )(transform : JsObject ⇒ JsObject ): Operation = mapEntity(tables.contains, transform)
262
- def apply (table : String )(transform : JsObject ⇒ JsObject ): Operation = mapEntity(_ == table, transform)
268
+
269
+ def apply (table : String )(transform : JsObject ⇒ JsObject ): Operation = mapEntity(_ == table, transform)
263
270
264
271
def removeEntity (tableFilter : String ⇒ Boolean , filter : JsObject ⇒ Boolean ): Operation =
265
272
Operation ((f : String ⇒ Source [JsObject , NotUsed ]) ⇒ {
@@ -268,7 +275,8 @@ object Operation {
268
275
})
269
276
270
277
def removeEntity (tables : String * )(filter : JsObject ⇒ Boolean ): Operation = removeEntity(tables.contains, filter)
271
- def removeEntity (table : String )(filter : JsObject ⇒ Boolean ): Operation = removeEntity(_ == table, filter)
278
+
279
+ def removeEntity (table : String )(filter : JsObject ⇒ Boolean ): Operation = removeEntity(_ == table, filter)
272
280
273
281
def renameAttribute (tableFilter : String ⇒ Boolean , newName : String , oldNamePath : Seq [String ]): Operation =
274
282
Operation ((f : String ⇒ Source [JsObject , NotUsed ]) ⇒ {
@@ -283,6 +291,7 @@ object Operation {
283
291
284
292
def renameAttribute (tables : Seq [String ], newName : String , oldNamePath : String * ): Operation =
285
293
renameAttribute(a ⇒ tables.contains(a), newName, oldNamePath)
294
+
286
295
def renameAttribute (table : String , newName : String , oldNamePath : String * ): Operation = renameAttribute(_ == table, newName, oldNamePath)
287
296
288
297
def rename (value : JsObject , newName : String , path : Seq [String ]): JsObject =
@@ -310,6 +319,7 @@ object Operation {
310
319
311
320
def mapAttribute (tables : Seq [String ], attribute : String )(transform : JsValue ⇒ JsValue ): Operation =
312
321
mapAttribute(a ⇒ tables.contains(a), attribute, transform)
322
+
313
323
def mapAttribute (table : String , attribute : String )(transform : JsValue ⇒ JsValue ): Operation = mapAttribute(_ == table, attribute, transform)
314
324
315
325
def removeAttribute (tableFilter : String ⇒ Boolean , attributes : String * ): Operation =
@@ -320,8 +330,10 @@ object Operation {
320
330
y - a
321
331
}
322
332
)
333
+
323
334
def removeAttribute (tables : Seq [String ], attributes : String * ): Operation = removeAttribute(a ⇒ tables.contains(a), attributes : _* )
324
- def removeAttribute (table : String , attributes : String * ): Operation = removeAttribute(_ == table, attributes : _* )
335
+
336
+ def removeAttribute (table : String , attributes : String * ): Operation = removeAttribute(_ == table, attributes : _* )
325
337
326
338
def addAttribute (tableFilter : String ⇒ Boolean , attributes : (String , JsValue )* ): Operation =
327
339
mapEntity(
@@ -331,8 +343,10 @@ object Operation {
331
343
y + a
332
344
}
333
345
)
346
+
334
347
def addAttribute (tables : Seq [String ], attributes : (String , JsValue )* ): Operation = addAttribute(t ⇒ tables.contains(t), attributes : _* )
335
- def addAttribute (table : String , attributes : (String , JsValue )* ): Operation = addAttribute(_ == table, attributes : _* )
348
+
349
+ def addAttribute (table : String , attributes : (String , JsValue )* ): Operation = addAttribute(_ == table, attributes : _* )
336
350
337
351
def addAttributeIfAbsent (tableFilter : String ⇒ Boolean , attributes : (String , JsValue )* ): Operation =
338
352
mapEntity(tableFilter, { x ⇒
@@ -345,5 +359,6 @@ object Operation {
345
359
})
346
360
347
361
def addAttributeIfAbsent (tables : Seq [String ], attributes : (String , JsValue )* ): Operation = addAttribute(t ⇒ tables.contains(t), attributes : _* )
348
- def addAttributeIfAbsent (table : String , attributes : (String , JsValue )* ): Operation = addAttribute(_ == table, attributes : _* )
362
+
363
+ def addAttributeIfAbsent (table : String , attributes : (String , JsValue )* ): Operation = addAttribute(_ == table, attributes : _* )
349
364
}
0 commit comments