-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathpow_assent.ecto.gen.migration.ex
More file actions
72 lines (56 loc) · 1.93 KB
/
pow_assent.ecto.gen.migration.ex
File metadata and controls
72 lines (56 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
defmodule Mix.Tasks.PowAssent.Ecto.Gen.Migration do
@shortdoc "Generates user identities migration file"
@moduledoc """
Generates a user identity migrations file.
mix pow_assent.ecto.gen.migration -r MyApp.Repo
mix pow_assent.ecto.gen.migration -r MyApp.Repo Accounts.Identity identities
This generator will add a migration file in `priv/repo/migrations` for the
`user_identities` table
## Arguments
* `-r`, `--repo` - the repo module
* `--binary-id` - use binary id for primary key
* `--users-table` - what users table to reference, defaults to "users"
"""
use Mix.Task
alias PowAssent.Ecto.UserIdentities.Schema.Migration, as: UserIdentitiesMigration
alias Mix.{Ecto, Pow, Pow.Ecto.Migration, PowAssent}
@switches [binary_id: :boolean, users_table: :string]
@default_opts [binary_id: false, users_table: "users"]
@mix_task "pow_assent.ecto.gen.migration"
@impl true
def run(args) do
Pow.no_umbrella!(@mix_task)
args
|> Pow.parse_options(@switches, @default_opts)
|> parse()
|> create_migration_files(args)
end
defp parse({config, parsed, _invalid}) do
parsed
|> PowAssent.validate_schema_args!(@mix_task)
|> Map.merge(config)
end
defp create_migration_files(config, args) do
args
|> Ecto.parse_repo()
|> Enum.map(&Ecto.ensure_repo(&1, args))
|> Enum.map(&Map.put(config, :repo, &1))
|> Enum.each(&create_migration_file/1)
end
defp create_migration_file(%{
repo: repo,
binary_id: binary_id,
users_table: users_table,
schema_plural: schema_plural
}) do
context_base = Pow.app_base(Pow.otp_app())
schema =
UserIdentitiesMigration.new(context_base, schema_plural,
repo: repo,
binary_id: binary_id,
users_table: users_table
)
content = UserIdentitiesMigration.gen(schema)
Migration.create_migration_file(repo, schema.migration_name, content)
end
end