I Welcome back to module five. This is listen 5 10 transactional procedure to in this lesson, we need to fix the air handling In the procedure that we discussed in the previous lesson, there is a problem in the loop. If there are some successes and then an error, those successes will insert into the database
and that will be explained as we fix it. Why that's occurring,
then we also need to add that procedure to the migration fouls.
We also need to fix a unique constraint on the student courses table to respect our composite school i d key.
And then, of course, we will also test the procedure to make sure that it's working the way that we expect it
now. With that said, let's go ahead and head over to the development environment.
I welcome back to the development environment. There is a small change we need to make to the procedure that we had in the previous lesson. There's an issue. If an air occurs in the transaction, the transactions that are successful will automatically commit, and there's a reason for that. So let's take a look at how we will handle that
so at the top, we declare sequel Air Tiny in default. False.
We declare a continue handler for sequel exception, and if that happens, we set sequel air equal to True.
Now you'll notice that this is one command because there's no semicolon that occurs at the end of this line, and it ends at this line where we set sequel air equal to true.
So we go down to the bottom if we get a string of courses and the 1st 2 are successful and the 3rd 1 has an air without that continue handler, what would happen is the procedure would exit at that point and automatically commit the successful inserts that had occurred.
And that's not what we want. We want all or nothing there, either all successful
or they all are. They all failed, and that has to be rerun. So what will happen now is we'll start transaction.
We'll go through the loop if in an air occurs because we have the continue handler at the top, will change this variable to true
and then after the loop will check the value of that variable. If it's false, that means we were successful. We will run the commit command, and we will print out a success message. Otherwise, if it's true, that means an error occurred. We will roll back the transactions or the inserts that have occurred and we'll print out. An error has occurred.
So let's go ahead and run that. Make sure that works, and it does.
So we need to add this into a migration file now.
So let's bring up our terminal session. I'm in an ssh session with the Bagram machine. I'm getting ready to run this command, which is PHP artisan. Make migration. Add User, I got to underscores there, So let me get rid of one
add user to course procedure.
So that's a good name for the file.
I'll go ahead and run that that's gonna make the file. So I'm gonna go ahead and get that open.
close, these other ones I have open
so that we can keep it simple.
let's go ahead and open that again.
I know that I need to perform in action similar to something I've done in the past. I'm gonna open up one of my previous files.
He added that one so I know. I need the DB un prepared statement,
and I don't need that skim a table statement. So let me get rid of this.
So, Michael, And don't forget the semicolon
and with same thing here and now let's go ahead and move our procedure into these.
So I don't need the limiter swapping, so I'm gonna end at the end
one a copy all the way to the create statement.
I'm gonna switch these single quotes to double quotes because I have single quotes in the procedure
and this will make it easier. I won't have to use any kind of escape characters.
And then for the UN prepared,
we need to drop the procedure if it exists,
and I will go ahead and save that.
Now, let's test this and make sure that the migration runs.
So we go back to my session and I'm gonna do pH PR person.
So that will dump any data out of the database and refresh all the tables, the functions in the procedures. Looks like I got an air. Let's let's see what happened.
Okay, who's a user to course
already exists. So that's because I was doing some testings already having in the database.
So it wasn't running the down command for it because it believes that it's the first time I've added it.
I'll go ahead and delete that.
Then I'll go back into my terminal session
and I'll go ahead and run a refresh command again.
I'm gonna also go ahead and seed the database after this completes because the database will be empty.
and that will put a bunch of fake data into the database.
It's like it hasn't gotten to that point yet.
Dollars should be data there,
but we don't want data in this table cause this is gonna be our test
someone a trunk, it it
they don't want to deal with the possibility that the debt I happen to be trying to add already exists, and we go ahead and run that test. But I'm just gonna go ahead and empty all this data out first.
truncate user courses
go ahead and refresh.
And now let's go ahead and give our procedure a test. I'm gonna right click and refresh.
gonna generate the sequel call.
I'm going to copy this portion.
on I'm going to paste.
So I know I'm gonna have a school idea of one.
No, I'm gonna have a user idea of one. I know. I'm gonna have a type idea of one
and let's add some courses.
go ahead and call that
printed out success. That's take a look.
let's put him at a different schools. Say school too.
says an air occurred.
Make sure nothing was added.
Okay, this is actually based on the other problem we need to fix were not respecting the fact that we have the school ideas part of our composite key and the user courses table. If we take a look at this,
we'll see that our constraints We have a unique constraint,
user. I d either type I d. Course I d. So we need to add the school. I d into this unique constraint. So we need to go into our migration files and make that change and then refresh and recede.
We go ahead and add it. This and we need to This is user courses, right? Create user courses
This is an array. It's in the brackets.
Add that string in there.
And let's go ahead and refresh.
And as soon as this finishes, we need to receive because all that data is gone.
That takes a moment for it to do that. So let's head back here. We're gonna need to truncate the user courses again and run this again.
And this being the add user to course procedure.
So let's check to see if we have data here.
So we'll go ahead and truncate that table again.
gonna run this procedure with the two again. Which would be school I d to
We have success gonna switch that to a one.
We have success again.
You go look at the user courses. Refresh. So 121212 for six entries.
same user I d at different schools, which is exactly what we wanted.
So let me go ahead and run this again. I should get in air.
Yep, and indicates an air. There should be no entries. That's correct.
Now let's Let's ah, make an entry that will be correct for the 1st 2 iterations of that loop and then fill on that last one. So 45 would be valid and would be successful. But three should fail.
Okay, it says that an air occurred. Let's double check that table and make sure that nothing was added.
So our procedure appears to be working. If in error occurs in that loop, we don't add anything. If everything successful, we go ahead and commit all the changes, and that completes this portion of the lesson. Let's go ahead and head over to the summary,
and that brings us to the 5 10 summary and this lesson. We added a procedure to the migrations. We fix that unique constraint on one of our tables, and we also fix the air handling in our procedure with a continue sequel exception handler. Then we, of course, tested the procedure to make sure it was working the way we expected.
And that completes this lesson.
I hope to see you in the next Thank you