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

Finish implementation of "delete user" #747

Closed
4 of 5 tasks
amberrignell opened this issue Mar 29, 2023 · 0 comments · Fixed by #796
Closed
4 of 5 tasks

Finish implementation of "delete user" #747

amberrignell opened this issue Mar 29, 2023 · 0 comments · Fixed by #796
Assignees

Comments

@amberrignell
Copy link
Contributor

amberrignell commented Mar 29, 2023

@elias-ba has confirmed that in resolution of this issue he will handle every user relation in the app:

  • credential.user_id
  • invocation_reason.user_id
  • project_user.user_id

Initial bug report

Describe the bug
Every day, we check for users that have been scheduled for deletion and delete users. The oban job is running ok, but the command is failing because of database constraints which means that users that should be deleted aren't

Version number v0.4.6
I have reproduced this locally on main:

  • Yes
  • No

To Reproduce

  1. In runtime.exs, update line 39 to check users for deletion every minute {"* * * * *", Lightning.Accounts, args: %{"type" => "purge_deleted"}},
  2. Run Lightning
  3. Log in as a superuser
  4. Delete a user
  5. Edit the scheduled for deletion time to 1 minute from now
  6. Wait one minute
  7. See user is not deleted and there is an error in the terminal (stack trace posted below)

Expected behaviour
The user should be deleted and removed from any project they are part of.
When a user is scheduled for deletion, check if any of their resources (ex: credentials) are being used in existing projects.

Additional context

Stack trace below:

[error] Oban exception:
%RuntimeError{message: "you are attempting to change relation :project_users of\nLightning.Projects.Project but the `:on_replace` option of this relation\nis set to `:raise`.\n\nBy default it is not possible to replace or delete embeds and\nassociations during `cast`. Therefore Ecto requires the parameters\ngiven to `cast` to have IDs matching the data currently associated\nto Lightning.Projects.Project. Failing to do so results in this error message.\n\nIf you want to replace data or automatically delete any data\nnot sent to `cast`, please set the appropriate `:on_replace`\noption when defining the relation. The docs for `Ecto.Changeset`\ncovers the supported options in the \"Associations, embeds and on\nreplace\" section.\n\nHowever, if you don't want to allow data to be replaced or\ndeleted, only updated, make sure that:\n\n  * If you are attempting to update an existing entry, you\n    are including the entry primary key (ID) in the data.\n\n  * If you have a relationship with many children, all children\n    must be given on update.\n\n"}
    (ecto 3.9.4) lib/ecto/changeset/relation.ex:238: Ecto.Changeset.Relation.on_replace/2
    (ecto 3.9.4) lib/ecto/changeset/relation.ex:409: Ecto.Changeset.Relation.reduce_delete_changesets/5
    (ecto 3.9.4) lib/ecto/changeset/relation.ex:121: Ecto.Changeset.Relation.cast/5
    (ecto 3.9.4) lib/ecto/changeset.ex:832: Ecto.Changeset.cast_relation/4
    (lightning 0.4.6) lib/lightning/projects/project.ex:37: Lightning.Projects.Project.changeset/2
    (lightning 0.4.6) lib/lightning/projects.ex:112: Lightning.Projects.update_project/2
    (elixir 1.14.2) lib/enum.ex:975: Enum."-each/2-lists^foreach/1-0-"/2
    (lightning 0.4.6) lib/lightning/accounts.ex:34: Lightning.Accounts.purge_user/1
    (elixir 1.14.2) lib/enum.ex:975: Enum."-each/2-lists^foreach/1-0-"/2
    (lightning 0.4.6) lib/lightning/accounts.ex:76: Lightning.Accounts.perform/1
    (oban 2.14.2) lib/oban/queue/executor.ex:129: Oban.Queue.Executor.perform/1
    (oban 2.14.2) lib/oban/queue/executor.ex:74: Oban.Queue.Executor.call/1
    (elixir 1.14.2) lib/task/supervised.ex:89: Task.Supervised.invoke_mfa/2
    (elixir 1.14.2) lib/task/supervised.ex:34: Task.Supervised.reply/4
    (stdlib 4.2) proc_lib.erl:240: :proc_lib.init_p_do_apply/3

meta:
  %{
  args: %{"type" => "purge_deleted"},
  attempt: 1,
  conf: %Oban.Config{
    dispatch_cooldown: 100,
    engine: Oban.Engines.Basic,
    get_dynamic_repo: nil,
    log: false,
    name: Oban,
    node: "ambers-MacBook-Air",
    notifier: Oban.Notifiers.Postgres,
    peer: Oban.Peers.Postgres,
    plugins: [
      {Oban.Plugins.Cron,
       [
         crontab: [
           {"* * * * *", Lightning.Jobs.Scheduler},
           {"* * * * *", ObanPruner},
           {"* * * * *", Lightning.Accounts,
            [args: %{"type" => "purge_deleted"}]},
           {"0 10 * * *", Lightning.DigestEmailWorker,
            [args: %{"type" => "daily_project_digest"}]},
           {"0 10 * * MON", Lightning.DigestEmailWorker,
            [args: %{"type" => "weekly_project_digest"}]},
           {"0 10 1 * *", Lightning.DigestEmailWorker,
            [args: %{"type" => "monthly_project_digest"}]}
         ]
       ]}
    ],
    prefix: "public",
    queues: [
      scheduler: [limit: 1],
      workflow_failures: [limit: 1],
      background: [limit: 1],
      runs: [limit: 4]
    ],
    repo: Lightning.Repo,
    shutdown_grace_period: 60000,
    stage_interval: 1000,
    testing: :disabled
  },
  id: 8224,
  job: %Oban.Job{
    __meta__: #Ecto.Schema.Metadata<:loaded, "public", "oban_jobs">,
    id: 8224,
    state: "executing",
    queue: "background",
    worker: "Lightning.Accounts",
    args: %{"type" => "purge_deleted"},
    meta: %{},
    tags: [],
    errors: [],
    attempt: 1,
    attempted_by: ["ambers-MacBook-Air"],
    max_attempts: 1,
    priority: 0,
    attempted_at: ~U[2023-03-29 09:18:00.289256Z],
    cancelled_at: nil,
    completed_at: nil,
    discarded_at: nil,
    inserted_at: ~U[2023-03-29 09:18:00.026985Z],
    scheduled_at: ~U[2023-03-29 09:18:00.026985Z],
    conf: %Oban.Config{
      dispatch_cooldown: 100,
      engine: Oban.Engines.Basic,
      get_dynamic_repo: nil,
      log: false,
      name: Oban,
      node: "ambers-MacBook-Air",
      notifier: Oban.Notifiers.Postgres,
      peer: Oban.Peers.Postgres,
      plugins: [
        {Oban.Plugins.Cron,
         [
           crontab: [
             {"* * * * *", Lightning.Jobs.Scheduler},
             {"* * * * *", ObanPruner},
             {"* * * * *", Lightning.Accounts,
              [args: %{"type" => "purge_deleted"}]},
             {"0 10 * * *", Lightning.DigestEmailWorker,
              [args: %{"type" => "daily_project_digest"}]},
             {"0 10 * * MON", Lightning.DigestEmailWorker,
              [args: %{"type" => "weekly_project_digest"}]},
             {"0 10 1 * *", Lightning.DigestEmailWorker,
              [args: %{"type" => "monthly_project_digest"}]}
           ]
         ]}
      ],
      prefix: "public",
      queues: [
        scheduler: [limit: 1],
        workflow_failures: [limit: 1],
        background: [limit: 1],
        runs: [limit: 4]
      ],
      repo: Lightning.Repo,
      shutdown_grace_period: 60000,
      stage_interval: 1000,
      testing: :disabled
    },
    conflict?: false,
    replace: nil,
    unique: nil,
    unsaved_error: %{
      kind: :error,
      reason: %RuntimeError{
        message: "you are attempting to change relation :project_users of\nLightning.Projects.Project but the `:on_replace` option of this relation\nis set to `:raise`.\n\nBy default it is not possible to replace or delete embeds and\nassociations during `cast`. Therefore Ecto requires the parameters\ngiven to `cast` to have IDs matching the data currently associated\nto Lightning.Projects.Project. Failing to do so results in this error message.\n\nIf you want to replace data or automatically delete any data\nnot sent to `cast`, please set the appropriate `:on_replace`\noption when defining the relation. The docs for `Ecto.Changeset`\ncovers the supported options in the \"Associations, embeds and on\nreplace\" section.\n\nHowever, if you don't want to allow data to be replaced or\ndeleted, only updated, make sure that:\n\n  * If you are attempting to update an existing entry, you\n    are including the entry primary key (ID) in the data.\n\n  * If you have a relationship with many children, all children\n    must be given on update.\n\n"
      },
      stacktrace: [
        {Ecto.Changeset.Relation, :on_replace, 2,
         [
           file: 'lib/ecto/changeset/relation.ex',
           line: 238,
           error_info: %{module: Exception}
         ]},
        {Ecto.Changeset.Relation, :reduce_delete_changesets, 5,
         [file: 'lib/ecto/changeset/relation.ex', line: 409]},
        {Ecto.Changeset.Relation, :cast, 5,
         [file: 'lib/ecto/changeset/relation.ex', line: 121]},
        {Ecto.Changeset, :cast_relation, 4,
         [file: 'lib/ecto/changeset.ex', line: 832]},
        {Lightning.Projects.Project, :changeset, 2,
         [file: 'lib/lightning/projects/project.ex', line: 37]},
        {Lightning.Projects, :update_project, 2,
         [file: 'lib/lightning/projects.ex', line: 112]},
        {Enum, :"-each/2-lists^foreach/1-0-", 2,
         [file: 'lib/enum.ex', line: 975]},
        {Lightning.Accounts, :purge_user, 1,
         [file: 'lib/lightning/accounts.ex', line: 34]},
        {Enum, :"-each/2-lists^foreach/1-0-", 2,
         [file: 'lib/enum.ex', line: 975]},
        {Lightning.Accounts, :perform, 1,
         [file: 'lib/lightning/accounts.ex', line: 76]},
        {Oban.Queue.Executor, :perform, 1,
         [file: 'lib/oban/queue/executor.ex', line: 129]},
        {Oban.Queue.Executor, :call, 1,
         [file: 'lib/oban/queue/executor.ex', line: 74]},
        {Task.Supervised, :invoke_mfa, 2, [file: 'lib/task/supervised.ex', ...]},
        {Task.Supervised, :reply, 4, [...]},
        {:proc_lib, :init_p_do_apply, 3, ...}
      ]
    }
  },
  kind: :error,
  max_attempts: 1, (truncated)
@amberrignell amberrignell added the bug Newly identified bug label Mar 29, 2023
@amberrignell amberrignell added this to the External Beta milestone Mar 30, 2023
@amberrignell amberrignell changed the title Scheduled deletion not working because of db constraint Scheduled deletion for users not working because of db constraint Apr 14, 2023
@taylordowns2000 taylordowns2000 removed the bug Newly identified bug label Apr 19, 2023
@taylordowns2000 taylordowns2000 changed the title Scheduled deletion for users not working because of db constraint Finish implementation of "delete user" Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants