Current section
Files
Jump to
Current section
Files
lib/mix/tasks/wallet_passes.gen.migration.ex
defmodule Mix.Tasks.WalletPasses.Gen.Migration do
@shortdoc "Generates WalletPasses Ecto migrations"
@moduledoc """
Generates the Ecto migrations for WalletPasses tables.
$ mix wallet_passes.gen.migration
## Options
* `--repo` - the repo to generate migrations for (defaults to the app's repo)
"""
use Mix.Task
import Mix.Ecto
import Mix.Generator
@templates_path "priv/templates/wallet_passes.gen.migration"
@impl true
def run(args) do
repos = parse_repo(args)
Enum.each(repos, fn repo ->
ensure_repo(repo, args)
path = Ecto.Migrator.migrations_path(repo)
migrations = [
{"apple_passes_migration.exs.eex", "create_wallet_passes_apple"},
{"google_passes_migration.exs.eex", "create_wallet_passes_google"},
{"device_registrations_migration.exs.eex", "create_wallet_pass_device_registrations"},
{"indexes_migration.exs.eex", "add_wallet_passes_indexes"},
{"validate_fk_migration.exs.eex", "validate_wallet_passes_foreign_keys"},
]
Enum.each(migrations, fn {template, name} ->
source = Application.app_dir(:wallet_passes, Path.join(@templates_path, template))
generated = EEx.eval_file(source, assigns: [repo: repo])
timestamp = timestamp()
filename = "#{timestamp}_#{name}.exs"
file = Path.join(path, filename)
create_file(file, generated)
# Small delay to ensure unique timestamps
Process.sleep(1000)
end)
end)
end
defp timestamp do
{{y, m, d}, {hh, mm, ss}} = :calendar.universal_time()
"#{y}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss)}"
end
defp pad(i) when i < 10, do: <<?0, ?0 + i>>
defp pad(i), do: to_string(i)
end