ecto.migrations.ex (2868B)
1 defmodule Mix.Tasks.Ecto.Migrations do 2 use Mix.Task 3 import Mix.Ecto 4 import Mix.EctoSQL 5 6 @shortdoc "Displays the repository migration status" 7 8 @aliases [ 9 r: :repo 10 ] 11 12 @switches [ 13 repo: [:keep, :string], 14 no_compile: :boolean, 15 no_deps_check: :boolean, 16 migrations_path: :keep, 17 prefix: :string 18 ] 19 20 @moduledoc """ 21 Displays the up / down migration status for the given repository. 22 23 The repository must be set under `:ecto_repos` in the 24 current app configuration or given via the `-r` option. 25 26 By default, migrations are expected at "priv/YOUR_REPO/migrations" 27 directory of the current application but it can be configured 28 by specifying the `:priv` key under the repository configuration. 29 30 If the repository has not been started yet, one will be 31 started outside our application supervision tree and shutdown 32 afterwards. 33 34 ## Examples 35 36 $ mix ecto.migrations 37 $ mix ecto.migrations -r Custom.Repo 38 39 ## Command line options 40 41 * `--migrations-path` - the path to load the migrations from, defaults to 42 `"priv/repo/migrations"`. This option may be given multiple times in which 43 case the migrations are loaded from all the given directories and sorted as 44 if they were in the same one. 45 46 Note, if you have previously run migrations from paths `a/` and `b/`, and now 47 run `mix ecto.migrations --migrations-path a/` (omitting path `b/`), the 48 migrations from the path `b/` will be shown in the output as `** FILE NOT FOUND **`. 49 50 * `--no-compile` - does not compile applications before running 51 52 * `--no-deps-check` - does not check dependencies before running 53 54 * `--prefix` - the prefix to check migrations on 55 56 * `-r`, `--repo` - the repo to obtain the status for 57 58 """ 59 60 @impl true 61 def run(args, migrations \\ &Ecto.Migrator.migrations/3, puts \\ &IO.puts/1) do 62 repos = parse_repo(args) 63 {opts, _} = OptionParser.parse! args, strict: @switches, aliases: @aliases 64 65 for repo <- repos do 66 ensure_repo(repo, args) 67 paths = ensure_migrations_paths(repo, opts) 68 69 case Ecto.Migrator.with_repo(repo, &migrations.(&1, paths, opts), [mode: :temporary]) do 70 {:ok, repo_status, _} -> 71 puts.( 72 """ 73 74 Repo: #{inspect(repo)} 75 76 Status Migration ID Migration Name 77 -------------------------------------------------- 78 """ <> 79 Enum.map_join(repo_status, "\n", fn {status, number, description} -> 80 " #{format(status, 10)}#{format(number, 16)}#{description}" 81 end) <> "\n" 82 ) 83 84 {:error, error} -> 85 Mix.raise "Could not start repo #{inspect repo}, error: #{inspect error}" 86 end 87 end 88 89 :ok 90 end 91 92 defp format(content, pad) do 93 content 94 |> to_string 95 |> String.pad_trailing(pad) 96 end 97 end