-
Couldn't load subscription status.
- Fork 14
Description
It is nice to have an error when the postgres trigger name is too long, but it would be nicer IMHO to not have to worry about that as a developer. A possible solution would be to hash the module name to a shorter string with a predictable hashing function and store a mapping in ets or persistent_term to the actual schema. By using a predictable hashing function the triggers remain valid after restarting the application.
The hashed label be stored when the watcher is registered, and any subsequent subscribe and unsubscribe will use the internal hashed value. Outwardly, the caller can still reference any subscriptions by using the schema name or label they provided.
When the watchers are setup, we implicitly add the label if it was not provided and use the following hashing function for example:
:crypto.hash(:sha256, to_string(schema))
|> Base.encode32(case: :lower, padding: false)
|> binary_part(0, 8)
We then store a reference and inverted reference in :persistent_term:
:persistent_term.put({EctoWatch, watcher_identifier}, encoded)
:persistent_term.put({EctoWatch, encoded}, watcher_identifier)
The inverted reference is used to convert PubSub labels back to the schema module names.