There was already an index on project_id and name but the index was not unique. The solution in preventing the creation of non-unique records would, therefore, be setting up a unique database index. The increase in the number and the complexity of hooks will also lead to an increased probability of the creation of non-unique records. As you can see hooks are also triggered between the validation and INSERT statement. In this time gap, another concurrent process can also create a record with the same name and the same project_id without any failing validation. Through this example, you can see that there is no lock on the table between the SELECT and the INSERT statement. # some before_save and before_create hooks # Tag Exists (0.3ms) SELECT 1 AS one FROM `tags` WHERE (`tags`.`name` = BINARY 'test' AND `tags`.`project_id` = 1) LIMIT 1 Tag Exists (0.2ms) SELECT 1 AS one FROM `tags` WHERE (`tags`.`name` = BINARY 'test' AND `tags`.`project_id` = 1) LIMIT 1Įxample success: pry(main)> Tag.create(name: "test", project_id: 1) If a record was found, the validation fails and the transaction will be rolled back, if not the record will be saved.Įxample Validation fail: pry(main)> Tag.create(name: "test", project_id: 1) If uniqueness validation is enabled, Rails will look for existing records before performing Tag.create, Tag.save, Tag.update. This combination dramatically increases the probability of two processes trying to create a tag at the same time. On any upload where the user can allocate a tag, the keys should also be tagged. By using our client’s push command, an upload for each locale is triggered. Uploads are processed asynchronously by concurrent workers. The last state was that the Tag should have a name field and belong to a project as well as have an ActiveRecord validation so that a tag was unique to the project_idand name State of Codebase class Tag 1) AS gįurthermore, we saw that all of the duplicated records were system generated tags made by our upload feature. So how could that happen? We had used ActiveRecord uniqueness validation and only used Rails without skipping the validation. So we investigated the issue and discovered that the error was caused by a tag name which was not unique to the project. The tagging feature had been used rather extensively by this customer to automatically tag all uploaded keys with a GitHub Pull Request, leading to a validation error with an existing tag. Although the feature was constantly improved over time and the performance was very reliable, one day a customer contacted us due to a validation error. The tagging of translation keys was added as a product feature in the very early stages of the Phrase development. With the help of a real world example, we show why relying solely on Rails validation could become a problem and demonstrate a way to cleanup your database without downtime. ActiveRecord Validation in Rails provides an easy way to validate records for uniqueness.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |