Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update phx.gen.auth timestamps to match config #5794

Merged
merged 5 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions priv/templates/phx.gen.auth/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ defmodule <%= inspect schema.repo %>.Migrations.Create<%= Macro.camelize(schema.
<%= if schema.binary_id do %> add :id, :binary_id, primary_key: true
<% end %> <%= migration.column_definitions[:email] %>
add :hashed_password, :string, null: false
add :confirmed_at, :naive_datetime
add :confirmed_at, <%= inspect schema.timestamp_type %>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would output one of :naive_datetime, :utc_datetime or :utc_datetime_usec.

I understand from the first note in https://hexdocs.pm/ecto/Ecto.Schema.html#module-primitive-types that those are valid values both in the schema definition and in the migration file.

When using database migrations provided by "Ecto SQL", you can pass your Ecto type as the column type. However, note the same Ecto type may support multiple database types. For example, all of :varchar, :text, :bytea, etc. translate to Ecto's :string. Similarly, Ecto's :decimal can be used for :numeric and other database types. For more information, see all migration types.


timestamps(<%= if schema.timestamp_type != :naive_datetime, do: "type: #{inspect schema.timestamp_type}" %>)
end

Expand All @@ -20,7 +21,8 @@ defmodule <%= inspect schema.repo %>.Migrations.Create<%= Macro.camelize(schema.
<%= migration.column_definitions[:token] %>
add :context, :string, null: false
add :sent_to, :string
timestamps(updated_at: false)

timestamps(<%= if schema.timestamp_type != :naive_datetime, do: "type: #{inspect schema.timestamp_type}, " %>updated_at: false)
end

create index(:<%= schema.table %>_tokens, [:<%= schema.singular %>_id])
Expand Down
9 changes: 6 additions & 3 deletions priv/templates/phx.gen.auth/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule <%= inspect schema.module %> do
field :email, :string
field :password, :string, virtual: true, redact: true
field :hashed_password, :string, redact: true
field :confirmed_at, :naive_datetime
field :confirmed_at, <%= inspect schema.timestamp_type %>

timestamps(<%= if schema.timestamp_type != :naive_datetime, do: "type: #{inspect schema.timestamp_type}" %>)
end
Expand Down Expand Up @@ -126,8 +126,11 @@ defmodule <%= inspect schema.module %> do
Confirms the account by setting `confirmed_at`.
"""
def confirm_changeset(<%= schema.singular %>) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
change(<%= schema.singular %>, confirmed_at: now)
<%= case schema.timestamp_type do %>
<% :naive_datetime -> %>now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
<% :utc_datetime -> %>now = DateTime.utc_now() |> DateTime.truncate(:second)
<% :utc_datetime_usec -> %>now = DateTime.utc_now() |> DateTime.truncate(:microsecond)
<% end %>change(<%= schema.singular %>, confirmed_at: now)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only handling the 3 documented values; formatted so as to not produce extraneous empty lines when rendered.

I noticed Elixir 1.15 supports the shorter DateTime.utc_now(:second), but I assume that since phx.new generates a mix.exs requiring Elixir 1.14 then there's no reason to break compatibility right now.

end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion priv/templates/phx.gen.auth/schema_token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule <%= inspect schema.module %>Token do
field :sent_to, :string
belongs_to :<%= schema.singular %>, <%= inspect schema.module %>

timestamps(updated_at: false)
timestamps(<%= if schema.timestamp_type != :naive_datetime, do: "type: #{inspect schema.timestamp_type}, " %>updated_at: false)
end

@doc """
Expand Down
12 changes: 9 additions & 3 deletions test/mix/tasks/phx.gen.auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -950,12 +950,18 @@ defmodule Mix.Tasks.Phx.Gen.AuthTest do

assert_file migration, fn file ->
assert file =~ "timestamps(type: :utc_datetime)"
assert file =~ "timestamps(type: :utc_datetime, updated_at: false)"
end


assert_file("lib/my_app/accounts/user.ex", fn file ->
assert_file "lib/my_app/accounts/user.ex", fn file ->
assert file =~ "field :confirmed_at, :utc_datetime"
assert file =~ "timestamps(type: :utc_datetime)"
end)
assert file =~ "now = DateTime.utc_now() |> DateTime.truncate(:second)"
end

assert_file "lib/my_app/accounts/user_token.ex", fn file ->
assert file =~ "timestamps(type: :utc_datetime, updated_at: false)"
end
end)
end)
end
Expand Down