ecto.dump.ex (2315B)
1 defmodule Mix.Tasks.Ecto.Dump do 2 use Mix.Task 3 import Mix.Ecto 4 import Mix.EctoSQL 5 6 @shortdoc "Dumps the repository database structure" 7 @default_opts [quiet: false] 8 9 @aliases [ 10 d: :dump_path, 11 q: :quiet, 12 r: :repo 13 ] 14 15 @switches [ 16 dump_path: :string, 17 quiet: :boolean, 18 repo: [:string, :keep], 19 no_compile: :boolean, 20 no_deps_check: :boolean 21 ] 22 23 @moduledoc """ 24 Dumps the current environment's database structure for the 25 given repository into a structure file. 26 27 The repository must be set under `:ecto_repos` in the 28 current app configuration or given via the `-r` option. 29 30 This task needs some shell utility to be present on the machine 31 running the task. 32 33 Database | Utility needed 34 :--------- | :------------- 35 PostgreSQL | pg_dump 36 MySQL | mysqldump 37 38 ## Example 39 40 $ mix ecto.dump 41 42 ## Command line options 43 44 * `-r`, `--repo` - the repo to load the structure info from 45 * `-d`, `--dump-path` - the path of the dump file to create 46 * `-q`, `--quiet` - run the command quietly 47 * `--no-compile` - does not compile applications before dumping 48 * `--no-deps-check` - does not check dependencies before dumping 49 """ 50 51 @impl true 52 def run(args) do 53 {opts, _} = OptionParser.parse! args, strict: @switches, aliases: @aliases 54 opts = Keyword.merge(@default_opts, opts) 55 56 Enum.each parse_repo(args), fn repo -> 57 ensure_repo(repo, args) 58 ensure_implements(repo.__adapter__(), Ecto.Adapter.Structure, 59 "dump structure for #{inspect repo}") 60 61 migration_repo = repo.config()[:migration_repo] || repo 62 63 for repo <- Enum.uniq([repo, migration_repo]) do 64 config = Keyword.merge(repo.config(), opts) 65 66 case repo.__adapter__().structure_dump(source_repo_priv(repo), config) do 67 {:ok, location} -> 68 unless opts[:quiet] do 69 Mix.shell().info "The structure for #{inspect repo} has been dumped to #{location}" 70 end 71 {:error, term} when is_binary(term) -> 72 Mix.raise "The structure for #{inspect repo} couldn't be dumped: #{term}" 73 {:error, term} -> 74 Mix.raise "The structure for #{inspect repo} couldn't be dumped: #{inspect term}" 75 end 76 end 77 end 78 end 79 end