Versioning.Change behaviour (Versioning v0.4.1) View Source

Defines a versioning change.

A versioning change is used to make small changes to data of a certain type. They are used within a Versioning.Schema. Changes should attempt to be as focused as possible to ensure complexity is kept to a minimum.

Example

defmodule MyApp.Cheanges.PostStatusChange do
  use Versioning.Change

  @desc "The 'active' attribute has been changed in favour of the 'status' attribute"

  @impl Versioning.Change
  def down(versioning, _opts) do
    case Versioning.pop_data(versioning, "status") do
      {:active, versioning} -> Versioning.put_data(versioning, "active", true)
      {_, versioning} -> Versioning.put_data(versioning, "active", false)
    end
  end

  @impl Versioning.Change
  def up(versioning, _opts) do
    case Versioning.pop_data(versioning, "active") do
      {true, versioning} -> Versioning.put_data(versioning, "status", "active")
      {false, versioning} -> Versioning.put_data(versioning, "status", "hidden")
      {_, versioning} -> versioning
    end
  end
end

The above change module represents us modifying our Post data to support a new attribute - status - which replaces the previous active attribute.

When changing data "down", we must remove the status attribte, and replace it with a value that represents the previous active attribute. When changing data "up", we must remove the active attribute and replace it with a value that represents the new status attribute.

Descriptions

Change modules can optionally include a @desc module attribute. This will be used to describe the changes made in the change module when constructing changelogs. Please see the Versioning.Changelog documentation for more information on changelogs.

Link to this section Summary

Callbacks

Accepts a Versioning struct and applies changes downward.

Accepts a Versioning struct, and applies changes upward.

Link to this section Callbacks

Specs

down(versioning :: Versioning.t(), opts :: any()) :: Versioning.t()

Accepts a Versioning struct and applies changes downward.

Examples

MyApp.Change.down(versioning)

Specs

up(versioning :: Versioning.t(), opts :: any()) :: Versioning.t()

Accepts a Versioning struct, and applies changes upward.

Examples

MyApp.Change.up(versioning)