Add possibility to define bidirectional migrations#62
Add possibility to define bidirectional migrations#62psliwa wants to merge 1 commit intoscalapenos:masterfrom
Conversation
|
@agemooij Any chance to take a look at this PR soon? |
|
@raboof what do you think of this PR? |
agemooij
left a comment
There was a problem hiding this comment.
This code seems to always apply all forward and backward migrations instead of allowing them to cancel each other out. Have you considered removing migrations if they would be cancelled out by a backward one?
| implicit class MigrationWithComposition[T](val firstMigration: Migration[T]) extends AnyVal { | ||
| def &&(secondMigration: Migration[T]): Migration[T] = { | ||
| (value: T) ⇒ secondMigration(firstMigration(value)) | ||
| } |
There was a problem hiding this comment.
Because it is not used anymore.
| * creating Migrator[T, V2], etc. Its migration will be the identity | ||
| * function so calling its migrate function will not have any effect. | ||
| */ | ||
| def from[T, V <: V1: VersionInfo]: Migrator[T, V] = new Migrator[T, V](Map(Version.numberFor[V] → identityMigration[T])) |
There was a problem hiding this comment.
Why not make the empty map a default argument of the Migrator class?
There was a problem hiding this comment.
Because backward migrations are empty when we start defining migration - so empty map is natural initial value.
|
I am not sure what you mean under
|
|
@agemooij ping |
|
Any progress? Maybe something is unclear? |
| */ | ||
| class Migrator[R, V <: Version: VersionInfo] private[stamina] (migrations: Map[Int, Migration[R]] = Map.empty) { | ||
| def canMigrate(fromVersion: Int): Boolean = migrations.contains(fromVersion) | ||
| class Migrator[R, V <: Version: VersionInfo] private[stamina] (forwardMigrations: Map[Int, Migration[R]] = Map.empty, backwardMigrations: Map[Int, Migration[R]] = Map.empty) { |
There was a problem hiding this comment.
Is this correct? It's not obvious to me that we now also need forwardMigrations.contains(toVersion), and shouldn't this also take into account that we can support backwards migrations?
There was a problem hiding this comment.
There is a simple code from tests:
val migrator = from[String, V1]
.to[V2](_ + "V2")
.backTo[V1](_.replace("V2", ""))
.to[V3](_ + "V3")
.backTo[V2](_.replace("V3", ""))
.to[V4](_ + "V4")
.backTo[V3](_.replace("V4", ""))
.to[V5](_ + "V5")
The assumption is to define continuous forward migrations, so you cannot define migrations only for version 1 and version 3. You have to define migration also for version 2. So in case when we are going to migrate from version 2 to version 5, we need migrations from 2 to 3, from 3 to 4 and from 4 to 5. Migration from 4 to 5 is under 5 key, it is toVersion, so it is needed as well. canMigrate function checks in this case existence of 2 -> 3 and 4 -> 5 migrations, because forward migrations are defined continuously. Backward migrations are optional, so they doesn't affect canMigrate function at all. If backward migration is not defined for particular version, identity function is used.
|
@raboof ping |
|
@psliwa we are currently waiting on your replies to our comments. |
|
|
||
| import stamina._ | ||
|
|
||
| class MigratorSpec extends StaminaSpec { |
There was a problem hiding this comment.
This test suits to scalacheck, do you think I can add scalacheck as dependency and rewrite this test?
| implicit class MigrationWithComposition[T](val firstMigration: Migration[T]) extends AnyVal { | ||
| def &&(secondMigration: Migration[T]): Migration[T] = { | ||
| (value: T) ⇒ secondMigration(firstMigration(value)) | ||
| } |
There was a problem hiding this comment.
Because it is not used anymore.
| * creating Migrator[T, V2], etc. Its migration will be the identity | ||
| * function so calling its migrate function will not have any effect. | ||
| */ | ||
| def from[T, V <: V1: VersionInfo]: Migrator[T, V] = new Migrator[T, V](Map(Version.numberFor[V] → identityMigration[T])) |
There was a problem hiding this comment.
Because backward migrations are empty when we start defining migration - so empty map is natural initial value.
| */ | ||
| class Migrator[R, V <: Version: VersionInfo] private[stamina] (migrations: Map[Int, Migration[R]] = Map.empty) { | ||
| def canMigrate(fromVersion: Int): Boolean = migrations.contains(fromVersion) | ||
| class Migrator[R, V <: Version: VersionInfo] private[stamina] (forwardMigrations: Map[Int, Migration[R]] = Map.empty, backwardMigrations: Map[Int, Migration[R]] = Map.empty) { |
There was a problem hiding this comment.
There is a simple code from tests:
val migrator = from[String, V1]
.to[V2](_ + "V2")
.backTo[V1](_.replace("V2", ""))
.to[V3](_ + "V3")
.backTo[V2](_.replace("V3", ""))
.to[V4](_ + "V4")
.backTo[V3](_.replace("V4", ""))
.to[V5](_ + "V5")
The assumption is to define continuous forward migrations, so you cannot define migrations only for version 1 and version 3. You have to define migration also for version 2. So in case when we are going to migrate from version 2 to version 5, we need migrations from 2 to 3, from 3 to 4 and from 4 to 5. Migration from 4 to 5 is under 5 key, it is toVersion, so it is needed as well. canMigrate function checks in this case existence of 2 -> 3 and 4 -> 5 migrations, because forward migrations are defined continuously. Backward migrations are optional, so they doesn't affect canMigrate function at all. If backward migration is not defined for particular version, identity function is used.
|
@agemooij Sorry, I am not sure how github conversations work. I added comments long ago, but they were marked as |
|
Looks like you started a review but did not finish it. This is a common mistake made by quite a few people I know since Github introduced this feature. @raboof could you also have a look at the review comments? |
This PR is for #57.
DSL hasn't changed, api is simple. If you want to define backward migration, you can use
backTofunction onMigrator. I've assumed not for every forward migration has to exist backward migration (sobackToinvocation is optional), because when forward migration eg. adds new field, backward migration is not needed. The second reason for optionalbackTomigration is just not using backward migrations in the project.Usage from tests: