1
00:00:05,210 --> 00:00:08,090
Welcome back, everyone, to this lecture on migrations.

2
00:00:09,430 --> 00:00:15,910
In general, migrations are going to be the act of connecting changes in your Jingle project or jingle

3
00:00:15,910 --> 00:00:18,910
application to the back end database.

4
00:00:19,060 --> 00:00:24,490
And this includes things like adding new models in an application, adding an entirely new application

5
00:00:24,490 --> 00:00:30,160
or even updating models of a new column or attribute in an application and models that profile that

6
00:00:30,160 --> 00:00:30,970
already exists.

7
00:00:31,150 --> 00:00:37,150
So you can think of this as any general changes that should be being kept track of in the database should

8
00:00:37,150 --> 00:00:41,500
eventually get migrated from Python code to the back end SQL database.

9
00:00:42,970 --> 00:00:48,130
Now, typically, you'll see these commands done through the managed pie file, so we're going to discuss

10
00:00:48,130 --> 00:00:55,330
three key based commands for managing migrations, and those are the Make Migrations Command, Migrate

11
00:00:55,330 --> 00:01:00,910
and then cycle migrate, which is kind of a specialty command that we won't use too often, but we will

12
00:01:00,910 --> 00:01:02,470
show it in this particular lecture.

13
00:01:03,040 --> 00:01:05,349
Let's actually begin with make migrations.

14
00:01:07,190 --> 00:01:11,240
When you make migrations, you're going to run it with the managed top pie file, so something like

15
00:01:11,240 --> 00:01:15,530
python, manage the pie, make migrations and the name of your application.

16
00:01:16,010 --> 00:01:22,040
What make migrations does is it creates but does not actually run the set of instructions that will

17
00:01:22,040 --> 00:01:24,290
apply changes to the database.

18
00:01:24,770 --> 00:01:30,080
So, for example, if you run Make Migrations app, that's going to have a Python file, be created

19
00:01:30,080 --> 00:01:33,230
and ready to be run in order to actually change that database.

20
00:01:33,560 --> 00:01:39,620
Make migrations by itself does not actually perform any changes on the database, and you should note

21
00:01:39,620 --> 00:01:45,440
that the default applications in Django, like admin or authentication, you can think of them as already

22
00:01:45,440 --> 00:01:50,240
having had their skill make migrations code ready to go, and it's already been run.

23
00:01:50,480 --> 00:01:55,910
However, they haven't actually been migrated yet, so the migrations are ready to go, but they haven't

24
00:01:55,910 --> 00:02:01,340
been applied to the database, which is why you'll be able to see most tutorials as well as our own

25
00:02:01,340 --> 00:02:01,670
code.

26
00:02:01,970 --> 00:02:03,430
Well, we run right off the bat.

27
00:02:03,440 --> 00:02:08,600
Something like Python managed that pie migrate and you saw that some migrations were being applied.

28
00:02:08,840 --> 00:02:10,070
Those were the default ones.

29
00:02:10,070 --> 00:02:11,180
But more on that later.

30
00:02:11,600 --> 00:02:17,330
When you actually run, make migrations with your app, you can actually see the migration files being

31
00:02:17,330 --> 00:02:17,750
created.

32
00:02:18,170 --> 00:02:22,970
Those will be automatically located in your application and a new directory called migrations.

33
00:02:22,970 --> 00:02:25,250
And then your very first one would be something like this.

34
00:02:25,580 --> 00:02:31,550
Zero zero one underscore initial that pie and we'll explore this python file, but it contains a set

35
00:02:31,550 --> 00:02:35,270
of instructions to go then and update your database.

36
00:02:35,270 --> 00:02:40,430
And eventually these instructions are converted into sequel that can interact with a skill based database.

37
00:02:40,910 --> 00:02:45,470
What's really nice about this is technically this Python file is very human, readable, and you can

38
00:02:45,470 --> 00:02:46,430
edit it yourself.

39
00:02:46,880 --> 00:02:48,290
Typically, we won't be doing that.

