zio-schema icon indicating copy to clipboard operation
zio-schema copied to clipboard

Allow using default values for migrations

Open thinkharderdev opened this issue 3 years ago • 4 comments

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:

  1. Change Migration.AddNode to case class AddNode(override val path: NodePath, node: SchemaAst, defaultValue: DynamicValue) extends Migration
  2. Populate the default value (using the Schema when deriving the Migration.
  3. 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] = ...
}
  1. Change the signature of Schema.migrate to def migrate[B](newSchema: Schema[B]): Either[String,SchemaMigration[A,B]]
  2. Modify the migration logic to conditionally use the supplied default value
  3. Update default value implementation to use all empty container types. Default value for Schema[List[A]] should be Nil, etc

Open question:

  1. 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?

thinkharderdev avatar Jan 19 '22 10:01 thinkharderdev

I would like to work on this

kaushik143 avatar Jan 25 '22 14:01 kaushik143

I am in favor of using default values for any type and giving the option to pass default values.

kaushik143 avatar Jan 25 '22 14:01 kaushik143

@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.

thinkharderdev avatar Jan 25 '22 22:01 thinkharderdev

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 Screenshot 2024-09-07 at 10 13 38

shivamgupta88 avatar Sep 07 '24 04:09 shivamgupta88