Time
9 hours 41 minutes
Difficulty
Intermediate
CEU/CPE
10

Video Transcription

00:00
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.
00:17
We can create database declaration files
00:20
that themselves created tables,
00:23
indexes and just about any database component that you need on command.
00:28
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,
00:47
which makes testing
00:48
ah, lot easier when dealing with some complicated scenarios.
00:53
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
01:08
any changes that you make to my files. Please push the version control,
01:12
and then I'll pull those changes. And then we can work with each other without undoing or redoing each other's work.
01:21
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
01:37
and you'll easily see more easily anyway, see where the problem is
01:42
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,
02:01
which can really help us identify any choke points in our design
02:07
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.
02:19
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
02:38
and it can change my version. So I always use the newest version. But if you, for example, search level migrations,
02:46
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,
03:06
So make sure that your virtual box is running and that you can connect to the database because we are running some database commands.
03:15
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!
03:30
And that will connect to your machine through a ssh shell.
03:36
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.
03:46
So must change directory or CD into that. Fooled. I'm gonna Let's make sure I'm in the right one,
03:52
and then I'm gonna write this command
03:54
that's over here, but I'm gonna change a little bit
03:58
once a PHP
04:00
Hardison make and gotta be sure to spell it correctly because if you spell it wrong and it's gonna crash,
04:08
but it won't be the end of the world, so you'll be able to just type it again. Hopefully,
04:12
so create
04:14
cars
04:15
table.
04:17
The best *** creates
04:19
equal cars.
04:23
Run that
04:27
so looks like created the migration. Let's check our sink directory.
04:31
So be sure to go into the director you sink, which sinks between your virtual machine and the
04:39
the the host machine. So that's the shared folder.
04:44
So I'm gonna go into that.
04:46
We're going to my database folder. We're going to my Migrations folder
04:50
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
05:00
drop of exists. Okay, that looks correct. I'm gonna go down to an example of how they create the columns in the table.
05:08
So there we go
05:10
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,
05:23
which are is useful because, you know, it's pretty common to want to know when certain things were created or updated.
05:29
So this is a car's table, someone say,
05:32
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.
05:45
So string rests a model
05:49
and
05:51
table
05:54
once a string again, and we're gonna make that I make
06:00
And you know, we're gonna add one more column,
06:04
Chris, A table.
06:10
I see.
06:13
What
06:14
are the choices for our data types?
06:19
Here we are. Here is the list of all our data types,
06:24
so big increments is the same as big imager,
06:31
but I believe it's unsigned. Big increments is an unsigned big manager. So if we want to link to that
06:38
through constraints, we gotta make sure that our linking
06:41
column is the same data type. Otherwise it won't work. So it's gonna be
06:46
big imager unsigned. So, table
06:49
big
06:50
imager.
06:54
I would say
06:56
owner
06:58
I D
07:00
and Arrow and add the keyword unsigned.
07:04
I'm going to spell it right.
07:10
And so, Michael, and don't forget the semicolons like I almost did That will cause a problem.
07:15
Let's go ahead and save that and migrates and see if that works for us. So
07:23
PHP artisan migrates,
07:30
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,
07:49
uh, straight into your own file.
07:53
So let's go look in our database and see what happened.
07:57
So here's the level database were connected to. I'm gonna refresh that.
08:01
We can see that we now have a new
08:03
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.
08:16
So if he were to delete this table, you would lose a little bit of functionality related to this tables use by Larible.
08:24
So here's the cars table we created,
08:28
but the columns who created these air the time stamp columns,
08:31
you look at the properties,
08:35
we can see them
08:35
big increments
08:39
became big and unsigned.
08:43
Big imager unsigned,
08:46
which allows these to be linked to each other
08:48
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
08:58
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
09:05
converts it to a bar chart to 55.
09:09
Okay, so that's how that works.
09:15
So let's create one more table that we can relate to the cars table, and we're gonna call that a owner.
09:22
Great owners table
09:26
great
09:30
owners on and run.
09:37
Okay? It created the owners table. So let's go take a look at that.
09:41
And let's edit that. And no pad plus plus as well.
09:46
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
10:00
string
10:03
name.
10:05
How would you table?
10:11
See that big imager
10:18
is that?
10:20
Yep, it was big imager.
10:24
And we'll go ahead and put in a car, i d
10:31
and we're gonna make it unsigned.
10:37
And we're also going to add a foreign key constraint here.
10:41
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
10:48
we're going to put
10:50
table
10:54
foreign
11:00
car I D
11:07
Preferences
11:13
I d
11:18
on
11:20
cars,
11:22
which is the table we made before.
11:26
So table foreign
11:30
cardi
11:31
references. I d on cars.
11:33
So that should work.
11:37
I get that spelling right and got can't forget those semicolons.
11:41
We go back over here, I'm gonna hit up twice and run. My PHP are Titian Migrate Command
11:52
and it created its Let's go in our database and take a look.
11:58
I hit refresh right click Refresh.
12:01
There's our owners
12:05
now. If I look at the e r E r diagram, we can see that we created that constraint
12:11
and that is that. So we created two files.
12:18
The cars and the owners.
12:20
Larible is tracking our migrations in its own table. That gives us some capability,
12:26
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
12:43
how to use the PHP are Titian commands to create
12:46
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.
13:05
So that completes this lesson, and I hope to see in the next thank you

Up Next

Intermediate SQL

This free course introduces the student to intermediate concepts found in the implementation and application of Structured Query Language (SQL) within professional business environments.

Instructed By

Instructor Profile Image
Kitt Parker
Instructor