40
00:02:48,320 --> 00:02:49,670
That's pretty advanced stuff.

41
00:02:49,670 --> 00:02:53,660
And in general, you want to Django handle the heavy lifting and more complex tasks.

42
00:02:53,960 --> 00:02:56,330
But in case you wanted to know, you could go in there.

43
00:02:56,360 --> 00:02:56,930
Open it up.

44
00:02:56,930 --> 00:03:03,950
Explore and edit the sexual python file before you end up migrating or running the migrations so it

45
00:03:03,950 --> 00:03:09,500
can make migrations, makes the migration instructions, but doesn't actually run them to connect to

46
00:03:09,500 --> 00:03:10,160
the database.

47
00:03:11,190 --> 00:03:13,290
That is the migrates command.

48
00:03:13,470 --> 00:03:16,680
So we run Python, manage that pie, migrate.

49
00:03:16,830 --> 00:03:19,380
And this is going to run any existing migrations.

50
00:03:19,590 --> 00:03:20,970
How does it find those migrations?

51
00:03:21,270 --> 00:03:25,590
Well, we already know that when we create them through the Make Migrations command, that there's a

52
00:03:25,590 --> 00:03:32,010
directory called migrations in the same way jingles able to find static or template files using directories

53
00:03:32,010 --> 00:03:33,330
like static or templates.

54
00:03:33,660 --> 00:03:40,500
It's also able to find migration instructions in the migrations directories, which is why, when you

55
00:03:40,500 --> 00:03:43,350
run make migrations, those directories are created for you.

56
00:03:43,500 --> 00:03:46,920
So that's a set of instructions that migrate then runs.

57
00:03:47,100 --> 00:03:50,970
So this is actually running those Python files under the migration directory.

58
00:03:53,070 --> 00:03:56,670
Now, the last commander I want to show you is Scott Migrates.

59
00:03:56,760 --> 00:04:00,000
And this one's a little more complicated and we won't really use it that often.

60
00:04:00,330 --> 00:04:05,760
But if you go and run in Python managed up high school migrate, then the name of your application and

61
00:04:05,760 --> 00:04:09,360
the number associated with the specific migration python file.

62
00:04:09,720 --> 00:04:13,230
This would actually show you what the school code looks like.

63
00:04:13,590 --> 00:04:17,339
Keep in mind, it doesn't actually run any migration to your database.

64
00:04:17,640 --> 00:04:24,630
It just spits out the set of SQL instructions in case you had maybe a backend SQL based developer or

65
00:04:24,630 --> 00:04:30,940
database administrator who wanted to see what SQL code was actually being run to your database.

66
00:04:30,960 --> 00:04:36,540
So it's really useful for things like debugging or interfacing with some sort of database admin.

67
00:04:39,370 --> 00:04:43,840
Now again, you should note that we're not typically going to be reviewing files created under the migration's

68
00:04:43,840 --> 00:04:48,520
directory or even be running Sequel Migrate, but we're going to be doing usually is just running,

69
00:04:48,520 --> 00:04:50,440
make migrations and then migrate.

70
00:04:50,980 --> 00:04:56,050
You can think of the very first migrate command we run as executing the default make migrations that

71
00:04:56,050 --> 00:05:00,790
was already created for you upon creating the project, which is why when you create a new project,

72
00:05:00,790 --> 00:05:06,940
you can actually run Python managed up high migrate and see things like the default authentication and

73
00:05:06,940 --> 00:05:12,640
admin applications have their migrations being connected to the database, which is also why you see

74
00:05:13,150 --> 00:05:19,390
upon running migrate the database actually being created, which is why again, we run that right off

75
00:05:19,390 --> 00:05:21,520
the bat without actually creating a new application.

76
00:05:22,640 --> 00:05:24,260
So what are the actual steps for migration?

77
00:05:24,740 --> 00:05:29,120
I would recommend that you run your initial project migrant command that's going to create that database

78
00:05:29,120 --> 00:05:34,370
for you and also make the migrations necessary for things like authentication and administration.

79
00:05:34,910 --> 00:05:39,350
Then as your project grows larger, you're going to end up creating an app and creating models associated

80
00:05:39,350 --> 00:05:39,890
with that app.

81
00:05:40,280 --> 00:05:44,480
Then you have to remember to register the app and installed apps in settings that pie.

82
00:05:44,870 --> 00:05:49,610
Having to register the app makes Django very modular, and it's very plug and play that way.

83
00:05:49,820 --> 00:05:55,160
You can go ahead and grab this application and use it in a new project simply by registering it in that

84
00:05:55,160 --> 00:05:57,020
new projects settings that pi file.

85
00:05:57,080 --> 00:06:01,130
It also makes it easy to quickly disconnect an application from an existing project.

86
00:06:01,790 --> 00:06:05,930
Once you've created the app, created the models and register the app, then you run.

87
00:06:05,930 --> 00:06:11,270
Make migrations for that new app to get a set of instructions, and then you run migrate for those new

88
00:06:11,270 --> 00:06:11,960
migrations.

89
00:06:13,200 --> 00:06:19,260
OK, so let's explore these concepts based on the patient model class created in the previous lecture.

90
00:06:19,530 --> 00:06:25,170
Recall that we've already run Python Managed Pie Migrate in order to run the migrations for the very

91
00:06:25,170 --> 00:06:30,420
first set of default migrations ready to go like authentication and administration.

92
00:06:30,690 --> 00:06:34,950
Now we're focused on the models that we ourselves have created in our new application.

93
00:06:35,480 --> 00:06:36,480
Going to head to the coding app.

94
00:06:37,820 --> 00:06:45,950
OK, here we are back inside our code editor recall that we've already ran the Python managed pie migrate

95
00:06:46,070 --> 00:06:50,840
and note that it's applying migrations, all the things that are by default included in a single project

96
00:06:50,840 --> 00:06:53,930
like administration authentication sessions, et cetera.

97
00:06:54,350 --> 00:07:00,860
So it runs all those migrations, and you can also see this kind of zero zero one sort of initial Python

98
00:07:00,860 --> 00:07:05,300
script naming scheme that are underneath directories migrations.

99
00:07:05,330 --> 00:07:11,150
Now you don't really get to see those, but if you click here, you can see there's in it that PI file.

100
00:07:11,330 --> 00:07:15,530
We're going to talk a lot more about being able to edit things default at a project level.

101
00:07:15,530 --> 00:07:19,940
When we talk about the admin panel buffer right now, just go ahead and kind of ignore that and just

102
00:07:19,940 --> 00:07:24,710
think, OK, there's some default applications that are already installed under settings that PI.

103
00:07:24,980 --> 00:07:32,360
You can confirm that if you just go to the My Site settings, that PI essentially have all these installed

104
00:07:32,360 --> 00:07:34,340
apps and we ran migrations for them.

105
00:07:34,670 --> 00:07:37,250
But what about our own models on our own apps?

106
00:07:37,400 --> 00:07:42,200
So we already have the patient model created, but we haven't actually made a set of instructions yet.

107
00:07:42,710 --> 00:07:45,680
So we're going to do here because we're going to say Python.

108
00:07:46,780 --> 00:07:54,190
Managed the pie, make migrations and then pointed to our application and our application name was office.

109
00:07:54,760 --> 00:08:00,070
Now what's going to happen is it's going to try to figure out, OK, do you have an application called

110
00:08:00,070 --> 00:08:00,550
office?

111
00:08:00,790 --> 00:08:02,680
And then what are its models, etc.?

112
00:08:02,800 --> 00:08:09,940
In order to make sure this actually works, you'll need to register the application as a configuration

113
00:08:09,940 --> 00:08:11,140
within installed apps.

114
00:08:11,530 --> 00:08:17,800
So before you run this, what you need to do is come to again settings, not pie, and check the installed

115
00:08:17,800 --> 00:08:18,320
apps.

116
00:08:18,340 --> 00:08:21,220
You'll notice our office app isn't actually there yet.

117
00:08:21,700 --> 00:08:25,090
So what we're going to do is just come to app stop pie.

118
00:08:25,120 --> 00:08:26,980
We've actually already done this one time before.

119
00:08:27,370 --> 00:08:30,910
And note that by default, it's called office config.

120
00:08:31,480 --> 00:08:33,850
So we're going to just use office config.

121
00:08:33,850 --> 00:08:37,470
If you want, you can copy it, but come back to settings, not pie.

122
00:08:39,100 --> 00:08:41,650
And then we're going to add that in, so we're going to say the following.

123
00:08:42,190 --> 00:08:48,760
You can find our new installed app under office apps and then the office config.

124
00:08:48,790 --> 00:08:50,020
Don't forget the comma here.

125
00:08:50,530 --> 00:08:54,790
So this is the way you can then register this in settings that pi that way.

126
00:08:54,790 --> 00:08:56,320
When to save those changes?

127
00:08:56,440 --> 00:09:01,910
This python managed up to make migrations, knows what you're talking about when you actually say office.

128
00:09:02,260 --> 00:09:06,370
So make sure you do this first under installed apps before you actually run this command.

129
00:09:06,970 --> 00:09:08,530
If you get an error, run the command.

130
00:09:08,530 --> 00:09:11,740
You should always be checking settings that pie to see if there's a simple fix there.

131
00:09:12,130 --> 00:09:13,210
So we're going to enter here.

132
00:09:14,210 --> 00:09:19,700
And it says, OK, I made some migrations for office, and by the way, they're located here office

133
00:09:19,700 --> 00:09:20,360
migrations.

134
00:09:20,660 --> 00:09:25,250
And here's an issue about PI and the general comment there is I created model patient.

135
00:09:25,280 --> 00:09:27,680
So even the comments are automated for us, which is really nice.

136
00:09:28,190 --> 00:09:34,400
Now, if we go to office under migrations, I can now see there's zero zero zero one underscore initialed

137
00:09:34,400 --> 00:09:34,910
that pi.

138
00:09:35,390 --> 00:09:42,590
And if you open this up, this is the Python file created by Django, which is eventually going to create

139
00:09:42,830 --> 00:09:45,890
the actual sequel code in order to interact with the database.

140
00:09:46,310 --> 00:09:50,170
Technically, you could mess around with this and edit things.

141
00:09:50,180 --> 00:09:55,850
For example, you notice that it actually uses Django DB, but this time it's using a migrations class.

142
00:09:56,240 --> 00:10:00,860
So a lot of this work has already been done for you, and Django has this migration class that automates

143
00:10:00,860 --> 00:10:01,430
all of this.

144
00:10:01,850 --> 00:10:04,620
You'll notice there's a bunch of variables that are important.

145
00:10:04,640 --> 00:10:09,920
For example, initial equals true that just tells Django, by the way, this is the first migration

146
00:10:09,920 --> 00:10:11,480
I'm going to do so initial.

147
00:10:11,480 --> 00:10:12,020
It's true.

148
00:10:12,320 --> 00:10:16,580
You can actually see that reflected in the naming schema with zero zero zero one score initialed that

149
00:10:16,580 --> 00:10:16,940
PI.

150
00:10:17,480 --> 00:10:20,410
And then it says, OK, I'm going to create this model.

151
00:10:20,420 --> 00:10:23,210
The name of the model is patient, and then here are the fields.

152
00:10:23,510 --> 00:10:29,700
And then it shows you the fields, and you'll notice by default, it actually creates a primary I.D.

153
00:10:29,780 --> 00:10:34,610
key field, which if you're familiar, if Django or excuse me, familiar of sequel, that's a way to

154
00:10:34,610 --> 00:10:37,970
identify each row as a unique ID row.

155
00:10:38,000 --> 00:10:41,420
So you don't need to worry about that yourself when you create the models up here.

156
00:10:41,540 --> 00:10:46,100
So even though you have first name, last name and age, when you actually create this, there is going

