1
00:00:05,290 --> 00:00:10,070
Welcome back in this lecture we're going to talk about flask migrate.

2
00:00:10,260 --> 00:00:16,280
We just showed how to create a database and then perform crud create read up and delete using Flast

3
00:00:16,280 --> 00:00:17,530
sequel alchemy.

4
00:00:17,550 --> 00:00:23,490
Now describe how to utilize migrations when creating a model for a database table.

5
00:00:23,490 --> 00:00:28,380
You're going to sometimes need to make adjustments that model inside your python file such as adding

6
00:00:28,380 --> 00:00:29,420
a new column.

7
00:00:29,460 --> 00:00:34,410
However just updating that Python script isn't going to automatically update the database.

8
00:00:34,410 --> 00:00:39,210
So upon making these changes we need to migrate these changes in order to update the actual sequence

9
00:00:39,360 --> 00:00:40,390
database table.

10
00:00:41,640 --> 00:00:46,450
And we can do this with flask migrate which you can install with Pip install flask migrate.

11
00:00:46,450 --> 00:00:50,950
This allows us to make adjustments in our model class and then make sure they actually take effect in

12
00:00:50,950 --> 00:00:55,190
the real SQL database and there are four main commands used at the command line.

13
00:00:55,360 --> 00:01:00,400
In order to perform this migration the first thing to do in order to make sure everything works is to

14
00:01:00,400 --> 00:01:03,240
set the flask app environment variable.

15
00:01:03,250 --> 00:01:08,980
This allows Flast migrate to no attraction that PI script is your flask application and the command

16
00:01:08,980 --> 00:01:12,050
is slightly different depending on what operating system you using.

17
00:01:12,070 --> 00:01:17,410
If you on a Mac OS or Linux you're going to say export and then all caps flask underscore app is equal

18
00:01:17,410 --> 00:01:22,900
to and then the name of the python script that actually holds your flask application.

19
00:01:23,480 --> 00:01:28,420
In Windows what you needs to do is say set and then flask app is equal to my app that.

20
00:01:28,440 --> 00:01:32,760
Or again whatever your flask application happens to be called when you run these two lines.

21
00:01:32,780 --> 00:01:37,340
You need to make sure they are in the same directory as the actual Python script.

22
00:01:37,340 --> 00:01:42,080
Otherwise your command line will say you can't find the file which makes sense because you need to be

23
00:01:42,080 --> 00:01:44,530
in the same directory in order to run that command.

24
00:01:46,530 --> 00:01:50,220
If you don't set the flask up then you're going to get an error and it'll say something like could not

25
00:01:50,220 --> 00:01:51,940
locate flask application.

26
00:01:51,940 --> 00:01:54,150
Either not provide the flask at environment variable.

27
00:01:54,150 --> 00:01:59,040
So if you ever get this error make sure you perform either one of these two lines depending on your

28
00:01:59,040 --> 00:02:03,270
operating system and you can make sure to check the migrations underscore instructions.

29
00:02:03,270 --> 00:02:08,820
Text file for a full text explanation of not just these commands but all the commands we show during

30
00:02:08,820 --> 00:02:10,800
the lecture.

31
00:02:10,970 --> 00:02:15,610
Once you set your flask application where you can do as are these three following commands.

32
00:02:15,710 --> 00:02:21,520
And so we start off with flask D-B in it and that basically sets up the migrations directory.

33
00:02:21,710 --> 00:02:23,270
Then we'll say flask.

34
00:02:23,300 --> 00:02:29,960
D.B migrate dash em and whatever your messages so that sets up the migration file and once you've done

35
00:02:29,960 --> 00:02:35,210
that you can say flask DBI upgrade that actually updates the database with the migration so to break

36
00:02:35,210 --> 00:02:40,610
down these three main steps we first actually set up a directory folder for our migrations.

37
00:02:40,610 --> 00:02:46,310
Then once you've actually made a change toward database we need to set a migration file so we say Flast

38
00:02:46,310 --> 00:02:52,480
the B migrate and then the dash and flag that stands for message and then you can report back what message.

39
00:02:52,490 --> 00:02:56,800
That way you can check the history of your migrations and you can understand what you changed.

