- Podcasts
- Python Bytes
- #196 Version your SQL schemas with git + automatically migrate them
#196 Version your SQL schemas with git + automatically migrate them
Python Bytes
- August 27, 2020
- 31 min
- Episode 196
About this episodeTopics covered in this episode: Surviving Django (if you care about databases) Python Numbers and the Flyweight design pattern What Are…
- Surviving Django (if you care about databases)
- Python Numbers and the Flyweight design pattern
- What Are Python Wheels and Why Should You Care?
- Pandas_Alive
- How To Use the Python Map Function
- Version your SQL schemas with git + automatically migrate them
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#196 Version your SQL schemas with git + automatically migrate them
The community seems pretty awesome. There's a lot of tutorials. There's a lot of expertise that they can help you out. So there's an interesting article by Dan Verrazzo called Surviving Django, If You Care About Databases. So, I mean, Surviving Django, right off the start, it's an odd title for an article about Django.
It's going to be kind of hard to summarize, but basically, the take on it is a little bit of a... There's a lot of people that are going to be able to do that. It's a lot of people that are going to be able to do that. And it's an interesting perspective, but the gist of it really centers around that there's a lot of parts of Django that seem to be database agnostic.
So you could use MySQL or Postgres or something else. But he says, kind of in reality, people don't do that. People don't really switch databases that much. So if you really want to utilize the database and some of the great things about whatever database you pick, maybe not being database agnostic is good.
Also, he talks about how to set up schemas and database migrations using the database, not using the built-in Django stuff. It seems a little bit more like, why would I do that? It seems more technical than I want to do with Django. But there is some reasoning around it. And then he also shows exactly how to do this, how to do migrations, how to do schemas.
And it really doesn't look that bad. The interesting take, I was curious about what the rest of the Django community would feel about this. But then after the article, there's comments on the article. There's a really nice civilized discussion between the author and someone named Paolo Melchior, I think, and Andrew Godwin.
And because there's a lot of fans of Django that really aren't going to talk about the bad parts. And this isn't necessarily the bad part. It's just something to be aware of. Another really interesting comment by Andrew was, I agree that at some point in a project or company's life, when it's big enough, SQL migrations are the way to go instead of the Django migrations.
Migrations in the out-of-box state are mostly there to supplement rapid prototyping. Like a lot of Django, it can be removed or ignored progressively if and when you outgrow the single set of design constraints when you chose them. So that kind of take of using Django's migrations and all the agnostic stuff might be good early on.
And then maybe slowly going towards using your database more later. Yeah. That's an interesting take. Yeah, that's cool. A bit of a practicality beats purity on both ends there. This article also made me really appreciate the Django community because this was not a flame war. This was a civilized discussion about a technical topic.
So it happens. But at the same time, like that's usually not my SQL to Postgres. It's usually like relational to non-relational or something massive where it's going to require rewrite anyway. So I do like the idea of saying you have this capability to be completely agnostic, but you're working with the lowest common denominator there.
And that's usually not the best choice if you're writing an application. Maybe if you're working with a library, tons of people are going to use it in ways you don't anticipate. But if it's an application, you know how it's going to be used most often. Yeah. Also, some of those speed and speed improvements you can get out of a database, you really can't do too much of with the agnostic front end.
You kind of need to know the specifics of that database. Yeah, pretty cool. For this next one, I want to talk about an interesting pattern that Python uses, I guess. Interesting technique. So you know the ID function, right? You can say ID of a thing and it'll give you a number back and it basically tells you what it is, like where it is in memory.
Are you familiar with this? I guess I don't use this. Yeah. So if you want to know, like if I'm giving two variables, are they actually referring to the same object or do they just have the same value? Right? Like if I had a dictionary and I want to know, is it the same dictionary or does it just have the same keys and the same values for those keys?
You can say ID of one thing and ID the other. And in CPython, that'll actually give you the memory address. But in all Python, that gives you a unique identifier that is guaranteed to be different if they're different objects, the same if it's the same object, right? Okay. Okay. So one of the things that Python does that's really interesting, and this is all research I've pulled up from working on my Python for memory management course that is probably out by the time that this comes out.
But you don't have to take that to care about this. So one of the things that's really interesting in Python is everything is a pointer, right? Allocated on the heap, including numbers and strings and other small stuff that might be allocated on the stack in languages like C# or C++ or whatever, right?
So numbers in Python are way more expensive than they are in languages that treat them as value types rather than reference types. So for example, the number four uses 28 bytes of memory in Python, whereas the number four could use one, two, four, or eight in the languages that treat them as value types, depending if they're like shorts or longs or whatever.
Right? So that's from Wikipedia. I'll link over to that. In Python, Python does that for numbers. So if you compute, like through some mathematical function, if you compute the number 16 and then some other way you compute the number 16 and then somewhere else you parse a string, the number 16, those are all literally the same 16 in memory.
Okay. Okay? Because 16 is pretty common. But if you computed 423, the three different ways, that would be three copies of 423. So Python uses this flyweight pattern for the numbers from negative 5 to 256. And you'll only ever have one of those in the language, in the runtime. But beyond 256 or below negative 5, those are always recreated.
But if you assign x to minus 10, x and minus 10 are different IDs. Isn't that funky? Yeah. It's because the numbers in Python are extra expensive. So Python takes special care to not recreate these very common numbers. And apparently, very common means negative 5 to 256. Anyway, I thought that might be interesting to people, this flyweight design pattern concept and then applied to the numbers might be interesting.
And there's a little example code that I included it there. So it's not quite an article, but it's like an idea with some code. Yeah. So can you, I mean, as a user, can I use the flyweight pattern in Python for other stuff? You totally should. Yeah. Like imagine you've got some objects you're creating and instead of recreating them over and over, they're being used in a lot of places.
The big important thing here to make this work correctly is they have to be immutable. Right. Because if one person gets the state Georgia and it has certain values and another person gets it, oh, it has a new county. Let's add that. And like, wait a minute. That's not. I've now not recreated a different thing or like, you know, so it's got to be immutable, which is why it works for numbers.
And you could do it for strings and things like that. Okay. Cool. Yeah. Pretty cool. Something else that's really cool is Datadog. So thank you, Datadog for sponsoring this episode. Let me ask you a question. Do you have an app in production that's slower than you like? It's performance all over the place, sometimes fast, sometimes slow.
Now here's an important question. Do you know why? With Datadog, you will. You can troubleshoot your app's performance with Datadog's end-to-end tracing. Use the detailed flame graphs to identify bottlenecks and latency in that finicky app of yours. Be the hero that got the app back on track with your company.
Get started today with a free trial at pythonbytes.fm/Datadog. Awesome. Thanks, Datadog. You know what else is awesome? What is awesome? Pip installing a thing that when I pip install something and it happens right away and it's not like 30 seconds of compile time, like say MicroWhisgy is, to get the thing installed and I don't have to have like MSBuild or VCVarsBat set up right or whatever.
But as a normal casual user of pip install, we don't really think about it. But this is the first half of this article talks about kind of what the user's perspective is. And it's kind of a nice look. When you say pip install something and it's a cool because it as an example, I'm glad they list an example.
And it's a particular version of MicroWhisky because most packages are wheels now. But if you install something that is not a wheel, it's probably a tarball. And I don't know if there's other options other than tarballs. But anyway, a tarball is something that ends in tar.gz. So it's a tarred and zipped.
And that's a whole bunch of Unix speak that you don't really have to care about. But it downloads this blob of stuff and then unpacks it. And then pip calls setup and some other stuff to build the wheel after you download it. And then it labels it and then it installs it. There's a whole bunch of steps in there.
Plus, it's calling setup.py. So there could be really any code in there. And so that's kind of creepy. The difference is often with if you actually have a wheel instead of a tarball, pip install will just pull this down and install it. And it doesn't call setup.py. That's really nice, actually, because one of the things I think a lot of people don't realize until they're like, oh, wait, what just happened?
When you pip install something, you're running semi-arbitrary code off of the internet. That's not ideal. Right. With the wheels, you don't have to run. Because basically that runs the setup.py in the sdist version, I believe. So this is really nice that wheels can cut out that Python execution bit.
So if you have compiled code, then there's kind of a different CI pipeline to try to build all those wheels. But on the user end, we don't have to care about it. So one of the different things is one of the interesting bits about going and moving towards wheels is there's a whole bunch, often a whole bunch of different, whole bunch of packages up there.
And that's something that users will see if they look at what downloads are available. There'll be this whole slew of stuff. And for the most part, you don't have to care about that. If you do pip install, it'll just pick the right one for your operating system. However, it's good to be aware of those because if you are creating like a cache of stuff at your, if you have your office or something, you may want to cache more of those depending on what operating systems are being used around.
Like all the awesome stuff that we happen to have covered periodically. But this one is called Pandas Alive. And so, Brian, to get the experience of this one, you need to open it up and just scroll through the readme on the GitHub page and just look at the animations. So you probably have seen these racing histograms or racing bar charts that show stuff happening over time.
Like here's the popularity of web browsers all the way back from 1993. Where it was Mozilla and then Netscape and then IE and then, you know, whatever. And you see them like growing and moving over time. Yeah. So this is a package that if you have a Pandas data frame in a really simple format where the columns are basically the different things you want to graph.
Yeah. And the, I mean, like the race charts and stuff, those are cool. But then you can also do the, like the line, the line graphs, the growing, zooming. Yeah. Yeah. You can do like line graphs and you can do other types of things, little plot, scatter plot type things. You can also do pie charts, but you can even have them together.
So you can have maps. So if you want to have a map evolving over time with like different countries or counties fading in and out, you could have like those two graphs animated side by side at the same time. So you could have like the chart of the bars as well as the map all animated together in like one graph.
Oh, that's cool. So then you can just generate this GIF and then put it wherever. Exactly. You can put it on your website. You can put it wherever you want. You can share it on Twitter, I guess, even. Right? But it doesn't require like a JavaScript backend running something and your Jupyter notebook and then all that kind of stuff to wire up.
Like, no, it's just an animated GIF that comes out. Neat. This is mesmerizing. I could just watch these all day. You could watch it for quite a while. So, yeah. Anyway, really think that's a cool project if you want to visualize data over time, which, you know, there's a lot of good reasons to do that.
One of the things that it has there is animated maps. But maps are something else also. There's also a map function, which has nothing to do with geographic maps. You probably learned a Python a long time ago, but do you remember being surprised by map and all? Yeah, map and all those things, they always confuse me and I've always tried to basically avoid them.
It's an extremely useful function, a useful thing. So it's a built-in. And what it does, if you're not familiar with it, it takes two or more parameters. The first parameter to map is the function that you want to apply. And then, like, let's say if you give it as the second argument an iterable, like a list or something, it takes that function that you passed in and applies it to absolutely every element of the iterable, the other one.
And you can make one list into another. I think it's good for people to, like, read about them every once in a while if they're not using them often because they do come in handy in places that you, all the time, for me at least. So, it's not an obvious thing if you're not used to this sort of a function from other languages.
I wasn't coming from C. Yeah. And maybe Perl has something like this, but I never used it. So, that's the normal use of applying it. One of the things I like about this tutorial is it goes through a few different things. So, applying lambdas to a list or an iterable, and then the function that you apply doesn't have to be a lambda.
It could be your own user-defined function, or it could be a built-in function that you map to it. I want to, like, warn people, the part where she's talking about the user-defined function, it's oddly complex for some reason. I'm not sure why this was made so complex, because a user-defined function just works like anything, any other function that's using for map.
But one of the things that I even got out of it is I had forgotten that map applies the function to the iterable one element at a time, and it doesn't do it ahead of time. So, like, for instance, and I'm like, really? And I had to, like, prove it to myself by putting a print statement or something in a function to do it.
But what happens is, like, let's say I've got iterable hooked up to grab, like, a huge data chunk out of a stream or something. I can apply some function to each element as I'm pulling it out and using map to do that, so I can iterate over map. So map returns a map object, which, whatever, it doesn't matter.
And the advice in this article is comprehensions are very useful for smaller data sets, but often for large data sets, map can be more powerful. So that's reasonable. And sometimes you want to do operations that if you had to go over different collections of data would make a really nasty-looking comprehension and stuff.
So, yeah, cool. Yep. You also can do, like, pandas type of things a little bit, like multiplying vectors, right? Like, if I've got two lists and I want to have pieces put together, like that power example that's in there, right? It'll take the first element of the first one, the second element of the second one, and then apply the function and generate a new list, effectively, that has, like, as if you had sort of done vector multiplication, which is cool.
Nice. So, we spoke earlier about databases and I've got another one for us. This cool thing called AutoMigrate. It's a project called AutoMigrate. Okay. So, what it does is it's kind of like you talked about Django migrations and we also have SQLAlchemy migrations with Olympic. But some people, either they're not using an ORM at all, in which case those tools are useless, or they want to very carefully write the SQL scripts that control their databases.
Like, some people, there's, like, a group of DBAs that manage the database and that's that, right? We're not going to run just random tooling against the database. We're going to run scripts that are very carefully considered. So, this AutoMigrate thing, what it will do is if you have a, those DDL, data definition language scripts that say create table, add column, and so on.
All it has to do is have the script that will say, here's how we create something from scratch. You put that into GitHub and then you make changes to it. Like, to add a column, I go and edit the create table thing and I just type in the new column in there. And what this will do is it'll look at your git history and it'll do diffs on the create table statements and it will generate the migration scripts from that.
So, I think Alembic is better, but the big requirement there is you are using SQLAlchemy. If you're not using SQLAlchemy to do these migrations, then this tool, but you're using these scripts instead to define your database, like, I'm sure a lot of, like, especially the larger companies where there's, like, a database team or, like, DBAs and so on are doing.
And this seems like a really cool project for it. Yeah. That said, the converse is actually pretty cool. So, what it can do is it can look at a database and it will generate your SQLAlchemy files for you. That's pretty cool. That's nice. Yeah, it'll generate ORM definitions from SQL, right, using the SQLAlchemy generator, which is pretty awesome.
So, you can say, here is my create table scripts, generate me the corresponding SQLAlchemy thing to match that. So, in that direction, it's pretty awesome also. So, which does that? This one, this auto migrate. It'll look at your DDL, like, create these table scripts, and it'll turn it into Python SQLAlchemy classes.
But the reverse, it was saying, like, oh, it's painful to use Olympic in the other direction. But if you use the auto generate feature of Olympic, then it's also not painful. But there's certainly a couple of use cases that are pretty awesome here. One, like, starting from all the create stuff, like, given a database, just ramp me up to getting a SQLAlchemy set of classes that'll talk to it as quick as possible.
Well, I've had a rough week, so I've got no extra stuff. No extra stuff? No extra stuff. I don't have too much either. I have a little bit. I just want to give a shout out that we have a ton of new courses coming. And I want to just encourage people, if they're interested in these, to go to training. talkpython.fm/getnotified and put the email there if they haven't created an account or signed up there before.
Because we have Excel, moving from Excel to Python with pandas coming out. We have getting started with data science coming out. We have Python memory management tips coming out. Those all three will probably be within, like, a couple of weeks. And then getting started with Git and Python design patterns as well.
So there's a bunch of cool stuff. If you want to hear about any of those, just be sure to get on the mail list. Oh, wow. That's cool. If I didn't talk to you every week, I would totally get on this mailing list. Awesome. Actually, I think I'm already on it. I'm sure you are. Because you do talk to me, though, you get jokes.
Definitely. But everybody who listens gets them also. That's right. This is a fun game to play. The idea is you take some actual, legitimate, classical painting. And if you go to an art gallery, it'll say, like, flowers in bloom, oil canvas, Monet 1722, or something like that, like, in the little placard underneath.
So the game is to reinterpret these paintings in modern tech speak. Okay? Yeah. So here, I'll do the first one. I put three in the show notes that people can check out. I'll describe this to you, and then I'll read the little thing. So there's, like, a ship that seems to be on fire with some extremely strong guys trying to drag the ship out of the water.
Maybe, no, they're pushing it into the water. And a bunch of folks on the edge sitting off. It's like a Viking ship. I think they're actually cremating. Somebody's sitting out. Anyway, it's this historical picture, and it says, the placard says, Engineers remove dead code after dropping a feature flag.
It kind of looks like a violin. And the title is CSS Without Comments. That's good. Pablo Picasso, 1912. All right. The last one. The last one we'll do. By the way, there's hundreds of these. They're all really good. So this one is a little disturbing. There's a person who looks deathly ill with a bunch of, like, gargoyles over them.
And were you there for that? You might have been. No, I wasn't. I missed that one, but. That was good. Remember that one when you could go to conferences? If there were people around you? Other people? Close? It was weird. Actually, we don't need anybody to contact us and tell us that we have no idea when different painters were alive.
That's what burgers do. Let's do it with, that's right. Let's do it with abstract art. Yeah. Yeah, it's funny. Anyway. Awesome. All right. Well, thanks, Brian. Thank you. Yep. Bye. Bye. Thank you for listening to Python Bytes. Follow the show on Twitter at Python Bytes. That's Python Bytes, as in B-Y-T-E-S.
And get the full show notes at Python Bytes.fm. If you have a news item you want featured, just visit Python Bytes.fm and send it our way, where I was on the lookout for sharing something cool. This is Brian Okken, and on behalf of myself and Michael Kennedy, thank you for listening and sharing this podcast with your friends and colleagues.
