In this article, I’ll describe the process of switching a project’s test suite from running against an in-memory database (SQLite) to a disk-based database instead (MySQL) and give some tips on how it can actually speed things up. A big chunk of these tests are Feature Tests. Sticking with our example code from earlier, let's take a look at how we could use a transaction when creating a user and assigning them a role.We have a really good testing culture at madewithlove and, in one of the projects I am working on, we have a decent suite of tests. In Laravel, it's actually really to easy to get started with using transactions thanks to the transaction() method that we can access on the DB facade. Now that we have a brief idea on what transactions are and what they achieve, let's take a look at how to use them in Laravel. By doing this, it means that we wouldn't be able to create a user without an assigned role. For example, if the user was inserted into the database but the query to assign the role failed for any reason, the transaction would be rolled back and the user's row would be removed. By using transaction, it would ensure that if anything goes wrong when executing the code, any changes to the database from inside that transaction would be rolled back. So, to solve this issue, we can use database transactions. As you can imagine, this will likely cause exceptions and bugs in other places across your application because you would always be making an assumption that a user has a role (and rightly so). This could be caused by many different things, such as a bug in the code that assigns the roles, or even a hardware problem that stops us from reaching the database.Īs a result of this happening, this would mean that we would have a user in the system that doesn't have a role. It's possible that we could create the user but not assign them the role. But, when we take a closer look we can see that there's actually something that could go wrong. $user->roles()->attach(Role::where('name', 'general')->first()) Īt first glance, it might seem like this code is completely fine. Our code might look something like this: $user = User::create([ Whenever a user register, we want to create a new account for them and then assign them a default role of 'general'. Let's imagine that we have an application that lets users register. To understand what this actually means, let's take a look at a basic example that will give a little bit of context. But, for a large majority of us, as web developers, we just need to know that transactions are way of completing a unit of work as a whole in a database. There's a lot of technical, complicated-sounding explanations for what a database transaction is. What are Database Transactions?īefore we get started with looking at database transactions in Laravel, let's take a look at what they are and how they can are beneficial. We're also going to look at a common "gotcha" involving queued jobs and database transactions. In this article, we're going to look at what database transactions are, why they're important, and how to get started with using them in Laravel. So, making sure that we write code that stores, updates and deletes data in our databases in a safe way is imperative. In web development, data integrity and accuracy is really important.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |