Background
I am working with multiple postgres schemas that basically identify as individual tenants sharing the app. Every request that the backend receives contains an instance identifying header which allows us to set the schema for each request.
For example, my database looks like this:
- postgres database
- common
- tenantOne
- tenantTwo
Problem
However, I have run into a problem when working with Symfony Messanger (DSN transport - doctrine://default). An example scenario is as follows:
Request comes in and sets the instance. This essentially runs a SQL command on the current Doctrine Connection:
'SET SEARCH_PATH TO ' . $schema_name . ', global'
Process creates a message. The message bus dispatches this message as normal.
The message gets written to the
messenger_messages
table on the current instanceWhen the message is consumed by the worker, it is not being set to a schema, so cannot handle a message.
Desired Behaviour
- Write ALL messages to the
common
schema - Worker consumes the message on the correct schema.
For #1 I am thinking that the schema can be set before the message is written to the database. But unsure where is best. My concern also is that if we change the schema with the current doctrine connection, it will need to be reset back so that the rest of the process can continue on the correct schema.
Is there a way to define which schema all messages should be written to without switching it and reverting it in middleware or event listeners?
For #2, I was thinking a stamp to pass the current instance with the message. This means my handler can use this to set the schema when it is received and handled?
It would be great to get some thoughts on how this can be handled neatly. A multi-tenancy application is common, but I cannot find anything anywhere in the docs where I can control the schema within a database in which a message is written (or the messenger_messages
table can be stored).