9 hours 41 minutes
Hi. Welcome back to Model Three. This is less than 3.6 database migrations. And this lesson, we're gonna take a look at actually implementing and using some of our database of migration commands. So what are these commands were talking about what we're talking about. The PHP are Titian commands.
We can create database declaration files
that themselves created tables,
indexes and just about any database component that you need on command.
We can rewind when appropriate. What does that mean? That means if we have five database files that are creating different database objects, but we only want to run up to the third file, we can do that. Or we can go from file five to file three so we can move forward and backwards in our design,
which makes testing
ah, lot easier when dealing with some complicated scenarios.
What are some of the strengths of having file definitions Will now files can be shared with other programmers. So you could give another programmer all your migration files and say here, run this and create the database that I'm working on. And on top of that, if you're using version control, you can say
any changes that you make to my files. Please push the version control,
and then I'll pull those changes. And then we can work with each other without undoing or redoing each other's work.
Some of the migration strengths, flaws in design, arm or easily exposed. So if you create a bunch of files that are supposed to run one after the other and you're creating constraints along the way, and there's a flaw in your design, the migrations going to crash
and you'll easily see more easily anyway, see where the problem is
and migrations also, or PHP artistic commands. Also, Ladas seed the database using a library called Faker, which works in conjunction with the O. R M. Part of level, which allows us to insert test data so we can easily create hundreds or even thousands of records and populate our database,
which can really help us identify any choke points in our design
where we will get slow performance for some reason now, with that said, let's go over to the virtual machine and take a look at running some of these commands and in Stan sheeting, some database objects.
Hi, welcome back to my virtual machine and let's get started with some database migrations. Now, if you want to find the migration documents on this, in case you have a question on some commands because we're not gonna go over every command. But finding the documentation is pretty easy
and it can change my version. So I always use the newest version. But if you, for example, search level migrations,
the first hit you'll get is probably the correct one which will be database migrations. And this is all the documentation on the various commands that we can use to create various objects. And one of the first ones we're gonna do is this. Create user tables or we're not gonna create users. We're gonna create a table called cars as an example,
So make sure that your virtual box is running and that you can connect to the database because we are running some database commands.
So to get into your virtual box, you need to have a bash shell open to the directory that is housing your vagrant files. So and then once you're there and you have that basho open, you're gonna do Bagram ssh!
And that will connect to your machine through a ssh shell.
Okay, now, once we're there, we need to make sure that we're in the correct directory that we sink with all the layer ville files and that's layer Veldt folder.
So must change directory or CD into that. Fooled. I'm gonna Let's make sure I'm in the right one,
and then I'm gonna write this command
that's over here, but I'm gonna change a little bit
once a PHP
Hardison make and gotta be sure to spell it correctly because if you spell it wrong and it's gonna crash,
but it won't be the end of the world, so you'll be able to just type it again. Hopefully,
The best *** creates
so looks like created the migration. Let's check our sink directory.
So be sure to go into the director you sink, which sinks between your virtual machine and the
the the host machine. So that's the shared folder.
So I'm gonna go into that.
We're going to my database folder. We're going to my Migrations folder
and here's the cars table file I just created with that PHP are Titian command. I'm gonna edit with no pad plus plus
drop of exists. Okay, that looks correct. I'm gonna go down to an example of how they create the columns in the table.
So there we go
and put the big put the big increments in there for me and the time stamps, The time steps is going to add columns created at and updated at which our time stamps,
which are is useful because, you know, it's pretty common to want to know when certain things were created or updated.
So this is a car's table, someone say,
dollar sign table. And of course, you got to get the syntax correct. Otherwise, it won't work, but it's not too bad. So string, which means which is ah, pretty much means of Arch are in sequel.
So string rests a model
once a string again, and we're gonna make that I make
And you know, we're gonna add one more column,
Chris, A table.
are the choices for our data types?
Here we are. Here is the list of all our data types,
so big increments is the same as big imager,
but I believe it's unsigned. Big increments is an unsigned big manager. So if we want to link to that
through constraints, we gotta make sure that our linking
column is the same data type. Otherwise it won't work. So it's gonna be
big imager unsigned. So, table
I would say
and Arrow and add the keyword unsigned.
I'm going to spell it right.
And so, Michael, and don't forget the semicolons like I almost did That will cause a problem.
Let's go ahead and save that and migrates and see if that works for us. So
PHP artisan migrates,
Okay. Says it was able to create the car stable. If you've got an air, you would want to check that you're getting the syntax right, because these words have to be spelled correctly and you have to get the semicolons on the end there. Thes files will be available as a resource for download as well. In case you just want to copy it,
uh, straight into your own file.
So let's go look in our database and see what happened.
So here's the level database were connected to. I'm gonna refresh that.
We can see that we now have a new
record on the migrations table that it's part of wearable. So this is level keeping track of our migrations that we are running, and this is part of that magic that allows us to roll back and Ford in our migrations.
So if he were to delete this table, you would lose a little bit of functionality related to this tables use by Larible.
So here's the cars table we created,
but the columns who created these air the time stamp columns,
you look at the properties,
we can see them
became big and unsigned.
Big imager unsigned,
which allows these to be linked to each other
and two other big manager unsigned on other tables. If I had done imager unsigned and then on the other table, I had another in committing
I. D column of Big, and I wouldn't be able to form constraints because they would be considered different data types, and the string command
converts it to a bar chart to 55.
Okay, so that's how that works.
So let's create one more table that we can relate to the cars table, and we're gonna call that a owner.
Great owners table
owners on and run.
Okay? It created the owners table. So let's go take a look at that.
And let's edit that. And no pad plus plus as well.
Again, it automatically went ahead and added the time steps, the time stamps and the big increments, which is fine. And I'm gonna do table
How would you table?
See that big imager
Yep, it was big imager.
And we'll go ahead and put in a car, i d
and we're gonna make it unsigned.
And we're also going to add a foreign key constraint here.
So in the documentation I search for in and then brought up the floor and key constraints and there we can see the command for that. So
we're going to put
car I D
which is the table we made before.
So table foreign
references. I d on cars.
So that should work.
I get that spelling right and got can't forget those semicolons.
We go back over here, I'm gonna hit up twice and run. My PHP are Titian Migrate Command
and it created its Let's go in our database and take a look.
I hit refresh right click Refresh.
There's our owners
now. If I look at the e r E r diagram, we can see that we created that constraint
and that is that. So we created two files.
The cars and the owners.
Larible is tracking our migrations in its own table. That gives us some capability,
and that is how that is. The basics of the PHP are Titian commands. You will create a file. You'll define your table. In that file, you'll run the migration. That migration will create your tables. Well, that brings us to the database migrations summary. So what do we do in this lesson? What we discussed
how to use the PHP are Titian commands to create
database migrations. So how to create those migration files we ransom migration commands to demonstrate how it works? And we also identified where you can go to look up some documentation if you have some questions on some of the commands, what they do or if you have a question on what commands even exist.
So that completes this lesson, and I hope to see in the next thank you