zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

ecto.load.ex (3643B)


      1 defmodule Mix.Tasks.Ecto.Load do
      2   use Mix.Task
      3   import Mix.Ecto
      4   import Mix.EctoSQL
      5 
      6   @shortdoc "Loads previously dumped database structure"
      7   @default_opts [force: false, quiet: false]
      8 
      9   @aliases [
     10     d: :dump_path,
     11     f: :force,
     12     q: :quiet,
     13     r: :repo
     14   ]
     15 
     16   @switches [
     17     dump_path: :string,
     18     force: :boolean,
     19     quiet: :boolean,
     20     repo: [:string, :keep],
     21     no_compile: :boolean,
     22     no_deps_check: :boolean,
     23     skip_if_loaded: :boolean
     24   ]
     25 
     26   @moduledoc """
     27   Loads the current environment's database structure for the
     28   given repository from a previously dumped structure file.
     29 
     30   The repository must be set under `:ecto_repos` in the
     31   current app configuration or given via the `-r` option.
     32 
     33   This task needs some shell utility to be present on the machine
     34   running the task.
     35 
     36    Database   | Utility needed
     37    :--------- | :-------------
     38    PostgreSQL | psql
     39    MySQL      | mysql
     40 
     41   ## Example
     42 
     43       $ mix ecto.load
     44 
     45   ## Command line options
     46 
     47     * `-r`, `--repo` - the repo to load the structure info into
     48     * `-d`, `--dump-path` - the path of the dump file to load from
     49     * `-q`, `--quiet` - run the command quietly
     50     * `-f`, `--force` - do not ask for confirmation when loading data.
     51       Configuration is asked only when `:start_permanent` is set to true
     52       (typically in production)
     53     * `--no-compile` - does not compile applications before loading
     54     * `--no-deps-check` - does not check dependencies before loading
     55     * `--skip-if-loaded` - does not load the dump file if the repo has the migrations table up
     56   """
     57 
     58   @impl true
     59   def run(args, table_exists? \\ &Ecto.Adapters.SQL.table_exists?/2) do
     60     {opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
     61     opts = Keyword.merge(@default_opts, opts)
     62 
     63     Enum.each(parse_repo(args), fn repo ->
     64       ensure_repo(repo, args)
     65 
     66       ensure_implements(
     67         repo.__adapter__(),
     68         Ecto.Adapter.Structure,
     69         "load structure for #{inspect(repo)}"
     70       )
     71 
     72       {migration_repo, source} = Ecto.Migration.SchemaMigration.get_repo_and_source(repo, repo.config())
     73       {:ok, loaded?, _} = Ecto.Migrator.with_repo(migration_repo, &table_exists?.(&1, source))
     74 
     75       for repo <- Enum.uniq([repo, migration_repo]) do
     76         cond do
     77           loaded? and opts[:skip_if_loaded] ->
     78             :ok
     79 
     80           (skip_safety_warnings?() and not loaded?) or opts[:force] or confirm_load(repo, loaded?) ->
     81             load_structure(repo, opts)
     82 
     83           true ->
     84             :ok
     85         end
     86       end
     87     end)
     88   end
     89 
     90   defp skip_safety_warnings? do
     91     Mix.Project.config()[:start_permanent] != true
     92   end
     93 
     94   defp confirm_load(repo, false) do
     95     Mix.shell().yes?(
     96       "Are you sure you want to load a new structure for #{inspect(repo)}? Any existing data in this repo may be lost."
     97     )
     98   end
     99 
    100   defp confirm_load(repo, true) do
    101     Mix.shell().yes?("""
    102     It looks like a structure was already loaded for #{inspect(repo)}. Any attempt to load it again might fail.
    103     Are you sure you want to proceed?
    104     """)
    105   end
    106 
    107   defp load_structure(repo, opts) do
    108     config = Keyword.merge(repo.config(), opts)
    109 
    110     case repo.__adapter__().structure_load(source_repo_priv(repo), config) do
    111       {:ok, location} ->
    112         unless opts[:quiet] do
    113           Mix.shell().info "The structure for #{inspect repo} has been loaded from #{location}"
    114         end
    115       {:error, term} when is_binary(term) ->
    116         Mix.raise "The structure for #{inspect repo} couldn't be loaded: #{term}"
    117       {:error, term} ->
    118         Mix.raise "The structure for #{inspect repo} couldn't be loaded: #{inspect term}"
    119     end
    120   end
    121 end