Skip to content

Commit

Permalink
Change Job and UserBackupCode datetime fields to use UTC format
Browse files Browse the repository at this point in the history
- There is a migration to change `user_backup_codes.used_at` to a UTC datetime.
- The Job timestamps were already UTC datetimes but the model was set to Naive.
  • Loading branch information
stuartc committed Dec 8, 2023
1 parent 098bcb1 commit 7a3746c
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 71 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ and this project adheres to
### Changed

- Updated CLI to 0.4.10 (fixes logging)
- Changed UserBackupToken model to use UTC timestamps (6563cb77)

### Fixed

- Adjusted z-index for Monaco Editor's sibling element to resolve layout
conflict [#1329](https://github.com/OpenFn/Lightning/issues/1329)
- Demo script sets up example Runs with their log lines in a consistant order.
[#1487](https://github.com/OpenFn/Lightning/issues/1487)

## [v0.11.0] - 2023-12-06

Expand Down
3 changes: 1 addition & 2 deletions lib/lightning/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ defmodule Lightning.Accounts do
Enum.map_reduce(backup_codes, false, fn backup, valid? ->
if Plug.Crypto.secure_compare(backup.code, user_code) and
is_nil(backup.used_at) do
{Ecto.Changeset.change(backup, %{used_at: NaiveDateTime.utc_now()}),
true}
{Ecto.Changeset.change(backup, %{used_at: DateTime.utc_now()}), true}
else
{backup, valid?}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lightning/accounts/user_backup_code.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule Lightning.Accounts.UserBackupCode do
redact: true,
autogenerate: {__MODULE__, :generate_backup_code, []}

field :used_at, :naive_datetime_usec
field :used_at, :utc_datetime_usec
belongs_to :user, Lightning.Accounts.User

timestamps()
Expand Down
97 changes: 49 additions & 48 deletions lib/lightning/setup_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -915,57 +915,58 @@ defmodule Lightning.SetupUtils do

Attempts.start_attempt(attempt)

run_params
|> Enum.reduce(%{}, fn run, previous ->
run_id = Ecto.UUID.generate()

input_dataclip_id =
Map.get(
run,
:input_dataclip_id,
Map.get(previous, :output_dataclip_id, input_dataclip.id)
)

Attempts.start_run(%{
attempt_id: attempt.id,
run_id: run_id,
job_id: run.job_id,
input_dataclip_id: input_dataclip_id,
started_at: Ticker.next(ticker)
})

run.log_lines
|> Enum.each(fn line ->
Attempts.append_attempt_log(attempt, %{
_runs =
run_params
|> Enum.reduce(%{}, fn run, previous ->
run_id = Ecto.UUID.generate()

input_dataclip_id =
Map.get(
run,
:input_dataclip_id,
Map.get(previous, :output_dataclip_id, input_dataclip.id)
)

Attempts.start_run(%{
attempt_id: attempt.id,
run_id: run_id,
message: line.message,
timestamp: Ticker.next(ticker)
job_id: run.job_id,
input_dataclip_id: input_dataclip_id,
started_at: Ticker.next(ticker)
})
end)

complete_run_params =
%{
attempt_id: attempt.id,
project_id: workflow.project_id,
run_id: run_id,
reason: run.exit_reason,
finished_at: Ticker.next(ticker)
}
|> Map.merge(
if run[:output_dataclip] do
%{
output_dataclip_id: Ecto.UUID.generate(),
output_dataclip: run.output_dataclip
}
else
%{}
end
)

{:ok, run} = Attempts.complete_run(complete_run_params)

run
end)
run.log_lines
|> Enum.each(fn line ->
Attempts.append_attempt_log(attempt, %{
run_id: run_id,
message: line.message,
timestamp: Ticker.next(ticker)
})
end)

complete_run_params =
%{
attempt_id: attempt.id,
project_id: workflow.project_id,
run_id: run_id,
reason: run.exit_reason,
finished_at: Ticker.next(ticker)
}
|> Map.merge(
if run[:output_dataclip] do
%{
output_dataclip_id: Ecto.UUID.generate(),
output_dataclip: run.output_dataclip
}
else
%{}
end
)

{:ok, run} = Attempts.complete_run(complete_run_params)

run
end)

state =
List.last(run_params)
Expand Down
27 changes: 12 additions & 15 deletions lib/lightning/workflows/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Lightning.Workflows.Job do

field :delete, :boolean, virtual: true

timestamps(type: :naive_datetime_usec)
timestamps(type: :utc_datetime_usec)
end

def new(attrs \\ %{}) do
Expand All @@ -56,20 +56,17 @@ defmodule Lightning.Workflows.Job do

@doc false
def changeset(job, attrs) do
change =
job
|> cast(attrs, [
:id,
# Note: we can drop inserted_at once there's a reliable way to sort yaml for export
:inserted_at,
:name,
:body,
:adaptor,
:project_credential_id,
:workflow_id
])

change
job
|> cast(attrs, [
:id,
# Note: we can drop inserted_at once there's a reliable way to sort yaml for export
:inserted_at,
:name,
:body,
:adaptor,
:project_credential_id,
:workflow_id
])
|> validate()
|> unique_constraint(:name, name: "jobs_name_workflow_id_index")
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Lightning.Repo.Migrations.NaiveToUtcDatetimes do
use Ecto.Migration

def change do
alter table(:user_backup_codes) do
modify :used_at, :utc_datetime_usec
end
end
end
10 changes: 5 additions & 5 deletions test/lightning/accounts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ defmodule Lightning.AccountsTest do

test "does not update email if token expired", %{user: user, token: token} do
{1, nil} =
Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
Repo.update_all(UserToken, set: [inserted_at: ~U[2020-01-01 00:00:00Z]])

assert Accounts.update_user_email(user, token) == :error
assert Repo.get!(User, user.id).email == user.email
Expand Down Expand Up @@ -1027,7 +1027,7 @@ defmodule Lightning.AccountsTest do

test "does not return user for expired token", %{token: token} do
{1, nil} =
Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
Repo.update_all(UserToken, set: [inserted_at: ~U[2020-01-01 00:00:00Z]])

refute Accounts.get_user_by_auth_token(token)
end
Expand Down Expand Up @@ -1060,7 +1060,7 @@ defmodule Lightning.AccountsTest do

test "does not return user for expired token", %{token: token} do
{1, nil} =
Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
Repo.update_all(UserToken, set: [inserted_at: ~U[2020-01-01 00:00:00Z]])

refute Accounts.get_user_by_session_token(token)
end
Expand Down Expand Up @@ -1147,7 +1147,7 @@ defmodule Lightning.AccountsTest do

test "does not confirm email if token expired", %{user: user, token: token} do
{1, nil} =
Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
Repo.update_all(UserToken, set: [inserted_at: ~U[2020-01-01 00:00:00Z]])

assert Accounts.confirm_user(token) == :error
refute Repo.get!(User, user.id).confirmed_at
Expand Down Expand Up @@ -1201,7 +1201,7 @@ defmodule Lightning.AccountsTest do

test "does not return the user if token expired", %{user: user, token: token} do
{1, nil} =
Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
Repo.update_all(UserToken, set: [inserted_at: ~U[2020-01-01 00:00:00Z]])

refute Accounts.get_user_by_reset_password_token(token)
assert Repo.get_by(UserToken, user_id: user.id)
Expand Down

0 comments on commit 7a3746c

Please sign in to comment.