MongoDB with Async Python Transcripts
Chapter: PyPI Beanie
Lecture: Creating a Release

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The next thing I want to tackle here is creating a release. How's that going to go? Well, we need to gather some information from the user,
0:07 like what package are you creating the release for? What version is it? Major, minor, and build? What's the comment on the release?
0:16 What are the changes in this particular release? The change log sort of thing. So we're going to do that here,
0:23 and I've got kind of that structure I just laid out, put together, and then we'll write the MongoDB code to do it.
0:29 So here we're going to say we're creating a release, give me the name, go find the package. Remember we already wrote that, that's cool.
0:35 And if it doesn't exist, sorry you can't update it. Then we'll ask you for the version, come up with that by creating a tuple
0:43 and then exploding that out, projecting that into those three variables. Get our comment, get the size and bytes, the URL where it goes,
0:51 or you can leave that empty for the release notes if you want. And then we're going to need to take all that information and create one.
1:00 Basically create the release. So let's go and add that to the package service.
1:06 Again, my charm takes a guess, but first thing it misses is it doesn't know async apparently. And then this is going to be an int.
1:17 Okay, some of this code we need to run again because even though our use case of it up here, we did check that there was a package
1:27 and it would be an error if not, we still shouldn't assume that every caller is doing that correctly.
1:33 So what we'll do is we'll say package equals await package by name, name. Could do a better job of handling exceptions
1:44 and create our own exception type and all that, but the point is not this, right?
1:48 Now that we have our package, maybe we should check it doesn't actually have a release
1:52 with whatever these three versions are, but we're going to assume that's fine. And we're just going to go ahead and create a released object.
1:58 Remember our package has our releases list, which is a list of release. And so we're going to create one of these and then append it to there.
2:08 So we'll say this is going to be a release. Import that. And it's going to take a major version.
2:16 And by the way, this autocomplete comes because I have the Pydantic plugin for PyCharm. So major version equals major.
2:24 Look how much easier it is to write with that thing. Minor version equals minor. Build version equals build.
2:32 Over here in our daytime, one thing that we didn't do is we didn't give it a default, right? But I think that's probably a good idea.
2:40 Let's go ahead and do that again like we did earlier. dot field default factory is going to be date time, not date time dot now remember, not
2:52 the function. No, the function, not the result of calling the function. And let's go ahead and add this just over here as well.
3:03 Anything else need one of those? No. Okay. Do we do it for our user as well? Let's go ahead and they've got a created date in the last updated date.
3:13 I release analytics, it has no time. Okay. So we don't need to specify the created date because it has this default, but we do have
3:25 a comment, which is going to be the comment and URL and be the URL. And the size is going to be the size.
3:37 The other thing I want to check real quick is that the type of the URL is the same as our expectation here. It's not.
3:45 We said that it could be none, but if it passes in as none, it's going to crash here.
3:50 So let's go ahead and amend our release to say that this type could also be optional. Here we go.
3:58 So we have our release created, and we want to put it into the packages. There's an interesting thing we can do here.
4:03 So let's do it the most natural way and I'll show you an improvement we can get as well. So we just append the release.
4:15 So we want to go to the collection of releases and just put this on the end and then we want to save this.
4:21 Again database calls so we await package.save. Excellent. What do we return out of here? Probably nothing. I don't need to do anything.
4:33 It's either going to work or there'll be an exception. So this should be fine. Let's go ahead and do this here. Run our CLI again.
4:42 So we'll create a release and it says, let's go and add this to Pydantic. Do we have Beanie? Let's see. Oh yes. So we're going to do a 2.0.0.
4:54 Big time release, major changes, 2.0. This is the big rewrite. If you've never heard the song, the big rewrite, search it on YouTube, it's funny.
5:05 Okay, size and bytes is 12743. Release notes, we're just gonna leave that empty. Look at that, release added for Beanie 2.0.
5:17 Let's go and actually see, we can go find which one of these is the one, the packages, there we go. Don't need our limit anymore, we'll just say
5:27 underscore ID is beanie. And if we go way to the bottom, there it is. This is the big rewrite. Super cool, right? Now, the other thing we've got to do,
5:43 we have to be very careful here about is we're storing the number of releases as a separate counter in our database. And there's a big danger for that
5:52 in that this becomes out of sync. So we need to make sure everywhere that we add a release or remove a release as well
6:01 that we somehow increment or decrement that counter. So let's do that real quick as well. So here, let's do one more time to make sure this is working.
6:21 And then we have a way to improve this function. Alright. So let's create a release. Notice how many releases we have. Instant 804, create a release.
6:31 And we're gonna call this 2.0.1. And let's show the stats again. And look, it went from 804 to 805. Excellent, so that's now consistent and working.
6:51 Great, our create release function is working as we expected. We'll see though, there's a way we can actually improve this if we want.


Talk Python's Mastodon Michael Kennedy's Mastodon