diff --git a/CHANGELOG.md b/CHANGELOG.md index 6871e85ff6..b2441f6213 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/lightning/accounts.ex b/lib/lightning/accounts.ex index 16f3c5b97a..435ac08b3a 100644 --- a/lib/lightning/accounts.ex +++ b/lib/lightning/accounts.ex @@ -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 diff --git a/lib/lightning/accounts/user_backup_code.ex b/lib/lightning/accounts/user_backup_code.ex index 5e74a0cdf8..70b9a84f5a 100644 --- a/lib/lightning/accounts/user_backup_code.ex +++ b/lib/lightning/accounts/user_backup_code.ex @@ -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() diff --git a/lib/lightning/setup_utils.ex b/lib/lightning/setup_utils.ex index 6709561f48..5d62125774 100644 --- a/lib/lightning/setup_utils.ex +++ b/lib/lightning/setup_utils.ex @@ -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) diff --git a/lib/lightning/workflows/job.ex b/lib/lightning/workflows/job.ex index 49f8e82cea..1fe010d504 100644 --- a/lib/lightning/workflows/job.ex +++ b/lib/lightning/workflows/job.ex @@ -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 @@ -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 diff --git a/priv/repo/migrations/20231208075606_naive_to_utc_datetimes.exs b/priv/repo/migrations/20231208075606_naive_to_utc_datetimes.exs new file mode 100644 index 0000000000..3f2899e372 --- /dev/null +++ b/priv/repo/migrations/20231208075606_naive_to_utc_datetimes.exs @@ -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 diff --git a/test/lightning/accounts_test.exs b/test/lightning/accounts_test.exs index 93e11422c9..44ca71727b 100644 --- a/test/lightning/accounts_test.exs +++ b/test/lightning/accounts_test.exs @@ -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 @@ -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 @@ -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 @@ -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 @@ -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)