Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
9 hours 41 minutes
I welcome back to module five. This is less than 5.8. Event events are commonly used for maintenance tasks such as cleaning logs, which would include controlling the log file size, preparing report tables or cleaning data tables.
Now there are some special notes to consider when dealing with the event. Scheduler In my sequel,
My Sequel Events, Cultural Scheduler is off by default, so we must run the command set Global event Scheduler equal on to enable it.
This is reset if my sequel or the server is restarted or reset. So if the computer is turned off and on the event schedule, er will be off. If the my Civil Services restarted, the event sculpture Scheduler will again be off to enable by to fall. You must edit the my sequel dot
CNF file and add the line event Scheduler Equal on. And this listen we're going to implement in the event that calls a procedure that accepts the parameter for the size of logs that we want to maintain. So with that said, Let's get started.
I welcome back to the virtual machine Now there is before we dive into this, there is an air that exists in our previous files for that trigger that we created. It's very subtle, but I was I did notice it and let's go ahead and fix it, and I will point out what the air is.
So I'm gonna open up the files, the migration files, and it's in the triggers user after the league gonna edit that
and you can see that we declare a variable called I D.
But we cannot do that because my sequel is mixing up our variable i d. With the I. D. That exists on the table. Now this is causing it to try and insert a zero every time it runs this method. So we're gonna change the name of this variable. And I just picked the word target,
so that's something to be aware of.
My circle can have a name collision that could go undetected if you accidentally select a variable name that shares the same name with a column on a table that you happen to be working with. The easiest way to avoid this is just to make sure that you're naming is distinct. Your variables are not the same as any of the columns on the table.
So let me go ahead and finish implementing this target target. So I d. This idea references the actual i d on the table so we can't change that.
And then right here, where we insert that value of I d. That's a reference to our variable. So we need to go ahead and change that to target
and save that. And let's go ahead and run the migration with a refresh and make sure that works and that will empty out all our data as well. So we'll have to see it again if we want to test our data.
And there we go, so that should fix that problem.
Let me go ahead and see the database again.
And let's move on to talking about our procedures. Will that runs.
So the way I wanted to implement this, I almost all put it all in just the event. But then I decided to go ahead and write a procedure for it, and then use that the event to call the procedure.
So the procedure is called control Underscore logs, and it accepts an imager that is a count of how many laws you want to keep
so and the procedure.
Okay, so this is just the syntax, you know, Drop procedure. If exists, create procedure. Declare the parameter that's coming in big. Unsigned.
Begin declare our variables, which is CNT begin assigned and declare target, which is a date time. So I set CNT equal to a select of count from blog's.
If count is greater than allowed, which is the parameter that comes in. Then I run the code in this if statement. Otherwise, we won't run this code in the f statement, and the procedure would just exit without doing anything.
So in the event that count is greater than allowed, we set allowed equal to allow minus one.
And then we set the target equal to select, created at from logs ordered by school. I d i d
So what we're trying to do is select the record from a table and the way we do that in my sequel as we select from Table
we ordered by the I D, which is the school I d. Comma I d. In this case. And then we limit the allowed comma one, and that's saying, select this number, but limit one record from that selection, and we set the variable target to the created at date of that record.
And then we delete from users
where created at is less than that date, so that effectively deletes the older logs and keeps the newer lochs in the table
based on the record we bring back, which is controlled by the number that we pass in,
which has a variable name of allowed.
So let's go ahead and create the migration files that will instance this into our database.
make migration. You need to make the procedure first because we can't have the event called the procedure
if the procedure doesn't exist yet. So
that would go into my are folder, our migration folder.
Go ahead and open up that new file.
Take a peek back in the old files. I want to pull out the DB unprepared again,
paste it into the new file,
delete what we don't need,
and I'm gonna go ahead and copy it while I have this here, paste it into the function down
and let's get the procedure
so we don't need the delimit er swapping.
Go back into our
Paste it in there,
gonna save it,
and then we just need the drop procedure. If exists, Command,
bring it here
on Save that and let's run a migration refresh. So I probably shouldn't have ceded the database. We were gonna lose it right here.
Make sure that procedure gets in there
When we have an air. Let's see what happened.
Okay, this might have happened because I had the procedure in the database before I ran this. So let's go ahead and delete it.
Uh, not that the leads triggers procedures were fresh,
Then someone to delete the event
on. I'm going to delete the procedure.
You may not need to do this if you're following along, because you won't have those in your database.
So let's go ahead and run it.
OK, It worked, But let's make sure the down part of that is running,
so we'll run it again,
and it's working.
So, two instance, that procedure it would have had to drop it again first. So if you go ahead and refresh this
there, it iss Okay, Next, let's make the migration for our our event
make migration control log
go ahead and open that file up.
See the way. And I won't have that in there.
Go ahead and copy this one.
I'm just gonna pace it in both areas.
The leading what we don't need.
Let's go get that event code.
I don't need the limiters switching.
So of course, we have to make the event after the procedure because we're calling that procedure from the event
I have a current size of three, which is fine,
because we have do some additional testing to test the rial size, which might be something as high as a 1,000,000 records.
and then we need the drop event. It exists.
That's going to run our migration again. And make sure this is working
I expect that air because it does exist.
All right, refresh.
I like to run the refresh twice just to make sure that it's dropping. And it's rien stance eating all my objects correctly because if you for example, if you forgot Teoh put the drops statement in there, it would work one time. It would just feel the next time.
Okay, it looks like it's working. So if we go and look at our events
should be there
that completes this part of this lesson. Let's go ahead and head over to the summary.
So that brings us to the 58 events summary. And this lesson we implemented an event and a procedure in our migration fouls. So we use those two objects to add a log rotation event so that we could control the size of our log table.
We implemented a procedure the event calls the procedure with a size parameter.
We also fix the hidden air in the trigger from the previous lesson. So that completes this lesson and I hope to see you in the next. Thank you.