View Source Versioned.Migration (Versioned v0.4.0)

Allows creating tables for versioned schemas.

example

Example

defmodule MyApp.Repo.Migrations.CreateCar do
  use Versioned.Migration

  def change do
    create_versioned_table(:cars) do
      add :name, :string
    end
  end
end

Link to this section Summary

Functions

Add a new column to both the main table and the versions table.

Create a table whose data is versioned by also creating a secondary table with the immutable, append-only history.

Modify orig_field column in table table_name and its versioned counterpart.

Removes a column from a table and its versioned counterpart.

Rename orig_field column in table table_name to a new name.

Renames a table table and its versioned counterpart.

Link to this section Functions

Link to this macro

add_versioned_column(table_name, name, type, opts \\ [])

View Source (macro)

Add a new column to both the main table and the versions table.

Any foreign key constraint via references/2 will be stripped for the versions table.

Link to this macro

create_versioned_table(name_plural, opts \\ [], list)

View Source (macro)

Create a table whose data is versioned by also creating a secondary table with the immutable, append-only history.

Link to this macro

modify_versioned_column(table_name, column, type, opts \\ [])

View Source (macro)

Modify orig_field column in table table_name and its versioned counterpart.

See Ecto.Migration.modify/3.

example

Example

defmodule MyApp.Repo.Migrations.RenameFooToBar do
  use Versioned.Migration

  def change do
    modify_versioned_column("my_table", :foo, :text, null: true)
  end
end
Link to this macro

remove_versioned_column(table_name, column)

View Source (macro)

Removes a column from a table and its versioned counterpart.

See Ecto.Migration.remove/1.

example

Example

defmodule MyApp.Repo.Migrations.RemoveFooToBar do
  use Versioned.Migration

  def change do
    remove_versioned_column("my_table", :foo)
  end
end
Link to this macro

rename_versioned_column(table_name, orig_field, opts)

View Source (macro)

Rename orig_field column in table table_name to a new name.

See Ecto.Migration.rename/3.

Note that this is indeed changing the field names as well in the complimenting and generally immutable "versions" table.

example

Example

defmodule MyApp.Repo.Migrations.RenameFooToBar do
  use Versioned.Migration

  def change do
    rename_versioned_column("my_table", :foo, to: :bar)
  end
end
Link to this macro

rename_versioned_table(orig_name, new_name)

View Source (macro)

Renames a table table and its versioned counterpart.

See Ecto.Migration.rename/2.

other-considerations

Other Considerations

If the table is cars, then cars_versions has a car_id field. If the table is being renamed to automobiles, then after using this macro, you'll want to also do something like the following in order to rename the car_id field in the versions table to automobile_id.

rename table("automobiles_versions"), :car_id, to: :automobile_id

If your table has additional foreign key constraints such as those set up with Ecto.Migration.references/2, you'll want to move those over, too, with something like this. The versions table won't have any such constraints.

execute(
  "ALTER TABLE automobiles RENAME CONSTRAINT cars_garage_id_fkey TO automobiles_garage_id_fkey;",
  "ALTER TABLE automobiles RENAME CONSTRAINT automobiles_garage_id_fkey TO cars_garage_id_fkey;"
)

example

Example

defmodule MyApp.Repo.Migrations.RenameCarsToAutomobiles do
  use Versioned.Migration

  def change do
    rename_versioned_table("cars", "automobiles")
    rename table("automobiles_versions"), :car_id, to: :automobile_id
  end
end