40
00:02:56,810 --> 00:03:00,890
So maybe your message could be added column to the puppy table.

41
00:03:01,220 --> 00:03:04,540
So that's the migration file to actually perform the migration.

42
00:03:04,550 --> 00:03:05,470
You say then flask.

43
00:03:05,480 --> 00:03:09,480
DBI upgrade which then goes to migration file and performs the upgrade.

44
00:03:09,680 --> 00:03:14,460
Essentially updating the table inside of your sequel database OK.

45
00:03:14,630 --> 00:03:18,770
Let's get an idea of how to perform all of this by jumping over to our text editor.

46
00:03:18,950 --> 00:03:20,960
OK here and at my text editor.

47
00:03:21,050 --> 00:03:24,300
These are the same files and folders we were working with last time.

48
00:03:24,470 --> 00:03:30,170
Well we need to start off with is actually adding the capabilities of migration to our application and

49
00:03:30,170 --> 00:03:33,410
the way we do that is just by adding a couple of lines here.

50
00:03:33,530 --> 00:03:38,330
After you've done Pip install Flast migrate or if you're using a requirements text file you should already

51
00:03:38,330 --> 00:03:39,640
have flask migrate.

52
00:03:39,830 --> 00:03:49,360
You're going to type out from flask underscore migrate import and then you're going to import this migrate.

53
00:03:49,390 --> 00:03:54,270
Remember you can just Pipp install flask migrates.

54
00:03:54,280 --> 00:03:59,460
It's going to be a capital F dash capital M there but you should already have it in your requirements

55
00:03:59,510 --> 00:04:00,550
text file.

56
00:04:00,550 --> 00:04:06,880
Once we have imported this migrate class what we can do is we can connect this application that we created

57
00:04:06,880 --> 00:04:11,890
here on line with this database from sequel alchemy and the way we do that is the following.

58
00:04:13,140 --> 00:04:14,940
We just say migrate.

59
00:04:15,000 --> 00:04:20,760
You pass in your application and then you pass in your database and this connects the application with

60
00:04:20,760 --> 00:04:25,690
the database in order to add on migration capabilities to run those terminal commands.

61
00:04:25,830 --> 00:04:27,860
And this is all we need to do so far.

62
00:04:27,910 --> 00:04:33,360
And the reason we do this is because even though we've already set up our model class here will you

63
00:04:33,360 --> 00:04:38,600
make any adjustments to this model class such as adding a new column and we run this basic thought py

64
00:04:38,610 --> 00:04:40,130
file again.

65
00:04:40,320 --> 00:04:43,810
Our data its sequel lite database that's already been created.

66
00:04:43,860 --> 00:04:49,410
So we're not going to be able to register the new migration of a new column without using Flast migrate.

67
00:04:49,410 --> 00:04:54,300
Technically there are ways to do it in a very manual fashion but it's much much easier by just using

68
00:04:54,330 --> 00:04:55,680
Flast migrate.

69
00:04:55,680 --> 00:05:00,970
So we've imported migrate from flask migrate and then we've connected the application to the database.

70
00:05:01,170 --> 00:05:06,420
So let's save this and then we're going to come down to our command line and we first need to actually

71
00:05:06,420 --> 00:05:08,340
set our flask app.

72
00:05:08,370 --> 00:05:12,960
So because I'm on a Windows computer I use set as my key word here.

73
00:05:12,990 --> 00:05:21,250
If you're on a Mac OS or Linux you're going to use export I'm on Windows so I will say set flask underscore

74
00:05:21,880 --> 00:05:28,270
at equal to and then you just need to set it equal to whatever the python script files called that's

75
00:05:28,270 --> 00:05:30,290
holding this flask application.

76
00:05:30,430 --> 00:05:36,390
In my case I've called it basic thought Pi so I'll say set my flask app equal to basic thought by the

77
00:05:36,550 --> 00:05:39,120
enter and you shouldn't see any output there.

78
00:05:39,160 --> 00:05:41,890
Remember Mac OS or Linux are going to use export.

79
00:05:41,890 --> 00:05:47,020
Once we've set that up it's time to actually initiate and set up our migrations directory essentially

80
00:05:47,020 --> 00:05:48,580
creating a folder for us.

