zio-schema
zio-schema copied to clipboard
Allow using default values for migrations
Now that we can generate a default value for any schema, we should allow the use of default values to "fill out" schema migrations.
For example, a migration from case class Foo(id: Int)
to case class Bar(id: Int, value: Option[String]
would not be possible right now because adding nodes to the AST is not permitted. But if we allowed default values we could migrate Foo(1)
to Bar(1,None)
.
This would, at a high level, involve the following changes:
- Change
Migration.AddNode
tocase class AddNode(override val path: NodePath, node: SchemaAst, defaultValue: DynamicValue) extends Migration
- Populate the default value (using the
Schema
when deriving the Migration. - Add an explicit type
case class SchemaMigration[A,B](migrations: Chunk[Migration]) extends (A,Boolean) => Either[String,B] {
def apply(a: A, useDefaults: Boolean = false): Either[String,B] = ...
}
- Change the signature of
Schema.migrate
todef migrate[B](newSchema: Schema[B]): Either[String,SchemaMigration[A,B]]
- Modify the migration logic to conditionally use the supplied default value
- Update default value implementation to use all empty container types. Default value for
Schema[List[A]]
should beNil
, etc
Open question:
- Do we use default values for any type or only for container types:
Option
,List
, etc? If so, do we provide an option to use default values for scalar types?
I would like to work on this
I am in favor of using default values for any type and giving the option to pass default values.
@kaushik143 Awesome! One thing I noticed recently is the default values for collection types are collections with a single element. They should probably be empty. So the default value for Schema[List[A]]
should always be Nil
.
I want to work on this issue , need someone assistance in setting up this project I have tried and reached at the img I have attached , now I want to know, How to proceed ahead ,any help will be very appreciated