157
00:10:46,100 --> 00:10:49,100
to be an initial ID index for that table.

158
00:10:49,910 --> 00:10:55,220
Now maybe you're working with someone who works a lot with a sequel database, and they're not super

159
00:10:55,220 --> 00:10:56,000
familiar of Django.

160
00:10:56,000 --> 00:11:00,800
So they ask you, Hey, by the way, what is the actual SQL code that Django is running?

161
00:11:00,980 --> 00:11:02,180
If you wanted to check that out?

162
00:11:02,450 --> 00:11:03,330
You could do the following.

163
00:11:03,410 --> 00:11:10,580
You could say Python managed our pie and then call the sequel Migrate Command, call your application

164
00:11:10,940 --> 00:11:17,390
office and then point it to the number that you were concerned about for the actual migration file.

165
00:11:17,720 --> 00:11:20,220
For example, the first one, we have zero zero zero one.

166
00:11:20,270 --> 00:11:23,240
So it can say zero zero zero one enter.

167
00:11:24,300 --> 00:11:26,400
And this will report back to you.

168
00:11:26,760 --> 00:11:30,960
The school code that will be run again, you don't need to worry about this actually running in a school

169
00:11:30,960 --> 00:11:31,260
code.

170
00:11:31,530 --> 00:11:37,200
It's just saying, OK, when you run this Python file, that's going to automatically create this SQL

171
00:11:37,200 --> 00:11:38,190
script for you.

172
00:11:38,340 --> 00:11:44,010
So then if your SQL database admin is questioning, Hey, what's Django actually doing here?

173
00:11:44,310 --> 00:11:48,780
You would just run this for whatever number of Python file migration you're interested in.

174
00:11:49,110 --> 00:11:54,840
And then you could send that back to her or him and say, Hey, here is the SQL code that is being run.

175
00:11:55,820 --> 00:12:02,030
OK, now, as I mentioned, just setting make migrations doesn't actually make the changes to the database

176
00:12:02,030 --> 00:12:03,830
file when we're ready to do that.

177
00:12:04,070 --> 00:12:09,170
We say Python, manage the pie, migrate.

178
00:12:09,530 --> 00:12:13,610
That's going to look at all the migrations that haven't been run yet throughout all the applications,

179
00:12:13,610 --> 00:12:14,870
and then I'll go ahead and run those.

180
00:12:15,410 --> 00:12:21,160
So I hit Python, manage the pie and it says, OK, I'm looking at all the operations I need to perform.

181
00:12:21,170 --> 00:12:25,520
I'm again checking admin authentication sessions and notice.

182
00:12:25,520 --> 00:12:26,690
It's also checking office.

183
00:12:27,080 --> 00:12:30,650
Now, it's not going to rerun the migrations that it already ran for Adam and authentication.

184
00:12:30,980 --> 00:12:32,100
So then it reports back to you.

185
00:12:32,120 --> 00:12:37,340
Well, the migrations that I haven't run yet that I'm going to run are the office ones and it says I'm

186
00:12:37,340 --> 00:12:43,220
applying office zero zero one initial pilot, which is basically saying I ran this python file, which

187
00:12:43,220 --> 00:12:46,940
in turn creates this SQL code, which in turn interacts with the database.

188
00:12:47,270 --> 00:12:50,330
OK, so that's the very basics of migrations.

189
00:12:50,360 --> 00:12:55,610
Again, you have the make migrations, which creates this python file set of instructions.

190
00:12:55,940 --> 00:13:00,470
And then if you did want to check that skill code, you could say SQL migrate to check that out.

191
00:13:00,890 --> 00:13:06,390
And if you are ready to actually run the migration, then you say Python managed API migrate.

192
00:13:06,890 --> 00:13:07,730
OK, that's it.

193
00:13:08,090 --> 00:13:11,930
I'll see you in the next lecture or begin to talk about database interaction.

194
00:13:11,960 --> 00:13:17,000
How do I create, read, update and delete from this backend database through Python and Django?

