Attached.Ecto.Migration (Attached v0.1.1)

Copy Markdown View Source

Migrations create and modify the database tables Attached needs to function.

Usage

Generate a migration in your app:

mix attached.install

This creates a migration that delegates to Attached.Ecto.Migration:

defmodule MyApp.Repo.Migrations.CreateAttachedTables do
  use Ecto.Migration

  def up, do: Attached.Ecto.Migration.up()
  def down, do: Attached.Ecto.Migration.down()
end

Upgrading

When a new version of Attached requires schema changes, generate a new migration:

mix ecto.gen.migration upgrade_attached_to_v2

Then call up with the target version:

def up, do: Attached.Ecto.Migration.up(version: 2)
def down, do: Attached.Ecto.Migration.down(version: 2)

Summary

Functions

Run all migrations down from the latest version, or a specific version.

Updates attached_originals ownership tracking when a field or table is renamed.

Run all migrations up to the latest version, or a specific version.

Functions

down(opts \\ [])

Run all migrations down from the latest version, or a specific version.

rename(table, list)

rename(table, old_field, list)

Updates attached_originals ownership tracking when a field or table is renamed.

Mirrors Ecto's rename/2,3 syntax — call it alongside the corresponding Ecto rename in your migration.

Field rename

def up do
  rename table(:users), :avatar_attached_original_id, to: :profile_picture_attached_original_id
  Attached.Ecto.Migration.rename table(:users), :avatar, to: :profile_picture
end

def down do
  rename table(:users), :profile_picture_attached_original_id, to: :avatar_attached_original_id
  Attached.Ecto.Migration.rename table(:users), :profile_picture, to: :avatar
end

Table rename

def up do
  rename table(:users), to: table(:accounts)
  Attached.Ecto.Migration.rename table(:users), to: table(:accounts)
end

def down do
  rename table(:accounts), to: table(:users)
  Attached.Ecto.Migration.rename table(:accounts), to: table(:users)
end

up(opts \\ [])

Run all migrations up to the latest version, or a specific version.