Skip to content

Conversation

@kinjalh
Copy link
Member

@kinjalh kinjalh commented Jan 16, 2026

gc cron job for dangling policy_bindings entries

Test Plan

unit test

Checklist

  • If required, I have updated the Plural documentation accordingly.
  • I have added tests to cover my changes.
  • I have added a meaningful title and summary to convey the impact of this PR to a user.

Plural Flow: console

@kinjalh kinjalh added the enhancement New feature or request label Jan 16, 2026
Copy link
Member

@michaeljguarino michaeljguarino left a comment

Choose a reason for hiding this comment

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

we definitely need to make sure there are indices on all these columns as well, the exists queries are going to be quite slow if not.


# Create a project with write_bindings - these should be kept
project = insert(:project, write_bindings: [%{user_id: user.id}])
%{write_bindings: [kept_binding]} = Console.Repo.preload(project, [:write_bindings])
Copy link
Member

Choose a reason for hiding this comment

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

i actually don't think this preload should be needed

Copy link
Member Author

Choose a reason for hiding this comment

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

i think it's needed if we want a kept_binding that we can run assertions against after the cronjob

|> PolicyBinding.ordered(asc: :id)
|> Repo.stream(method: :keyset)
|> Console.throttle(count: 100, pause: 1)
|> Task.async_stream(fn binding ->
Copy link
Member

Choose a reason for hiding this comment

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

we would want to chunk these in batches of 100 and delete each chunk in one query, to save db network round trips. Pretty sure there are some other examples of doing that here.

timestamps()
end

def dangling(query \\ __MODULE__) do
Copy link
Member

Choose a reason for hiding this comment

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

The slightly scary thing about this is if another table is added that sets bindings, it's pretty likely it'll be forgotten that a clause is needed here to validate. Ideally we have some static check that confirms this, i just don't know how to organize it.

@kinjalh kinjalh force-pushed the policy-bindings-gc-cron branch from 641a8d3 to ed1ea75 Compare January 22, 2026 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants