From fd98017a7df11581fbe614a2dcb91b271af5fcb5 Mon Sep 17 00:00:00 2001 From: Stuart Corbishley Date: Tue, 2 Jul 2024 19:59:10 +0200 Subject: [PATCH] All models to use public schema (#2249) * All models to use public schema * Fix mailer test --- CHANGELOG.md | 2 + lib/lightning/accounts/user.ex | 5 +-- lib/lightning/accounts/user_backup_code.ex | 6 +-- lib/lightning/accounts/user_token.ex | 5 +-- lib/lightning/accounts/user_totp.ex | 6 +-- lib/lightning/auditing/audit.ex | 7 +--- lib/lightning/auth_providers/auth_config.ex | 5 +-- lib/lightning/auth_providers/common.ex | 3 +- lib/lightning/credentials/credential.ex | 8 +--- lib/lightning/credentials/oauth_client.ex | 8 +--- lib/lightning/invocation/dataclip.ex | 5 +-- lib/lightning/invocation/log_line.ex | 5 +-- lib/lightning/invocation/run_step.ex | 6 +-- lib/lightning/invocation/step.ex | 6 +-- lib/lightning/projects/project.ex | 6 +-- lib/lightning/projects/project_credential.ex | 6 +-- .../projects/project_oauth_client.ex | 6 +-- lib/lightning/projects/project_user.ex | 5 +-- lib/lightning/runs/handlers.ex | 6 +-- lib/lightning/runs/run.ex | 5 +-- lib/lightning/runs/run_options.ex | 2 +- lib/lightning/schema.ex | 17 +++++++++ lib/lightning/setup_utils.ex | 16 ++++---- .../daily_report_configuration.ex | 5 +-- lib/lightning/usage_tracking/report.ex | 5 +-- .../project_repo_connection.ex | 6 +-- lib/lightning/workflows/edge.ex | 7 +--- lib/lightning/workflows/job.ex | 6 +-- lib/lightning/workflows/node.ex | 4 +- lib/lightning/workflows/snapshot.ex | 5 +-- lib/lightning/workflows/trigger.ex | 7 +--- .../workflows/webhook_auth_method.ex | 5 +-- lib/lightning/workflows/workflow.ex | 8 +--- lib/lightning/workorders/manual.ex | 4 +- lib/lightning/workorders/search_params.ex | 7 +--- lib/lightning/workorders/workorder.ex | 4 +- .../live/project_live/collaborators.ex | 3 +- mix.exs | 2 +- mix.lock | 10 ++--- test/lightning/projects_test.exs | 37 +++++++++++-------- 40 files changed, 97 insertions(+), 174 deletions(-) create mode 100644 lib/lightning/schema.ex diff --git a/CHANGELOG.md b/CHANGELOG.md index ff345410b8..15f4cc3c1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ and this project adheres to ### Changed +- All models to use the `public` schema. + [#2249](https://github.com/OpenFn/lightning/pull/2249) - In the workflow diagram, smartly update the view when adding new nodes [#2174](https://github.com/OpenFn/lightning/issues/2174) - In the workflow diagram, remove the "autofit" toggle in the control bar diff --git a/lib/lightning/accounts/user.ex b/lib/lightning/accounts/user.ex index d94fc591ad..2ecd98a6a5 100644 --- a/lib/lightning/accounts/user.ex +++ b/lib/lightning/accounts/user.ex @@ -2,9 +2,8 @@ defmodule Lightning.Accounts.User do @moduledoc """ The User model. """ - use Ecto.Schema + use Lightning.Schema - import Ecto.Changeset import Ecto.Query import EctoEnum @@ -19,8 +18,6 @@ defmodule Lightning.Accounts.User do :superuser ]) - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "users" do field :first_name, :string field :last_name, :string diff --git a/lib/lightning/accounts/user_backup_code.ex b/lib/lightning/accounts/user_backup_code.ex index 70b9a84f5a..3d704c428e 100644 --- a/lib/lightning/accounts/user_backup_code.ex +++ b/lib/lightning/accounts/user_backup_code.ex @@ -3,16 +3,12 @@ defmodule Lightning.Accounts.UserBackupCode do User backup codes schema """ - use Ecto.Schema - import Ecto.Changeset + use Lightning.Schema @type t :: %__MODULE__{ id: Ecto.UUID.t() | nil } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id - schema "user_backup_codes" do field :code, Lightning.Encrypted.Binary, redact: true, diff --git a/lib/lightning/accounts/user_token.ex b/lib/lightning/accounts/user_token.ex index 990b972076..d079c35108 100644 --- a/lib/lightning/accounts/user_token.ex +++ b/lib/lightning/accounts/user_token.ex @@ -17,10 +17,9 @@ defmodule Lightning.Accounts.UserToken do session they deem invalid. """ - use Ecto.Schema + use Lightning.Schema use Joken.Config - import Ecto.Changeset import Ecto.Query alias Lightning.Accounts.User @@ -37,8 +36,6 @@ defmodule Lightning.Accounts.UserToken do @sudo_session_validity_in_seconds 60 * 5 @auth_validity_in_seconds 30 - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "user_tokens" do field :token, :binary field :context, :string diff --git a/lib/lightning/accounts/user_totp.ex b/lib/lightning/accounts/user_totp.ex index de142a4717..bc9121271c 100644 --- a/lib/lightning/accounts/user_totp.ex +++ b/lib/lightning/accounts/user_totp.ex @@ -2,8 +2,7 @@ defmodule Lightning.Accounts.UserTOTP do @moduledoc """ User Time based OTPs schema """ - use Ecto.Schema - import Ecto.Changeset + use Lightning.Schema @type t :: %__MODULE__{ id: Ecto.UUID.t() | nil, @@ -13,9 +12,6 @@ defmodule Lightning.Accounts.UserTOTP do Lightning.Accounts.User.t() | Ecto.Association.NotLoaded.t() | nil } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id - schema "user_totps" do field :secret, :binary, redact: true field :code, :string, virtual: true diff --git a/lib/lightning/auditing/audit.ex b/lib/lightning/auditing/audit.ex index 8bf8ddfa2d..2cb77ce7db 100644 --- a/lib/lightning/auditing/audit.ex +++ b/lib/lightning/auditing/audit.ex @@ -2,9 +2,7 @@ defmodule Lightning.Auditing.Audit do @moduledoc """ Macro module to add common model behaviour to a given Ecto model """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema require Ecto.Query require Logger @@ -92,8 +90,7 @@ defmodule Lightning.Auditing.Audit do defmodule Changes do @moduledoc false - use Ecto.Schema - import Ecto.Changeset + use Lightning.Schema @primary_key false embedded_schema do diff --git a/lib/lightning/auth_providers/auth_config.ex b/lib/lightning/auth_providers/auth_config.ex index 964f7c1101..88c6743287 100644 --- a/lib/lightning/auth_providers/auth_config.ex +++ b/lib/lightning/auth_providers/auth_config.ex @@ -2,8 +2,7 @@ defmodule Lightning.AuthProviders.AuthConfig do @moduledoc """ AuthProvider model """ - use Ecto.Schema - import Ecto.Changeset + use Lightning.Schema @type t :: %__MODULE__{ __meta__: Ecto.Schema.Metadata.t(), @@ -14,8 +13,6 @@ defmodule Lightning.AuthProviders.AuthConfig do redirect_uri: String.t() | nil } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "auth_providers" do field :name, :string diff --git a/lib/lightning/auth_providers/common.ex b/lib/lightning/auth_providers/common.ex index ef2d54933b..5f2c5cef75 100644 --- a/lib/lightning/auth_providers/common.ex +++ b/lib/lightning/auth_providers/common.ex @@ -11,8 +11,7 @@ defmodule Lightning.AuthProviders.Common do Defines a schema for OAuth token information. """ - use Ecto.Schema - import Ecto.Changeset + use Lightning.Schema @primary_key false embedded_schema do diff --git a/lib/lightning/credentials/credential.ex b/lib/lightning/credentials/credential.ex index 16e1f62b67..4db7749753 100644 --- a/lib/lightning/credentials/credential.ex +++ b/lib/lightning/credentials/credential.ex @@ -2,9 +2,7 @@ defmodule Lightning.Credentials.Credential do @moduledoc """ The Credential model. """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema alias Lightning.Accounts.User alias Lightning.Credentials.OauthClient @@ -16,8 +14,6 @@ defmodule Lightning.Credentials.Credential do body: nil | %{} } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "credentials" do field :name, :string field :body, Lightning.Encrypted.Map, redact: true @@ -31,7 +27,7 @@ defmodule Lightning.Credentials.Credential do has_many :project_credentials, ProjectCredential has_many :projects, through: [:project_credentials, :project] - timestamps(type: :utc_datetime) + timestamps() end @doc false diff --git a/lib/lightning/credentials/oauth_client.ex b/lib/lightning/credentials/oauth_client.ex index 74a277fd0c..413967e53f 100644 --- a/lib/lightning/credentials/oauth_client.ex +++ b/lib/lightning/credentials/oauth_client.ex @@ -6,9 +6,7 @@ defmodule Lightning.Credentials.OauthClient do projects through relational fields. """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema alias Lightning.Accounts.User alias Lightning.Credentials.Credential @@ -21,8 +19,6 @@ defmodule Lightning.Credentials.OauthClient do name: String.t() } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "oauth_clients" do field :name, :string field :client_id, :string @@ -42,7 +38,7 @@ defmodule Lightning.Credentials.OauthClient do has_many :project_oauth_clients, ProjectOauthClient has_many :projects, through: [:project_oauth_clients, :project] - timestamps(type: :utc_datetime) + timestamps() end @doc """ diff --git a/lib/lightning/invocation/dataclip.ex b/lib/lightning/invocation/dataclip.ex index 7f8a34810d..4e38ae3b11 100644 --- a/lib/lightning/invocation/dataclip.ex +++ b/lib/lightning/invocation/dataclip.ex @@ -19,8 +19,7 @@ defmodule Lightning.Invocation.Dataclip do * `:saved_input` An arbitrary input, created by a user. (Only configuration will be overwritten.) """ - use Ecto.Schema - import Ecto.Changeset + use Lightning.Schema alias Lightning.Invocation.Step alias Lightning.Projects.Project @@ -37,8 +36,6 @@ defmodule Lightning.Invocation.Dataclip do @type source_type :: :http_request | :global | :step_result | :saved_input @source_types [:http_request, :global, :step_result, :saved_input] - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "dataclips" do field :body, :map, load_in_query: false field :request, :map, load_in_query: false diff --git a/lib/lightning/invocation/log_line.ex b/lib/lightning/invocation/log_line.ex index 40fa2ee6a5..0c038ccce2 100644 --- a/lib/lightning/invocation/log_line.ex +++ b/lib/lightning/invocation/log_line.ex @@ -10,9 +10,7 @@ defmodule Lightning.Invocation.LogLine do See [`LogMessage`](`Lightning.LogMessage`) for more information. """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema alias Lightning.Invocation.Step alias Lightning.LogMessage @@ -33,7 +31,6 @@ defmodule Lightning.Invocation.LogLine do only: [:id, :source, :level, :message, :timestamp, :step_id, :run_id]} @primary_key false - @foreign_key_type :binary_id schema "log_lines" do field :id, Ecto.UUID field :source, :string diff --git a/lib/lightning/invocation/run_step.ex b/lib/lightning/invocation/run_step.ex index 00eba4be61..16145795b9 100644 --- a/lib/lightning/invocation/run_step.ex +++ b/lib/lightning/invocation/run_step.ex @@ -2,9 +2,7 @@ defmodule Lightning.RunStep do @moduledoc """ Ecto model for an the Steps in a Run. """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema alias Lightning.Invocation.Step alias Lightning.Run @@ -16,8 +14,6 @@ defmodule Lightning.RunStep do step: Step.t() | Ecto.Association.NotLoaded.t() } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "run_steps" do belongs_to :run, Run belongs_to :step, Step diff --git a/lib/lightning/invocation/step.ex b/lib/lightning/invocation/step.ex index 5ad0dd0744..a2df6b6348 100644 --- a/lib/lightning/invocation/step.ex +++ b/lib/lightning/invocation/step.ex @@ -18,9 +18,7 @@ defmodule Lightning.Invocation.Step do - `"exception"` - `"lost"` """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema alias Lightning.Credentials.Credential alias Lightning.Invocation.Dataclip @@ -37,8 +35,6 @@ defmodule Lightning.Invocation.Step do job: Job.t() | Ecto.Association.NotLoaded.t() | nil } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "steps" do field :exit_reason, :string field :error_type, :string diff --git a/lib/lightning/projects/project.ex b/lib/lightning/projects/project.ex index 53a756b5e8..5d45d68142 100644 --- a/lib/lightning/projects/project.ex +++ b/lib/lightning/projects/project.ex @@ -2,9 +2,7 @@ defmodule Lightning.Projects.Project do @moduledoc """ Project model """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema alias Lightning.Projects.ProjectCredential alias Lightning.Projects.ProjectOauthClient @@ -21,8 +19,6 @@ defmodule Lightning.Projects.Project do @retention_periods [7, 14, 30, 90, 180, 365] - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "projects" do field :name, :string field :description, :string diff --git a/lib/lightning/projects/project_credential.ex b/lib/lightning/projects/project_credential.ex index 5eef396368..447fb38225 100644 --- a/lib/lightning/projects/project_credential.ex +++ b/lib/lightning/projects/project_credential.ex @@ -2,9 +2,7 @@ defmodule Lightning.Projects.ProjectCredential do @moduledoc """ Join table to assign credentials to a project """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema alias Lightning.Credentials.Credential alias Lightning.Projects.Project @@ -16,8 +14,6 @@ defmodule Lightning.Projects.ProjectCredential do project: Project.t() | Ecto.Association.NotLoaded.t() | nil } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "project_credentials" do belongs_to :credential, Credential belongs_to :project, Project diff --git a/lib/lightning/projects/project_oauth_client.ex b/lib/lightning/projects/project_oauth_client.ex index cec38039f4..7f212d2c0a 100644 --- a/lib/lightning/projects/project_oauth_client.ex +++ b/lib/lightning/projects/project_oauth_client.ex @@ -25,9 +25,7 @@ defmodule Lightning.Projects.ProjectOauthClient do rules such as uniqueness of the association. """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema alias Lightning.Credentials.OauthClient alias Lightning.Projects.Project @@ -39,8 +37,6 @@ defmodule Lightning.Projects.ProjectOauthClient do project: Project.t() | Ecto.Association.NotLoaded.t() | nil } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "project_oauth_clients" do belongs_to :oauth_client, OauthClient belongs_to :project, Project diff --git a/lib/lightning/projects/project_user.ex b/lib/lightning/projects/project_user.ex index 3797e4dfdb..e4595c5368 100644 --- a/lib/lightning/projects/project_user.ex +++ b/lib/lightning/projects/project_user.ex @@ -2,9 +2,8 @@ defmodule Lightning.Projects.ProjectUser do @moduledoc """ Join table to assign users to a project """ - use Ecto.Schema + use Lightning.Schema - import Ecto.Changeset import EctoEnum alias Lightning.Accounts.User @@ -31,8 +30,6 @@ defmodule Lightning.Projects.ProjectUser do :monthly ]) - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "project_users" do belongs_to :user, User belongs_to :project, Project diff --git a/lib/lightning/runs/handlers.ex b/lib/lightning/runs/handlers.ex index 2286e0456e..7bff28f182 100644 --- a/lib/lightning/runs/handlers.ex +++ b/lib/lightning/runs/handlers.ex @@ -15,8 +15,7 @@ defmodule Lightning.Runs.Handlers do @moduledoc """ Schema to validate the input attributes of a started step. """ - use Ecto.Schema - import Ecto.Changeset + use Lightning.Schema import Ecto.Query import Lightning.ChangesetUtils @@ -142,8 +141,7 @@ defmodule Lightning.Runs.Handlers do @moduledoc """ Schema to validate the input attributes of a completed step. """ - use Ecto.Schema - import Ecto.Changeset + use Lightning.Schema import Ecto.Query import Lightning.ChangesetUtils diff --git a/lib/lightning/runs/run.ex b/lib/lightning/runs/run.ex index bb2a0319c5..1de2d78021 100644 --- a/lib/lightning/runs/run.ex +++ b/lib/lightning/runs/run.ex @@ -4,9 +4,8 @@ defmodule Lightning.Run do """ - use Ecto.Schema + use Lightning.Schema - import Ecto.Changeset import Lightning.Validators alias Lightning.Accounts.User @@ -45,8 +44,6 @@ defmodule Lightning.Run do work_order: WorkOrder.t() | Ecto.Association.NotLoaded.t() } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "runs" do belongs_to :work_order, WorkOrder diff --git a/lib/lightning/runs/run_options.ex b/lib/lightning/runs/run_options.ex index b5eba75c47..0e50e88f27 100644 --- a/lib/lightning/runs/run_options.ex +++ b/lib/lightning/runs/run_options.ex @@ -3,7 +3,7 @@ defmodule Lightning.Runs.RunOptions do Options that are passed to the worker to control configurable limits and behaviors during run execution and reporting. """ - use Ecto.Schema + use Lightning.Schema @type t :: %__MODULE__{ save_dataclips: boolean(), diff --git a/lib/lightning/schema.ex b/lib/lightning/schema.ex new file mode 100644 index 0000000000..bf22314677 --- /dev/null +++ b/lib/lightning/schema.ex @@ -0,0 +1,17 @@ +defmodule Lightning.Schema do + @moduledoc """ + Defines the database schema and primary key type for Thunderbolt schemas. + """ + defmacro __using__(_opts) do + quote do + use Ecto.Schema + + import Ecto.Changeset + + @schema_prefix "public" + @primary_key {:id, :binary_id, autogenerate: true} + @foreign_key_type :binary_id + @timestamps_opts [type: :utc_datetime] + end + end +end diff --git a/lib/lightning/setup_utils.ex b/lib/lightning/setup_utils.ex index 04b5f58dfb..666f458c84 100644 --- a/lib/lightning/setup_utils.ex +++ b/lib/lightning/setup_utils.ex @@ -849,15 +849,17 @@ defmodule Lightning.SetupUtils do end defp get_most_privileged_user!(project) do - Ecto.assoc(project, :project_users) - |> with_cte("role_ordering", - as: - fragment( + role_ordering_query = + from( + s in fragment( "SELECT * FROM UNNEST(?::varchar[]) WITH ORDINALITY o(role, ord)", ~w[owner admin editor viewer] - ) - ) - |> join(:inner, [pu], o in "role_ordering", on: pu.role == o.role) + ), + select: %{role: s.role, ord: s.ord} + ) + + Ecto.assoc(project, :project_users) + |> join(:inner, [pu], o in ^role_ordering_query, on: pu.role == o.role) |> join(:inner, [pu], u in assoc(pu, :user)) |> order_by([pu, o], asc: o.ord) |> select([pu, _o, u], u) diff --git a/lib/lightning/usage_tracking/daily_report_configuration.ex b/lib/lightning/usage_tracking/daily_report_configuration.ex index 3d035ccbfe..3b72902059 100644 --- a/lib/lightning/usage_tracking/daily_report_configuration.ex +++ b/lib/lightning/usage_tracking/daily_report_configuration.ex @@ -3,11 +3,8 @@ defmodule Lightning.UsageTracking.DailyReportConfiguration do Configuration for the creation of daily reports """ - use Ecto.Schema + use Lightning.Schema - import Ecto.Changeset - - @primary_key {:id, :binary_id, autogenerate: true} schema "usage_tracking_daily_report_configurations" do field :instance_id, Ecto.UUID, autogenerate: true field :tracking_enabled_at, :utc_datetime_usec diff --git a/lib/lightning/usage_tracking/report.ex b/lib/lightning/usage_tracking/report.ex index 81eab46a2e..a8b174ff97 100644 --- a/lib/lightning/usage_tracking/report.ex +++ b/lib/lightning/usage_tracking/report.ex @@ -3,11 +3,8 @@ defmodule Lightning.UsageTracking.Report do Report submitted to Usage Tracker """ - use Ecto.Schema + use Lightning.Schema - import Ecto.Changeset - - @primary_key {:id, :binary_id, autogenerate: true} schema "usage_tracking_reports" do field :data, :map field :submitted, :boolean diff --git a/lib/lightning/version_control/project_repo_connection.ex b/lib/lightning/version_control/project_repo_connection.ex index 0fdaaa962f..c2be9d5ab8 100644 --- a/lib/lightning/version_control/project_repo_connection.ex +++ b/lib/lightning/version_control/project_repo_connection.ex @@ -3,9 +3,7 @@ defmodule Lightning.VersionControl.ProjectRepoConnection do Ecto model for project repo connections """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema alias Lightning.Projects.Project @@ -18,8 +16,6 @@ defmodule Lightning.VersionControl.ProjectRepoConnection do project: nil | Project.t() | Ecto.Association.NotLoaded } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "project_repo_connections" do field :github_installation_id, :string field :repo, :string diff --git a/lib/lightning/workflows/edge.ex b/lib/lightning/workflows/edge.ex index f82bf584b3..bf02c72397 100644 --- a/lib/lightning/workflows/edge.ex +++ b/lib/lightning/workflows/edge.ex @@ -8,8 +8,7 @@ defmodule Lightning.Workflows.Edge do The source of the edge is either a job or a trigger. The target of the edge is always a job. """ - use Ecto.Schema - import Ecto.Changeset + use Lightning.Schema import Lightning.Validators alias Lightning.Workflows.Job @@ -31,8 +30,6 @@ defmodule Lightning.Workflows.Edge do } @conditions [:on_job_success, :on_job_failure, :always, :js_expression] - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "workflow_edges" do belongs_to :workflow, Workflow belongs_to :source_job, Job @@ -47,7 +44,7 @@ defmodule Lightning.Workflows.Edge do field :delete, :boolean, virtual: true - timestamps(type: :utc_datetime) + timestamps() end def new(attrs) do diff --git a/lib/lightning/workflows/job.ex b/lib/lightning/workflows/job.ex index 50198b736c..49d5530bd3 100644 --- a/lib/lightning/workflows/job.ex +++ b/lib/lightning/workflows/job.ex @@ -15,9 +15,7 @@ defmodule Lightning.Workflows.Job do in this context, the front end will ensure a version is stated (`@latest` being the default). """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema alias Lightning.Credentials.Credential alias Lightning.Projects.ProjectCredential @@ -33,8 +31,6 @@ defmodule Lightning.Workflows.Job do workflow: nil | Workflow.t() | Ecto.Association.NotLoaded.t() } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "jobs" do field :body, :string diff --git a/lib/lightning/workflows/node.ex b/lib/lightning/workflows/node.ex index 3608e7bfef..ffe67ebe1a 100644 --- a/lib/lightning/workflows/node.ex +++ b/lib/lightning/workflows/node.ex @@ -2,14 +2,12 @@ defmodule Lightning.Workflows.Node do @moduledoc """ Represents a node in a workflow graph. """ - use Ecto.Schema + use Lightning.Schema alias Lightning.Workflows.Job alias Lightning.Workflows.Trigger alias Lightning.Workflows.Workflow - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "workflow_nodes" do belongs_to :workflow, Workflow belongs_to :job, Job diff --git a/lib/lightning/workflows/snapshot.ex b/lib/lightning/workflows/snapshot.ex index 4dd4ebd974..4632a06c28 100644 --- a/lib/lightning/workflows/snapshot.ex +++ b/lib/lightning/workflows/snapshot.ex @@ -4,9 +4,8 @@ defmodule Lightning.Workflows.Snapshot do Snapshots are a way to store the state of a workflow at a given point in time. """ - use Ecto.Schema + use Lightning.Schema - import Ecto.Changeset import Ecto.Query alias Ecto.Multi @@ -26,8 +25,6 @@ defmodule Lightning.Workflows.Snapshot do edges: [%Lightning.Workflows.Snapshot.Edge{}] } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "workflow_snapshots" do belongs_to :workflow, Workflow field :name, :string diff --git a/lib/lightning/workflows/trigger.ex b/lib/lightning/workflows/trigger.ex index 00816393bc..ae97a3d40e 100644 --- a/lib/lightning/workflows/trigger.ex +++ b/lib/lightning/workflows/trigger.ex @@ -11,8 +11,7 @@ defmodule Lightning.Workflows.Trigger do A webhook trigger allows a Job to invoked (via `Lightning.Invocation`) when it's endpoint is called. """ - use Ecto.Schema - import Ecto.Changeset + use Lightning.Schema import Ecto.Query alias Lightning.Workflows.Workflow @@ -25,8 +24,6 @@ defmodule Lightning.Workflows.Trigger do @trigger_types [:webhook, :cron] @type trigger_type :: :webhook | :cron - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "triggers" do field :comment, :string field :custom_path, :string @@ -45,7 +42,7 @@ defmodule Lightning.Workflows.Trigger do join_through: "trigger_webhook_auth_methods", on_replace: :delete - timestamps(type: :utc_datetime) + timestamps() end def new(attrs) do diff --git a/lib/lightning/workflows/webhook_auth_method.ex b/lib/lightning/workflows/webhook_auth_method.ex index 497f590376..f1e6ebe023 100644 --- a/lib/lightning/workflows/webhook_auth_method.ex +++ b/lib/lightning/workflows/webhook_auth_method.ex @@ -24,14 +24,11 @@ defmodule Lightning.Workflows.WebhookAuthMethod do The `valid_password?/2` function is provided to verify passwords and it avoids timing attacks by using `Bcrypt.no_user_verify/0` when there is no webhook_auth_method or the webhook_auth_method doesn't have a password. """ - use Ecto.Schema - import Ecto.Changeset + use Lightning.Schema @type t :: %__MODULE__{} @auth_types [:basic, :api] - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "webhook_auth_methods" do field :name, :string diff --git a/lib/lightning/workflows/workflow.ex b/lib/lightning/workflows/workflow.ex index c1ef721473..2baeb47bc5 100644 --- a/lib/lightning/workflows/workflow.ex +++ b/lib/lightning/workflows/workflow.ex @@ -7,9 +7,7 @@ defmodule Lightning.Workflows.Workflow do * `name` A plain text identifier """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema alias Lightning.Projects.Project alias Lightning.Workflows.Edge @@ -25,8 +23,6 @@ defmodule Lightning.Workflows.Workflow do project: nil | Project.t() | Ecto.Association.NotLoaded.t() } - @primary_key {:id, :binary_id, autogenerate: true} - @foreign_key_type :binary_id schema "workflows" do field :name, :string @@ -46,7 +42,7 @@ defmodule Lightning.Workflows.Workflow do field :delete, :boolean, virtual: true - timestamps(type: :utc_datetime) + timestamps() end @doc false diff --git a/lib/lightning/workorders/manual.ex b/lib/lightning/workorders/manual.ex index 0463164df9..caec5838d5 100644 --- a/lib/lightning/workorders/manual.ex +++ b/lib/lightning/workorders/manual.ex @@ -3,9 +3,7 @@ defmodule Lightning.WorkOrders.Manual do A model is used to build WorkOrders with custom input data. """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema alias Lightning.Validators diff --git a/lib/lightning/workorders/search_params.ex b/lib/lightning/workorders/search_params.ex index 530133cd05..8a5d59ff35 100644 --- a/lib/lightning/workorders/search_params.ex +++ b/lib/lightning/workorders/search_params.ex @@ -4,13 +4,10 @@ defmodule Lightning.WorkOrders.SearchParams do a query to the database. """ - # What should be defaults - - use Ecto.Schema - import Ecto.Changeset + use Lightning.Schema @statuses ~w(pending running success failed crashed killed cancelled lost exception) - @statuses_set MapSet.new(@statuses, fn x -> String.to_existing_atom(x) end) + @statuses_set MapSet.new(@statuses) @search_fields ~w(id body log) defmacro status_list do diff --git a/lib/lightning/workorders/workorder.ex b/lib/lightning/workorders/workorder.ex index e264ddd67b..7d7772a492 100644 --- a/lib/lightning/workorders/workorder.ex +++ b/lib/lightning/workorders/workorder.ex @@ -3,9 +3,7 @@ defmodule Lightning.WorkOrder do Ecto model for WorkOrders. """ - use Ecto.Schema - - import Ecto.Changeset + use Lightning.Schema alias Lightning.Invocation.Dataclip alias Lightning.Run diff --git a/lib/lightning_web/live/project_live/collaborators.ex b/lib/lightning_web/live/project_live/collaborators.ex index 23c371b33c..8542f48a51 100644 --- a/lib/lightning_web/live/project_live/collaborators.ex +++ b/lib/lightning_web/live/project_live/collaborators.ex @@ -4,8 +4,7 @@ defmodule LightningWeb.ProjectLive.Collaborators do It is mirroring the `Project -> ProjectUser` relationship. """ - use Ecto.Schema - import Ecto.Changeset + use Lightning.Schema embedded_schema do embeds_many :collaborators, Collaborator, on_replace: :delete do diff --git a/mix.exs b/mix.exs index 28f6281e90..7019346d8b 100644 --- a/mix.exs +++ b/mix.exs @@ -77,7 +77,7 @@ defmodule Lightning.MixProject do {:dialyxir, "~> 1.4.2", only: [:test, :dev], runtime: false}, {:ecto_enum, "~> 1.4"}, {:ecto_psql_extras, "~> 0.7.14"}, - {:ecto_sql, "~> 3.6"}, + {:ecto_sql, "~> 3.11"}, {:esbuild, "~> 0.3", runtime: Mix.env() == :dev}, {:ex_doc, "~> 0.28", only: :dev, runtime: false}, {:ex_json_schema, "~> 0.9.1"}, diff --git a/mix.lock b/mix.lock index e6371f36b8..3760457f22 100644 --- a/mix.lock +++ b/mix.lock @@ -19,16 +19,16 @@ "credo": {:hex, :credo, "1.7.3", "05bb11eaf2f2b8db370ecaa6a6bda2ec49b2acd5e0418bc106b73b07128c0436", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "35ea675a094c934c22fb1dca3696f3c31f2728ae6ef5a53b5d648c11180a4535"}, "crontab": {:hex, :crontab, "1.1.13", "3bad04f050b9f7f1c237809e42223999c150656a6b2afbbfef597d56df2144c5", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "d67441bec989640e3afb94e123f45a2bc42d76e02988c9613885dc3d01cf7085"}, "ctx": {:hex, :ctx, "0.6.0", "8ff88b70e6400c4df90142e7f130625b82086077a45364a78d208ed3ed53c7fe", [:rebar3], [], "hexpm", "a14ed2d1b67723dbebbe423b28d7615eb0bdcba6ff28f2d1f1b0a7e1d4aa5fc2"}, - "db_connection": {:hex, :db_connection, "2.6.0", "77d835c472b5b67fc4f29556dee74bf511bbafecdcaf98c27d27fa5918152086", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c2f992d15725e721ec7fbc1189d4ecdb8afef76648c746a8e1cad35e3b8a35f3"}, + "db_connection": {:hex, :db_connection, "2.7.0", "b99faa9291bb09892c7da373bb82cba59aefa9b36300f6145c5f201c7adf48ec", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "dcf08f31b2701f857dfc787fbad78223d61a32204f217f15e881dd93e4bdd3ff"}, "decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"}, "dialyxir": {:hex, :dialyxir, "1.4.3", "edd0124f358f0b9e95bfe53a9fcf806d615d8f838e2202a9f430d59566b6b53b", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "bf2cfb75cd5c5006bec30141b131663299c661a864ec7fbbc72dfa557487a986"}, "dotenvy": {:hex, :dotenvy, "0.8.0", "777486ad485668317c56afc53a7cbcd74f43e4e34588ba8e95a73e15a360050e", [:mix], [], "hexpm", "1f535066282388cbd109743d337ac46ff0708195780d4b5778bb83491ab1b654"}, "earmark": {:hex, :earmark, "1.4.46", "8c7287bd3137e99d26ae4643e5b7ef2129a260e3dcf41f251750cb4563c8fb81", [:mix], [], "hexpm", "798d86db3d79964e759ddc0c077d5eb254968ed426399fbf5a62de2b5ff8910a"}, "earmark_parser": {:hex, :earmark_parser, "1.4.37", "2ad73550e27c8946648b06905a57e4d454e4d7229c2dafa72a0348c99d8be5f7", [:mix], [], "hexpm", "6b19783f2802f039806f375610faa22da130b8edc21209d0bff47918bb48360e"}, - "ecto": {:hex, :ecto, "3.10.3", "eb2ae2eecd210b4eb8bece1217b297ad4ff824b4384c0e3fdd28aaf96edd6135", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "44bec74e2364d491d70f7e42cd0d690922659d329f6465e89feb8a34e8cd3433"}, + "ecto": {:hex, :ecto, "3.11.2", "e1d26be989db350a633667c5cda9c3d115ae779b66da567c68c80cfb26a8c9ee", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3c38bca2c6f8d8023f2145326cc8a80100c3ffe4dcbd9842ff867f7fc6156c65"}, "ecto_enum": {:hex, :ecto_enum, "1.4.0", "d14b00e04b974afc69c251632d1e49594d899067ee2b376277efd8233027aec8", [:mix], [{:ecto, ">= 3.0.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "> 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:mariaex, ">= 0.0.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm", "8fb55c087181c2b15eee406519dc22578fa60dd82c088be376d0010172764ee4"}, "ecto_psql_extras": {:hex, :ecto_psql_extras, "0.7.14", "7a20cfe913b0476542b43870e67386461258734896035e3f284039fd18bd4c4c", [:mix], [{:ecto_sql, "~> 3.7", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.16.0 or ~> 0.17.0", [hex: :postgrex, repo: "hexpm", optional: false]}, {:table_rex, "~> 3.1.1", [hex: :table_rex, repo: "hexpm", optional: false]}], "hexpm", "22f5f98592dd597db9416fcef00effae0787669fdcb6faf447e982b553798e98"}, - "ecto_sql": {:hex, :ecto_sql, "3.10.2", "6b98b46534b5c2f8b8b5f03f126e75e2a73c64f3c071149d32987a5378b0fdbd", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.10.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 0.17.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "68c018debca57cb9235e3889affdaec7a10616a4e3a80c99fa1d01fdafaa9007"}, + "ecto_sql": {:hex, :ecto_sql, "3.11.3", "4eb7348ff8101fbc4e6bbc5a4404a24fecbe73a3372d16569526b0cf34ebc195", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.11.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e5f36e3d736b99c7fee3e631333b8394ade4bafe9d96d35669fca2d81c2be928"}, "elixir_make": {:hex, :elixir_make, "0.7.7", "7128c60c2476019ed978210c245badf08b03dbec4f24d05790ef791da11aa17c", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "5bc19fff950fad52bbe5f211b12db9ec82c6b34a9647da0c2224b8b8464c7e6c"}, "enacl": {:git, "https://github.com/aeternity/enacl.git", "4eb7ec70084ba7c87b1af8797c4c4e90c84f95a2", [branch: "master"]}, "eqrcode": {:hex, :eqrcode, "0.1.10", "6294fece9d68ad64eef1c3c92cf111cfd6469f4fbf230a2d4cc905a682178f3f", [:mix], [], "hexpm", "da30e373c36a0fd37ab6f58664b16029919896d6c45a68a95cc4d713e81076f1"}, @@ -54,7 +54,7 @@ "hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"}, "httpoison": {:hex, :httpoison, "1.8.2", "9eb9c63ae289296a544842ef816a85d881d4a31f518a0fec089aaa744beae290", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "2bb350d26972e30c96e2ca74a1aaf8293d61d0742ff17f01e0279fef11599921"}, "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, - "jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"}, + "jason": {:hex, :jason, "1.4.3", "d3f984eeb96fe53b85d20e0b049f03e57d075b5acda3ac8d465c969a2536c17b", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "9a90e868927f7c777689baa16d86f4d0e086d968db5c05d917ccff6d443e58a3"}, "joken": {:hex, :joken, "2.6.0", "b9dd9b6d52e3e6fcb6c65e151ad38bf4bc286382b5b6f97079c47ade6b1bcc6a", [:mix], [{:jose, "~> 1.11.5", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm", "5a95b05a71cd0b54abd35378aeb1d487a23a52c324fa7efdffc512b655b5aaa7"}, "jose": {:hex, :jose, "1.11.6", "613fda82552128aa6fb804682e3a616f4bc15565a048dabd05b1ebd5827ed965", [:mix, :rebar3], [], "hexpm", "6275cb75504f9c1e60eeacb771adfeee4905a9e182103aa59b53fed651ff9738"}, "jsonpatch": {:hex, :jsonpatch, "1.0.2", "5fee0e0591d0cecc20dd9e25bcd9b20c4316343793b57e26bd3ce0419e3c1820", [:make, :mix], [], "hexpm", "5e56e626b4bad5380d3a54b1b69135bcea7bd69a18b87cd2d1305bea49236a21"}, @@ -112,7 +112,7 @@ "plug_cowboy": {:hex, :plug_cowboy, "2.7.1", "87677ffe3b765bc96a89be7960f81703223fe2e21efa42c125fcd0127dd9d6b2", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "02dbd5f9ab571b864ae39418db7811618506256f6d13b4a45037e5fe78dc5de3"}, "plug_crypto": {:hex, :plug_crypto, "2.1.0", "f44309c2b06d249c27c8d3f65cfe08158ade08418cf540fd4f72d4d6863abb7b", [:mix], [], "hexpm", "131216a4b030b8f8ce0f26038bc4421ae60e4bb95c5cf5395e1421437824c4fa"}, "poolboy": {:hex, :poolboy, "1.5.2", "392b007a1693a64540cead79830443abf5762f5d30cf50bc95cb2c1aaafa006b", [:rebar3], [], "hexpm", "dad79704ce5440f3d5a3681c8590b9dc25d1a561e8f5a9c995281012860901e3"}, - "postgrex": {:hex, :postgrex, "0.17.3", "c92cda8de2033a7585dae8c61b1d420a1a1322421df84da9a82a6764580c503d", [:mix], [{:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "946cf46935a4fdca7a81448be76ba3503cff082df42c6ec1ff16a4bdfbfb098d"}, + "postgrex": {:hex, :postgrex, "0.17.5", "0483d054938a8dc069b21bdd636bf56c487404c241ce6c319c1f43588246b281", [:mix], [{:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "50b8b11afbb2c4095a3ba675b4f055c416d0f3d7de6633a595fc131a828a67eb"}, "prom_ex": {:hex, :prom_ex, "1.8.0", "662615e1d2f2ab3e0dc13a51c92ad0ccfcab24336a90cb9b114ee1bce9ef88aa", [:mix], [{:absinthe, ">= 1.6.0", [hex: :absinthe, repo: "hexpm", optional: true]}, {:broadway, ">= 1.0.2", [hex: :broadway, repo: "hexpm", optional: true]}, {:ecto, ">= 3.5.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:finch, "~> 0.15", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}, {:oban, ">= 2.4.0", [hex: :oban, repo: "hexpm", optional: true]}, {:octo_fetch, "~> 0.3", [hex: :octo_fetch, repo: "hexpm", optional: false]}, {:phoenix, ">= 1.5.0", [hex: :phoenix, repo: "hexpm", optional: true]}, {:phoenix_live_view, ">= 0.14.0", [hex: :phoenix_live_view, repo: "hexpm", optional: true]}, {:plug, ">= 1.12.1", [hex: :plug, repo: "hexpm", optional: true]}, {:plug_cowboy, "~> 2.5", [hex: :plug_cowboy, repo: "hexpm", optional: false]}, {:telemetry, ">= 1.0.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}, {:telemetry_metrics_prometheus_core, "~> 1.0", [hex: :telemetry_metrics_prometheus_core, repo: "hexpm", optional: false]}, {:telemetry_poller, "~> 1.0", [hex: :telemetry_poller, repo: "hexpm", optional: false]}], "hexpm", "3eea763dfa941e25de50decbf17a6a94dbd2270e7b32f88279aa6e9bbb8e23e7"}, "rambo": {:hex, :rambo, "0.3.4", "8962ac3bd1a633ee9d0e8b44373c7913e3ce3d875b4151dcd060886092d2dce7", [:mix], [], "hexpm", "0cc54ed089fbbc84b65f4b8a774224ebfe60e5c80186fafc7910b3e379ad58f1"}, "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, diff --git a/test/lightning/projects_test.exs b/test/lightning/projects_test.exs index 100aee7a63..d1d23e0934 100644 --- a/test/lightning/projects_test.exs +++ b/test/lightning/projects_test.exs @@ -1,5 +1,6 @@ defmodule Lightning.ProjectsTest do require Phoenix.VerifiedRoutes + alias Swoosh.Email use Lightning.DataCase, async: true alias Lightning.Invocation.Dataclip @@ -576,34 +577,33 @@ defmodule Lightning.ProjectsTest do end test "schedule_project_deletion/1 schedules a project for deletion and notify all project users via email." do - user_1 = user_fixture(email: "user_1@openfn.org", first_name: "user_1") - user_2 = user_fixture(email: "user_2@openfn.org", first_name: "user_2") + user_1 = insert(:user, email: "user_1@openfn.org", first_name: "user_1") + user_2 = insert(:user, email: "user_2@openfn.org", first_name: "user_2") project = - project_fixture( + insert(:project, name: "project-to-delete", - project_users: [%{user_id: user_1.id}, %{user_id: user_2.id}] + project_users: [%{user: user_1}, %{user: user_2}] ) assert project.scheduled_deletion == nil Projects.schedule_project_deletion(project) - project = Projects.get_project!(project.id) - assert project.scheduled_deletion != nil - admin_email = Lightning.Config.instance_admin_email() - [user_1, user_2] - |> Enum.each(fn user -> - actual_deletion_date = - Lightning.Config.purge_deleted_after_days() - |> Lightning.Helpers.actual_deletion_date() - |> Lightning.Helpers.format_date() + actual_deletion_date = + Lightning.Config.purge_deleted_after_days() + |> Lightning.Helpers.actual_deletion_date() + |> Lightning.Helpers.format_date() - assert_email_sent( + for user <- [user_1, user_2] do + email = %Email{ subject: "Project scheduled for deletion", to: [{"", user.email}], + from: + {Lightning.Config.email_sender_name(), + Lightning.Config.instance_admin_email()}, text_body: """ Hi #{user.first_name}, @@ -615,8 +615,13 @@ defmodule Lightning.ProjectsTest do OpenFn """ - ) - end) + } + + assert_email_sent(email) + end + + project = Repo.reload!(project) + assert project.scheduled_deletion != nil end test "schedule_project_deletion/1 schedules a project for deletion to now when purge_deleted_after_days is nil" do