ecto.create.ex (2004B)
1 defmodule Mix.Tasks.Ecto.Create do 2 use Mix.Task 3 import Mix.Ecto 4 5 @shortdoc "Creates the repository storage" 6 7 @switches [ 8 quiet: :boolean, 9 repo: [:string, :keep], 10 no_compile: :boolean, 11 no_deps_check: :boolean 12 ] 13 14 @aliases [ 15 r: :repo, 16 q: :quiet 17 ] 18 19 @moduledoc """ 20 Create the storage for the given repository. 21 22 The repositories to create are the ones specified under the 23 `:ecto_repos` option in the current app configuration. However, 24 if the `-r` option is given, it replaces the `:ecto_repos` config. 25 26 Since Ecto tasks can only be executed once, if you need to create 27 multiple repositories, set `:ecto_repos` accordingly or pass the `-r` 28 flag multiple times. 29 30 ## Examples 31 32 $ mix ecto.create 33 $ mix ecto.create -r Custom.Repo 34 35 ## Command line options 36 37 * `-r`, `--repo` - the repo to create 38 * `--quiet` - do not log output 39 * `--no-compile` - do not compile before creating 40 * `--no-deps-check` - do not compile before creating 41 42 """ 43 44 @impl true 45 def run(args) do 46 repos = parse_repo(args) 47 {opts, _} = OptionParser.parse! args, strict: @switches, aliases: @aliases 48 49 Enum.each repos, fn repo -> 50 ensure_repo(repo, args) 51 ensure_implements(repo.__adapter__, Ecto.Adapter.Storage, 52 "create storage for #{inspect repo}") 53 case repo.__adapter__.storage_up(repo.config) do 54 :ok -> 55 unless opts[:quiet] do 56 Mix.shell().info "The database for #{inspect repo} has been created" 57 end 58 {:error, :already_up} -> 59 unless opts[:quiet] do 60 Mix.shell().info "The database for #{inspect repo} has already been created" 61 end 62 {:error, term} when is_binary(term) -> 63 Mix.raise "The database for #{inspect repo} couldn't be created: #{term}" 64 {:error, term} -> 65 Mix.raise "The database for #{inspect repo} couldn't be created: #{inspect term}" 66 end 67 end 68 end 69 end