DB Trigger - Sanitize App Content
Trigger: trigger_sanitize_app_content
Code:
CREATE TRIGGER trigger_sanitize_app_content
BEFORE INSERT OR UPDATE OF content
ON apps
FOR EACH ROW
EXECUTE FUNCTION sanitize_content();
Environment:
- ✅ Production
- ✅ Staging
- ❌ Development
Description:
This trigger is designed to sanitize the content field of the apps table before inserting or updating records. It uses the sanitize_content() function to replace any occurrences of the null character (represented as '\u0000') with an empty string while also removing any backslashes that precede the '\u0000' character. This ensures that not only null characters but also any escape sequences like '\u0000' are removed from the content field. These escaped characters appear due to the content field being serialized from a string to json and back to string by the ruby interpretor
Purpose:
- Prevents null characters from being stored in the
contentfield. The null unicode characters impact the serialization to JSON, resulting in corresponding bugsnags.
Usage:
This trigger is automatically activated before any INSERT or UPDATE operation on the content field of the apps table.
Removal: If this function needs to be removed for any reason, execute the following in the necessary environment. This will also remove the corresponding trigger:
DROP TRIGGER IF EXISTS trigger_sanitize_app_content ON apps;
Related Documentation: