All Downloads are FREE. Search and download functionalities are using the official Maven repository.

harness.sql.autoSchema.InMemoryMigration.scala Maven / Gradle / Ivy

There is a newer version: 5.1.3
Show newest version
package harness.sql.autoSchema

import cats.data.NonEmptyList
import harness.core.Version
import harness.sql.JDBCConnection
import harness.zio.*
import zio.*

final case class InMemoryMigration(version: Version, tables: Tables, steps: NonEmptyList[InMemoryMigration.Step])
object InMemoryMigration {

  type StepType = InMemoryMigration.Step | InMemoryMigration.StepType.Auto | MigrationStep.InMemory
  object StepType {

    final case class Auto(allowDrops: Boolean = false)

    def toStep(stepType: StepType, tables: Tables): InMemoryMigration.Step =
      stepType match {
        case step: InMemoryMigration.Step          => step
        case auto: InMemoryMigration.StepType.Auto => InMemoryMigration.Step.Auto(tables, auto.allowDrops)
        case inMemory: MigrationStep.InMemory      => InMemoryMigration.Step.Manual(inMemory)
      }

  }

  def apply(version: Version, tables: Tables)(step0: InMemoryMigration.StepType, stepN: InMemoryMigration.StepType*): InMemoryMigration =
    new InMemoryMigration(version, tables, NonEmptyList(step0, stepN.toList).map(StepType.toStep(_, tables)))

  def auto(version: Version, tables: Tables, allowDrops: Boolean = false): InMemoryMigration =
    InMemoryMigration(version, tables)(InMemoryMigration.Step.Auto(tables, allowDrops))

  sealed trait Step
  object Step {
    final case class Auto(tables: Tables, allowDrops: Boolean = false) extends Step
    final case class Manual(step: MigrationStep.InMemory) extends Step

    def apply(step: MigrationStep.InMemory): InMemoryMigration.Step =
      InMemoryMigration.Step.Manual(step)
    def code(name: String)(up: RIO[HarnessEnv & JDBCConnection, Unit]): InMemoryMigration.Step =
      InMemoryMigration.Step.Manual(MigrationStep.InMemory.Code(name, up, None))
    def code(name: String)(up: RIO[HarnessEnv & JDBCConnection, Unit], down: RIO[HarnessEnv & JDBCConnection, Unit]): InMemoryMigration.Step =
      InMemoryMigration.Step.Manual(MigrationStep.InMemory.Code(name, up, Some(down)))
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy