Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Importing the data from SQL to Mongo
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
It's looking like our app is working pretty well.
0:01
If we go and run it, try to pull up the home page here
0:05
you see, hey it looks like it's working.
0:07
It turns out when we have actual data
0:09
there's a few minor tweaks we're going to have to change.
0:11
That's not a big deal.
0:13
The bigger deal is that, well, there's no data here.
0:16
We have zero projects, zero releases, zero users.
0:20
How do we get that data in there?
0:21
We did have this load_data here where we took
0:24
a JSON file and imported into SQLAlchemy
0:27
but that's not what we're after.
0:29
What we want to do is take the data that's
0:30
in SQLAlchemy and put it in to MongoDB.
0:33
So I'm going to show you a cool technique
0:34
how you can take any database you connect
0:37
to through SQLAlchemy, go query all the data
0:40
and then transform that into MongoDB
0:42
MongoEngine classes and save that into MongoDB.
0:44
Basically, migrate data from one database type to the other.
0:48
So let's go create another script here.
0:57
So we have this file, now it's a little bit complicated
0:59
and not really worth you watching me type it in
1:01
so I'm going to paste something here
1:03
and we're going to talk through it real quick.
1:05
So we're going to go and initialize
1:07
both database connections.
1:09
One to MongoDB, as well as SQLAlchemy.
1:12
Because we need to read the data from SQLAlchemy or SQLite
1:15
and then push it into, save it into MongoDB.
1:18
And then, these three functions here, they all work
1:22
basically the same.
1:23
The first question is, are there any users?
1:25
If there are, we probably already run this
1:27
don't have to do that.
1:28
So be careful of the start with zero
1:30
but then it will not override it.
1:32
Then we just go and do a query against SQLAlchemy
1:34
for all the users. Get them all
1:37
and then we loop over them, one at a time
1:39
we create a MongoDB user, set the properties
1:42
and then call save.
1:44
Hash password is SQLAlchemy's hash password.
1:46
Name is SQLAlchemy's name. And that's it!
1:49
If you have a ton of data insert, doing a save
1:52
one at a time can be pretty slow, so you can do
1:54
bulk inserts as well with MongoEngine and what not
1:58
we don't have that much data. We're not doing it.
2:00
All the others, they're pretty much the same
2:02
packages, and releases, and so on.
2:05
So I want to show you a quick trick we're doing up here
2:07
that's kind of cool.
2:08
We're working with package, right?
2:10
So here's a package class and here's a package class
2:12
how in our code do we know which is which?
2:14
Well this one is the MonogDB one and this one
2:17
is the SQLAlchemy one.
2:18
So we're importing is as SQL package Mongo package
2:21
or SQL user and Mongo users.
2:22
That's just a cool technique to help you remain sane
2:25
while you're trying to run this.
2:27
So let's go ahead and run this and see what happens.
2:32
Takes a second, it's doing some work, and it's done!
2:39
So let's go look over here and we can do our query again.
2:42
Go show me all the packages. Ta-dah! There they are.
2:44
How cool is that?
2:46
We're got our ID name and email created.
2:48
We didn't copy over maintainers, because it turns out
2:50
we we're storing them in SQLAlchemy, so there's really
2:52
no way to copy them over.
2:54
Also, MongoEngine does not insert unmodified values.
2:58
Okay, so notice that some of the values that are not here
3:03
they're simply just not in there.
3:05
This is the package we want to move on
3:06
but still, we don't have the maintainers copied over.
3:09
Right? You can see it's just the values that we've set.
3:12
These times are the ones that come out of SQLAlchemy
3:16
not just the default ones, right?
3:18
So we copied all this over, saved in
3:20
and it's good to go.