81
00:05:48,670 --> 00:05:57,500
So I will say now flask D-B in its hit enter and then you should see a couple of lines here.

82
00:05:57,500 --> 00:06:01,310
Things like generating a bunch of files or directories and appear at the top.

83
00:06:01,310 --> 00:06:06,590
Now you should see now a migrations folder with a bunch of stuff in it that's now going to allow us

84
00:06:06,590 --> 00:06:10,900
to connect this application with the database and very easily run migrations.

85
00:06:10,970 --> 00:06:14,310
So your migrations are going to go and say that migration is directory.

86
00:06:14,630 --> 00:06:19,330
And the next step for this is to actually create a migration command.

87
00:06:19,400 --> 00:06:26,930
So we do this by saying flask D-B migrates and then technically that's all you need to run.

88
00:06:27,170 --> 00:06:32,330
But usually it's better to leave yourself a little migration message that way when you look at the history

89
00:06:32,330 --> 00:06:35,700
of your migrations you can understand what you did for each one.

90
00:06:35,710 --> 00:06:44,130
Some subjoin say dash em and I will say created puppy table and used double quotes there.

91
00:06:44,150 --> 00:06:47,880
Hit enter and that creates a migration file.

92
00:06:47,900 --> 00:06:55,000
Now that migration has actually been executed so all we have there is a migration with a unique ID.

93
00:06:55,250 --> 00:06:58,520
And then what we need to do is actually run the migration.

94
00:06:58,520 --> 00:07:03,920
So this is very analogous to using GET AS and get commit and then get push.

95
00:07:03,920 --> 00:07:07,310
So if you're familiar get maybe this feels a little familiar to you.

96
00:07:07,490 --> 00:07:09,140
If not no worries.

97
00:07:09,350 --> 00:07:15,970
We're just going to now say flask D-B and then upgrade hit enter.

98
00:07:16,180 --> 00:07:20,310
And this will now upgrade and perform the migration for us.

99
00:07:20,370 --> 00:07:27,610
OK so now that we've performed the migration we've seen the changes in the stop basic postscript to

100
00:07:27,610 --> 00:07:33,100
our actual sequel light database so to show you a more realistic example we're going to do is add a

101
00:07:33,100 --> 00:07:34,480
column to her model.

102
00:07:34,480 --> 00:07:42,140
So let's go here to puppie the B model and then we have an ID column a name column and an age column.

103
00:07:42,280 --> 00:07:50,840
I'm now going to add a breed column say breed is equal to D-B column and this will just be text D-B

104
00:07:50,860 --> 00:07:52,240
text.

105
00:07:52,240 --> 00:07:57,880
So I've added a column to my model and I can do that here by taking name age.

106
00:07:57,880 --> 00:08:05,520
Also take brede and say self-taught breed is equal to breed.

107
00:08:05,560 --> 00:08:11,440
Now that I have updated my Python script model with this new column I should create a migration and

108
00:08:11,440 --> 00:08:14,250
then upgrade that database to effect the change.

109
00:08:14,290 --> 00:08:23,090
So the way this works is will say flask D.B migrate dash em and then give yourself a message that you

110
00:08:23,090 --> 00:08:25,540
understood what changes you did in our case.

111
00:08:25,560 --> 00:08:33,660
I'll say something like added Breede column create that migration file so that migration file is now

112
00:08:33,720 --> 00:08:38,730
underneath your migrations directory and then to actually upgrade the database to effect the changes.

113
00:08:38,730 --> 00:08:40,180
I just say flask.

114
00:08:40,430 --> 00:08:44,900
DBI upgrade and that's successfully upgraded the database.

115
00:08:44,910 --> 00:08:49,430
So now it matches up with the model I created here in my Python script.

116
00:08:49,700 --> 00:08:50,290
OK.

117
00:08:50,430 --> 00:08:56,340
That's really the basics of using Flast migrate in order to keep your actual sequel light database connected

118
00:08:56,400 --> 00:09:02,120
and updated to your model or models that play file or your application file.

119
00:09:02,160 --> 00:09:08,040
Coming up next we're going to continue our discussion of databases including relationships between two

120
00:09:08,040 --> 00:09:08,880
models.

121
00:09:08,940 --> 00:09:10,070
We'll see you at the next lecture.
