Packages
ecto_sql
3.8.0
3.14.0
3.13.5
3.13.4
3.13.3
3.13.2
3.13.1
3.13.0
3.12.1
3.12.0
3.11.3
3.11.2
3.11.1
3.11.0
3.10.2
3.10.1
3.10.0
3.9.2
3.9.1
3.9.0
3.8.3
3.8.2
3.8.1
3.8.0
3.7.2
3.7.1
3.7.0
3.6.2
3.6.1
3.6.0
3.5.4
3.5.3
3.5.2
3.5.1
3.5.0
3.5.0-rc.1
3.5.0-rc.0
3.4.5
3.4.4
3.4.3
3.4.2
3.4.1
3.4.0
3.3.4
3.3.3
3.3.2
3.3.1
3.3.0
3.2.2
3.2.1
3.2.0
3.1.6
3.1.5
3.1.4
3.1.3
3.1.2
3.1.1
3.1.0
3.0.5
3.0.4
3.0.3
3.0.2
3.0.1
3.0.0
3.0.0-rc.1
3.0.0-rc.0
SQL-based adapters for Ecto and database migrations
Current section
Files
Jump to
Current section
Files
lib/ecto/migration/schema_migration.ex
defmodule Ecto.Migration.SchemaMigration do
# Defines a schema that works with a table that tracks schema migrations.
# The table name defaults to `schema_migrations`.
@moduledoc false
use Ecto.Schema
import Ecto.Query, only: [from: 2]
@primary_key false
schema "schema_migrations" do
field :version, :integer
timestamps updated_at: false
end
# The migration flag is used to signal to the repository
# we are in a migration operation.
@default_opts [timeout: :infinity, log: false, schema_migration: true]
def ensure_schema_migrations_table!(repo, config, opts) do
{repo, source} = get_repo_and_source(repo, config)
table_name = String.to_atom(source)
table = %Ecto.Migration.Table{name: table_name, prefix: opts[:prefix]}
meta = Ecto.Adapter.lookup_meta(repo.get_dynamic_repo())
commands = [
{:add, :version, :bigint, primary_key: true},
{:add, :inserted_at, :naive_datetime, []}
]
repo.__adapter__().execute_ddl(meta, {:create_if_not_exists, table, commands}, @default_opts)
end
def versions(repo, config, prefix) do
{repo, source} = get_repo_and_source(repo, config)
{repo, from(m in source, select: type(m.version, :integer)), [prefix: prefix] ++ @default_opts}
end
def up(repo, config, version, opts) do
{repo, source} = get_repo_and_source(repo, config)
%__MODULE__{version: version}
|> Ecto.put_meta(source: source)
|> repo.insert(default_opts(opts))
end
def down(repo, config, version, opts) do
{repo, source} = get_repo_and_source(repo, config)
from(m in source, where: m.version == type(^version, :integer))
|> repo.delete_all(default_opts(opts))
end
def get_repo_and_source(repo, config) do
{Keyword.get(config, :migration_repo, repo),
Keyword.get(config, :migration_source, "schema_migrations")}
end
defp default_opts(opts) do
Keyword.merge(
@default_opts,
[prefix: opts[:prefix], log: Keyword.get(opts, :log_migrator_sql, false)]
)
end
end