saverio-kantox
I need to set up a “audit” table in the database, where changes to records trigger an insert on some other table. Writing the trigger is not an issue.
Ideally, I would try to store also some sort of “current user” on the audit, which is known for instance in a specific plug. Here is the issue: I see no way to tell Ecto that all connections that are checked out from the pool should have an “initial” command sent like set session 'myapp.current_user' = 'abcdef12-1234-1234-12345678abcdcdef';.
The problem is threefold:
- How to hook into connection checkout
- How to ensure that the connection is not held for too much time
- How to pass it down for instance to a liveview (maybe it’s just a restatement of 1+2, because the liveview is long-lived, and the connection should not be held)
Does anyone have any suggestion on how to approach the problem?
Than you beforehand!!
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
wolfiton
Have your worked with context and plugs?
This How i would approach this problem first create an Accounts context then use the Plug session:
and you can use this code to access the session:
No you can create a function to insert it into your table.
Also if you are new to phoenix the documentation may be a bit scary at first. So don’t be afraid to ask questions or clarifications on my response.
I will gladly try to help you, even though I am new to this framework too.
saverio-kantox
Well, the issue was not to put data in plug’s session but to put data in the database’s one.
I have been suggested that it cannot be done automatically, but I have to implement some function taking a block, which ensures that the initial statement (setting a session variable), the block and the eventual cleanup is executed in the same connection.
There was a talk at ElixirConf about preemptively configuring queréis based on this, but I cannot seem to find a video for it.
wolfiton
If you need videos my search was successful and found a lot but that specific one eludes me as well.
In any case if you want eleixir conf videos this is what I found https://www.youtube.com/results?search_query=elixir+conf
Maybe this might help:
Also I am wondering if not the ecto multi, can be used here. https://hexdocs.pm/ecto/Ecto.Multi.html.(can make several repo actions like multiple inserts)
The changeset also can prepare queries and add to them.
Ecto.Changeset — Ecto v3.14.0(modify structures and validate them)
halostatue
The hex.pm source has a really good audit logging implementation that is worth looking at. I’ve been running a (heavily modified) variant of it in production for the last couple of years and it hasn’t failed me yet. It has a Task-based audit function that can be used for writing to the audit log on reads. I’m sure it wouldn’t be hard to write your error handling to use the audit function if, say, a multi operation failed.
wolfiton
This might be related to that conference talk you mentioned earlier
BusinessAuditLog
This is a simple microservice developed to complement my talk about Phoenix Framework. The main purpose of this microservice is storing audit log messages about business relevant events happening in an imaginary distributed system.
https://github.com/env0der/phoenix-audit-log-microservice
saverio-kantox
Thank everyone for te suggestions.
First point is that I really don’t want elixir to have the responsibility of writing the audits. If a record in a table changes because of action of an external system (or direct interaction through psql) I still want to audit the change.
I’m already doing the same for ids and timestamps. They are managed by the database in triggers.
So the only thing elixir has to do is to tell the database who is the “current responsible” of the changes.
I think that making an Ecto.Multi call hidden behind something like
MyRepo.with_current_user(id_or_name) do ... endis the most likely solution. I just hope that all the team remember to always wrap any write in said block.Or even tell the database to prevent any change unless the “current responsible” variable is set!!!
wolfiton
You can create custom validations for changesets https://medium.com/@QuantLayer/more-custom-validations-for-ecto-changesets-17f3641be2a0.
Then the insert happens only if certain conditions are met.
danschultzer
I’m not sure if this is a good way of doing it, but you could create a custom repo module, and require the user id to be passed along:
So in the context methods you’ll have to pass along the user id:
idi527
Or add
audited_insertto the default repo:saverio-kantox
Thank you, this is the most interesting approach.