Back to the video

🚀 Space Reviewers 👾 Episode 3 - Django - Backward operation for RemoveField enhancements

Transcript

By Speaker 1, Tim, Sage. From Djangonaut Space 2026. Published January 13, 2025.

Watch the video Markdown version 11,602 words · auto-generated · en

Summary

Django migrations can run forwards or backwards, and reversing a `RemoveField` operation re-adds the column. If that field is non-nullable and the table already contains rows, the database needs a value for each row; without one, the reverse migration can fail with a not-null constraint error. The reviewers tested a proposed change that prompts for a default when creating the migration, then examined its tests and message for clarity. They also discussed backend differences—MySQL may supply an implicit default—and noted that the prompt’s second response option needed a test.

Transcript

Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.

0:00 Introduction

And

Speaker 1

and thank you for joining us. Today we have a new member of the queue. We are now a fellow Sagent team and today we are going to make a code review about another ticket that we have truths and our mainly goal is to is to help people to make a better code review and also show our process and how we make some de ch some decision and how it it's and how we hope to help you as well

Tim

Yes. Yeah. Hi, I'm Tim. I'm one of the other space reviewers. And Sage, would you like to introduce yourself?

Sage

Hello, yeah, I'm Sage. I'm a member of the Django community, I suppose. And yeah, today I just this is my first Django Night Space review reviewers session. So Let's see how it goes.

Tim

Woohoo. Yes. Yeah, so this review session is typically we take a PR that is in some various state of review. Today we are looking at a PR that is handling a migration operation called remove field and we're it's supposed to add a default to it. It'll be a little bit different than what we've done in the past. in that the code and documentation for this look pretty good, but I think we will have to go through some more of the evaluation of the code, which we we've tried in the past, haven't made it really far, so I think this will be a nice mix-up for us.

And Raphael, she already explained, we do this to help expose the process, try to make it a little bit more accessible. Yeah, hopefully this helps people understand how some other people might approach code reviews. And so you can use that in your own professional world or review some Django PRs too. There go ahead, Rafaela.

Speaker 1

Oh can I? We also have would like to make a call to help us to choose a next PR that we could be use in our next online review and you can you can find out this form inside our repository or inside the maybe inside of description of the YouTube channel

Tim

That is a good note. I don't know if we let me Yeah. If we don't have that, we'll have it next time. Yeah, and the last thing is please be respectful. We're operating under the Jenga Space Code of Conduct. The link is also on a repository if you have not if you're not familiar with it. Alright. So with that We have our little show notes for everybody to look at. It identifies the ticket, the PR, and then our summary of the issue. And so the goal here is to kind of

Pull out the relevant bits that we need to know or that you need to understand.

Sage

I think the zoom controls is obstructing the view a bit.

Tim

Oh. Oh okay, thank you.

Sage

Cheers.

4:01 Migration Reversal and the Ticket

Tim

Yeah, so we have the Episode notes here and we try to identify the main pieces of information that you're going to need to understand to do a reasonable code review. So we sometimes some tickets It can have a very long set of comments. This one is not one of those. We probably didn't need to summarize all the discussion, but we did. Sage, would you mind introducing the ticket as you understand it?

Sage

Oh. Okay, so I think this is about the migrations feature in Django. And when you do migrations, a migration in Django consists of different operations that we that we add to the operations list inside the migrations class. And so Django by default has built in Operations such as add field, remove field, and for example also like create model. And these are the operations that will be performed on the database.

That's what will be that's how the changes will be made to your database and migrations can be executed forwards and also backwards. So for example, if you add a field to your model and you run the mic sorry, if you just added the field to your model in the code and then you run make migrations that's going to generate this migration file which has this migration class and also a list of operations and this is only a defin the definition of what will what

will be changed in your database, but it will not actually run until you execute the migrate command. And when you might when you run python manage. by migrate, that's going to execute the operations in order. And So the operation has one second. So yeah, that's okay, nice. Yes, so in here you see the SQL Statements that will be executed when you run the migrations.

And you can do that by using the SQL migrate command. And this is the forward operation, which yeah is the changes that that will be made to the database. But an operation can also have the Backwards operation.

Tim

Sorry, I don't I never remember it's backwards or reverse.

Sage

Right, yeah. So this is the reverse or like the backwards operation of that same migration. So for a create model operation, which translates to creating a table on the database. The reverse operation will be sorry, the backwards operation will be dropping the table. And yeah, if you change the field name of a model, that means the reverse will be changing the field name back to the original name before the change. And the ticket is about

Tim

yes. Hold that back up.

Sage

Right. So The ticket is about the backward operation for a remove field, which removes a field from your bottle, and it doesn't allow default value if the field is not null. So it also has the null equals false in the definition So yeah, as you can see in the description, the backward operation will break if the database is populated because If you refers if you refer a remove field operation, that's going to add the field back. But currently before with this

tickets still open. The issue is that the field will be added, but it will not have any values even if you have a wait one second. It is it when it has a default, like is it ignored?

Tim

So I think the default in the past was used. I think this is about catching the cases where there isn't a default and yeah, trying to alert the user like, hey, your backwards migration won't work.

Sage

Right. Yeah, so currently Django will just proceed to create a migration and if you try to run it, Django will not have any handling and it's just going to break, I suppose. if you try to run the referrus operation because yeah it adds the column but it cannot populate the column. So yeah, it's going to trigger the not null constraint on the column.

10:01 Prior Art and Review Plan

Tim

Yeah, yeah. Databases are tricky like that where they expect if you say it's not null, you can't create a column with all nulls. Okay. Thank you for that overview. Let me get rid of that. Yeah, I don't know So the discussion here I thought was pretty interesting. I'd if Florian and Simon had a discussion, I think this was years ago. But basically they wanted to do a very similar thing as like with AddField, where if you try to add a new field on a table that also has roll rows and you're adding a field that is not null

that it tries to it pauses and says, hey This is going to fail when you run the migration if you don't specify default or set a value for all the rows as it's this column is being added, or you have to make this a nullable column. And then I think there's also the option to just say. Yeah, I know what I'm doing. Ignore this Django. And so Florian pointed out that we should be able to do this in the reverse direction too, of having somebody be aware of this when you're creating the remove field operation. That we could slow the person down and say, like, is this really what you want to do? That that that really stuck out to me. But yes. So We have the PR

now All right. Yeah. So this one is a pre I think Devil's Autumn is also a Django Not space person. I believe they re-based it on or based this on this other PR from Marcus.

Sage

From 2016.

Tim

Yeah.

Sage

and the ticket is even older.

Speaker 1

Nine years.

Tim

Yeah. All right. Do either of you have a recommendation on how we want to proceed with this? So it looks like there's been a fair amount of review on this already. I haven't looked at it that thoroughly, but also I trust these five people quite a bit. Did either of you look through the code ahead of time?

12:38 Test Strategy and Database Differences

Sage

Only like I only skimmed it for a bit. Yeah, I think there are different ways. You can approach a PR, you can just straight into jump straight into the code or like either look at the implementation or sometimes it's also useful to look at the test first to because sometimes the test describes what is expected, right? And it also demonstrates the issue which ideally the test should fail before the fix is implemented. So that means we can prove that there's this issue that you can see by running the test

and after the patch is added. the test will pass. So yeah. You can either look at the implementation or in some cases it's useful to look at the tests so that you know where to start.

Tim

That's a good idea. I like that. Rafaela, any things that you wanna check out, review, make sure we touch on?

Speaker 1

I think I noticed even because I also saw the previous PR that it was open but close at a certain point and I think I noticed that my sequel is populated with the and not raise an error that it you probably will expect in this kind of operation I think.

Tim

Okay.

Speaker 1

Inside the test.

Tim

Yeah. I am curious. I remember when I was looking through so check SQL. Is that roughly right? Check the SQL to operation.

Speaker 1

Test operation.

Tim

The test. Okay.

Speaker 1

Test remove fields and unnullable

Sage

Yeah, and I think that issue was specific to MySQL. So yeah, you might encounter cases where a different behavior is observed on certain database backends. And in this case on MySQL, according to the comments here, MySQL populates not null columns with implicit defaults, like I'm assuming, for example, if you have a text field or a chart character field with without a default and you add a column with not null

constraint instead of failing MySQL will just populate the column with like empty strings for example

Tim

That's interesting.

Sage

Yeah, so it's also very useful to have the I think it's Docker Django developed.

Tim

Yeah, I don't trust Linux to run Docker and Zoom at the same time for me. Yeah. That's that's gonna crash immediately.

Sage

Yeah. I'm not saying that you should run it right now, but yeah, if you if you're trying things out on your own, it's very useful to use that as the starting point. Because It has all the databases set up for you so that you can run the tests with different database backends. So yeah, if you write a test and then you run it with one database backend, sometimes it would just fail on some others. Although yeah if it's not a feature that's like directly related to the databases, it's usually The same.

So just running it on a single database. is fine and you can also just let the Django CI do the work. But obviously in cases where you actually implement a feature that works differently on different databases, you would want to run the test locally as well.

Tim

Yeah. So yeah, the UR, it's Django slash Django dash Docker dash box. We'll put it in the show notes. I'll add it to the episode notes as well. Yeah. But I find I'm a little surprised that That there's a check for the vendor here? Because what you were explaining kind of sounded like this isn't going to err. So this wouldn't have It would never raise the integrity error here. Oh, never mind. This is a test. I'm sorry.

Sage

Yeah.

Tim

Okay. Yeah. I thought this was in okay. Yep. My mistake. This makes a lot more sense.

Sage

Yeah, but that that's a good point though because sometimes you also don't want to specifically check for the vendor here because Django also supports custom database backends, which are not in the Django package itself, and it can be implemented as a separate package. If you create such database backends, you also want to run the test like the whole Django test suite with your database backend. And if we have like spatial special cases like this inside the test itself and

If you have if you're writing a database backend that also has the same behavior as MySQL here. Since we hard code the vendor here, it's not going to pass like the test will fail right here because Yeah, it's it's hard coded in the in the test. And in some cases, Django avoids this by having what's called the database features flag.

Tim

So

Sage

Inside your database backend code we have a list not a list well We have a bunch of attributes on the database features class. I think it's in features. py Yeah, that one. Yeah, so these are like the default flags for all database backends. And if you build your own database backends, you can subclass this database features and set the flags appropriately. And then you can use this flag to check in the test instead of checking specifically for the vendors.

But what gets a flag is not ever not everything gets a flag here because there's a lot of different behaviors and just quirks, I guess, if each database back and that it it's not worth it if you have all of them as flags in here. So in some cases, there's also I think if you scroll down a bit more oh if you scroll up just a bit yeah there's Django test expected failures and also Django test skips that you can use to point to the specific tests that you would want to skip or expect it to fail.

for your database backend so that you don't have to batch Django itself to skip the tests

Tim

okay wow I had no idea that existed Yeah. Okay. I was gonna say, is there a reason like should it using like a decorator on this of

Sage

Yeah, normally you would check like connection dot features dot the f feature flag name.

Tim

Oh I meant more I've seen it in the past where there's a decorator here saying skip if.

Sage

Right. Oh yeah. So Django also has skip unless DB feature. And also skip if db feature, which I think is just a wrapper around the built-in unit test, skip if, and skip unless.

Tim

That's the test for it. Okay.

Sage

Yeah. You might need to I think if you look for it using camel case instead of snake case.

Yeah.

Tim

Thank you.

Sage

Yeah.

Tim

Okay.

Sage

So yeah, it's it's a handy wrapper around the built-in skip if and skip unless. that automatically uses like the database features. And so you just need to pass the string of the feature flag to the decorator.

Tim

So there's nothing Okay. Okay. For some reason I was thinking like it would be possible if we're gonna do a check on the vendor. Like this, there would be a decorator that you know annotates the test method and would say like valid vendors

Sage

I don't think Django has a specific decorator for that, but I think it should be Fairly simple to just use the built-in unit test skip if. So for example, you would do at skip if and then you put the condition in there, which is like connection. fender Yeah. Equals MySQL, for example.

Tim

Okay. All right. Well, we've talked about that quite a bit here, which is a very small part of the PR.

Sage

Exactly.

Tim

Cool. Alright. Do we wanna take some time and like five, ten minutes and read through the code? Do we wanna dive into like running the tests? What are both of you thinking?

24:11 Reproducing the Issue and Testing the Fix

Sage

I think I saw you have a local like small project to demonstrate the issue. Maybe it's easier to do that. And yeah, so we want to make sure that the issue is still there and not like because in some cases the issue might be fixed somewhere in some other VR and it's just the ticket hasn't been updated. Okay.

Tim

Anything we should do before that, Rafaela?

Speaker 1

Before starting the project the example that you have in mind?

Tim

To show I think we're Sage just suggesting is we go through the problem case and kind of show what the problem is in Django that this ticket was created for and then We can from there just, you know, pull the latest from the PR and confirm like, all right, did this work in the most simple of cases?

Speaker 1

Yeah, I think also the inside the PR just made some example of but I think it's more it's more useful if we can see the difference between

Tim

Sounds good. All right. Okay, so I have this little example app. I use it for a bunch of other things. It's actually a little bit based on Django models. I don't know if I even have any data. It should be all caught up. So I want to add Okay, so we I'm gonna add this index field to question.

I think I just yeah. So this is on the ad side, which is fine. Let's go ahead and ten. And so yeah, as a foreshadowing, this is what the one of those comments was from I think forget who. They were talking about this preserve default. So we've added that. I don't know if I have shell plus installed. I do, which is from Jang Django dash extensions, and then I have

iPython, which makes things a lot easier. It automatically imports stuff. And so I did it on question. I've already forgotten what fields are on this. Text update index Okay. So now we should have objects.

Of course. Cool. We have an object, we have a row there. So that should be enough. And we said we were gonna remove the field. So we have now removed field And now the case that they were talking about is if we try to go back to zero four. It should break. Yes, there we go. And so I'm running SQLite with this for if anyone was wondering. I But yeah, so it failed here on not null constraint

failed because we have that row that I just that I originally created up here. That's actually I'm curious what that sequel is. Yeah. I always forget SQL has to drop the whole table and like recreate. Yeah. So it's recreating this index. is not null and then when it inserts yeah it's literally trying to insert null into it which would explain why it fails

Right, so what is the best way to go backwards from this? Suppose maybe we can do is delete the database.

Sage

That works.

Tim

Right. So now we want to use okay. I'm I'm just skipping a few steps. So now we want to use the code from the PR. in my local project to run through that same use case and see what is different. And so to do that Oh, it's up right there. I just clicked on something. So GitHub provides this nice little GitHub CLI. And there's other ways you can do it with Git. We're not going to go through that, but if you're curious that that you can do some research on Git. But for now, we'll run GitHub PR checkout. And so it switched us to this branch. It should be from Devil's Autumn.

So we're on the right thing. So now this Django folder for me has the latest code and we can install That package with pip with the dash e. I don't know what dash e actually does. I believe it means editable.

Sage

It does, yeah. Okay. So if you don't use Dash E, if you make further changes to the local cop copy of Django, like for example, if you switch between branches, you'll have to reinstall it. That if you have dash e it will automatically be updated.

Tim

Okay. See, I've never even tried installing a local instance of a package without dash e like this. So I I've never Ran into that, I guess. But okay, so now we've installed the dev version. We're gonna have to undo some of these. Or no, I can migrate to four.

There's all the other Django we'll see if this breaks. I think it might. Oh, nope. Okay. It's complaining because I removed the field.

Sage

Oh yeah.

Tim

Okay. So now should say we have it in our database. So now we're back to where we were before. Now if we remove the fields, we're gonna use the same migration. Oh no, that's what we need to test. It's making that migration. There's our nice little message. Anyone have a suggestion on which one we use? Let's go with one.

Micrate. Okay. So it's still creating that column is not null, but this time instead of null, it's going to insert 15, which should do everything, right? Oh, yeah, all those other migrates were from Django, but the one we care about is right here. And so now if we migrate backwards again. So now we'll have the field. Sorry it got off re-add the field to the model.

So we've migrated backwards to where the question now has index back on it. And so we should see 15 as the value for index. And I believe

Sage

What was the original value before we dropped

Tim

Five. Yeah.

Sage

Five, right.

34:31 Clarifying the Migration Prompt

Tim

So yeah. So I shell plus and i python, like it provides like the history. And so yeah, that's I forgot to. That's why I was immediately looking that up. Cool. So that piece looks like it works, which is really cool. I wonder I want to look at that message again. So now I'm I'm trying to put myself in the shoes of a new developer. Like say you're just using Django for the first time. You don't really understand the relationship between models, migrations, and your database, like how they're all three moving parts. Is this going to be confusing? Because like it's not talking about your forward migration.

It's talking about the backwards migration. Not recommended to remove it. Not in the I'm creating backwards. I don't know.

Sage

This is very trivial, but I notice a typo there. After the dot after the first sentence, there's no space.

Tim

Good catch. Right. Notes. We can show the GitHub suggestion feature.

Speaker 1

I think I default.

Tim

Rafael?

Speaker 1

I saw inside the questioner that it's not I think it should work I don't know why In the because I saw the mm The ask not null removal that the this message come from and there's a There's a pond dot and then there's okay, okay, okay, okay. I noticed that it's not

slash N for going in another for like for making the space

Tim

I'm not sure I'm file. Should I be looking at the code base?

Sage

Yeah, I think Yeah, I think we can look at the typo in the code later after but yeah at least we've we've made a note for that so yeah So I guess right now we are trying to assess the clarity of the message for Newcomers, especially?

Tim

Yeah. Yeah, that's that that was something that crossed my mind of that might be a little confusing. I don't know if you can make this that much better. Remove a not nullable non-nullable field. I mean arguably this should be the to remove the non-nullable field. Unless this has a

Sage

It might be based on the message for the other one. I forgot what that one was.

Tim

Yeah, it's a minor thing. Because something needs to populate the existing roles when migrating backwards. Yeah, I wonder if like it's worth trying to s like it's alluding to the fact that When you remigrate backwards, you would be adding the field back. And that field needs to have some sort of.. say I wonder if it's better for us to call that out explicitly of saying like rather than implying like hey the remove field when you migrate it backwards is gonna add a field

Sage

Yeah, because I think especially for newcomers they might not know that you can even run migrations backwards. Like yeah, so what does migrating backwards mean? It may not be clear. So yeah, I get what you mean.

Tim

I don't know if it's worth making the change or like suggesting it. Like I don't know how verbose we should be here. Because I think like once you see this once and you understand it, like you're only I only ever look at like what these messages are. I don't read this anymore. Yeah. So I'm gonna put a note. We can come back to that.

Sage

Maybe it can be as simple as adding like the phrase. So for example, this is just an example. This is because the database needs something to populate existing rows when migrating backwards, which will add the field back. 2D model or something like that.

Tim

Yeah, I think we would probably want to have I agree that is probably as simple as it could be or as it needs to be, but I would probably put as a separate sentence. of like when migrating backwards, period. When you migrate backwards, it's going to re add the field. Hey, Shafia's here. Yeah, I think that would probably be what did you say? So I agree.

Sage

I think we're talking in terms of models and fields, so maybe we add it to the model.

Tim

Is that but then yeah we the if you

Sage

I'm not sure. Yeah, but yeah, it's it's just nitpicking.

Tim

I'd actually feel kind of strongly about not using the word model there. But yeah.

Sage

Yeah.

Tim

I don't know. We'll we'll see what happens when we get closer to it later. Okay. That that satisfied my need to be worried about that aspect. Okay. So now we know we've confirmed that this works here. I think the other note we had was to force the test to fail to make sure the test is actually doing something. And then checking the SQL on the test. So should we move on to those? Is there anything else we want to test in the application, the CLI?

Sage

Well, I mean you can also test each of those options like what happens if you choose number two? But

Tim

that is true. I'm hoping that the tests will have like the unit test would have that.

Sage

Oh yeah. That's that's also a good point. Yeah.

42:20 Reviewing CLI and Operation Tests

Tim

I suppose we can review that for completeness. We said This is gonna be difficult. I think I'm gonna try going full screen. There we go. We're not gonna have the notes for now So there are hm okay. So test commands. I just want to see is it worth switching out of this view?

Sage

I think it's the cogwheel icon at the top in the middle. In the in the sticky header, yeah.

Tim

Thank you. Yeah, okay. This might be a little bit easier. Okay, so test migrations, not null field

Sage

Yeah, you can see there's no space after the period, and this is using the I'm not sure what the name is, but I tend to call it implicit string concatenation, where if you have a parenthesis and then you have a bunch of strings in there without having a comma in between the strings it will be automatically concatenated but yeah in here you don't have the space after the first sentence so that's why we have the typo

Tim

So the thing I wanted to show where is it?

Sage

It's the first button, I think. Yep.

Tim

Add a s no, this is the one I want. So add a suggestion here. I like I like this in GitHub because you hit this, it brings in the line, and then if we add the space, it does the diff for you. And then the person Say I don't well I'm not gonna have the ability to merge this, but like what it would look like then the author side is you'd have the ability to like batch all the suggestions up and commit them in one go, which is I think is really helpful on code bases where like there are going to be some nitpicky comments. Like this one doesn't re deserve anything besides like the subject. I don't think we need to comment anything besides You know, showing the suggestion of

a missing space. Okay. I'm just gonna add that. Okay, so I think is this test checking the messaging?

Sage

Yeah, it captures the standard output.

Tim

Okay. So I don't know if this would do arc which is good. Like this t tests the CLI. I don't think this tests the actual functionality, like what you were saying earlier, Sage, of what happens when you type in one, two, or three and making sure like the full integration.

Sage

I think that will be in test operations. Okay.

Tim

Okay. That's not removed.

Sage

Yeah, I think it's also interesting to see because now you can also infer the structure of the Django code base, which means that the CLI part is located in a separate part of the code base and it's tested in different ways. So for the for testing the CLI that's the only thing you do you just test the what it prints and what happens if you input one of the options and for the actual operations you test it you have a separate unit test that doesn't involve the a C L I at all.

Tim

I think that makes sense. Right? Rafael, any

Speaker 1

I would like to ask you Sage for So basically we have two type of texts, one with that involve CR the CI and one who don't. Am I correct? Correct.

Sage

So one tests the CLI interface that pops up when you try to run make migrations where it asks you the question. And it doesn't actually check, like for example, the SQL that's generated and also whether it will raise an integrity error, for example. That part is not tested in the test commands that we just saw. And that yeah, we have a separate test for the migration operation itself. If that makes sense.

I guess it's it will be clearer once we start reading the test for the operations.

48:01 Backend-Specific Operation Tests

Tim

Yeah. So I was waiting to see if there was a follow-up question to that. Okay. So excellent. There is only one method here. I just wanted to see if it was It's not exactly easier to read there. Cool. Okay, so this is the test operation. Okay. Project state. I'm gonna ignore that for now. Create an operation model. Must create the data. So like this is when we did our question with index equals five.

And which field were removing? Wait, okay. That's funny.

Sage

Nice one.

Tim

Yep. All right, so migrate it forwards, which would remove the weight. And then this is what you were talking about, what we talked about earlier. For MySQL, it's gonna ignore Trying to confirm that it's gonna air. So this is so is this effectively testing that databases do error?

Sage

Yes, on databases other than MySQL.

Tim

Okay. That's interesting. It's like it we are effectively if one of those databases were to adopt MySQL 's approach of having an implicit default instead of raising an error, we would then know about that in our main Django would know about that because main would start breaking.

Sage

Yep.

Tim

Okay. Interesting.

Speaker 1

So Sage probably in this case it It's we are we maybe could use the decorator that we talked about or instead of checking for vendor.

Sage

I think if we want to use the decorator, we'll need to have a database feature flag on the database feature class that we discussed earlier. Yeah, I'm not sure like the decision on what exactly gets added, like whether this justifies a feature flag is something that's Usually I I'm I might be wrong here, but I think it's made on a case by case basis. And Yeah, if somebody has a particular concern, they would usually raise that during review, like whether this justifies a database feature flag or not.

and the other reviewers will chime in and add their opinions and if everybody well if it's agreed that it should have a database feature flag, then it will be added. Otherwise, you might see this specific vendor checks in the tests.

Tim

So is there a reason not for us not to adopt something like this where we're doing the check for Psycho PG three versus two?

Sage

Right. The only reason might be might just be that you'll have to write a separate test method for that. So you might have to duplicate the entire test and then remove the assert races.

Tim

Well, okay, so hold on, let me skip if

Sage

connection dot vendor.

Tim

Right. Well, so I mean if we had a property here of So that would Oh, I guess we don't want to skip the entire test. You're right.

Sage

Yeah, so

Tim

it's just that one part.

Sage

Yeah. So you'll have to duplicate it or like structure it in a way that it's tested separately.

Tim

Which I don't know if that's really

Sage

It's it's not a problem. Like it's it's perfectly okay to duplicate it, I think.

Tim

well yeah, especially because like this part is doing This is like a canary for how do databases work. This isn't evalu I don't think this is evaluating anything with this change. It's just to see how do databases work in the future and do we need to make a change. So like should that be a separate test method? And I don't know enough about Django to know

Sage

Right, so I think one way to see it is this way. Let's let's see if we imagine removing the if check so that we only have the assert races. Yeah, so this is the behavior that we ideally want across all databases. So if you try to run it backwards, it's going to erase an integrity error But yes, so like I in an ideal world, this is just how the test is.

Tim

Right, but I think my arg or my question is like this is Do tests are tests supposed to be written where they serve one purpose? Because this is testing two things. One, it's testing the operation itself. Oh, I guess not. Okay, never mind. I'm getting confused here. I've

Sage

So without the default, it will raise an error. And then a after you add the default, it doesn't. But yeah, since on MySQL, even if you don't specify the default, it doesn't raise an error. That's why we had the check. But I think One potential issue with this is that if MySQL in the future erases an error, Django will not know about it. Which is not necessarily a problem. It's just that we have this check that

end up not being used and actually is harmful if you now expect it to erase an error because yeah Django just ignores MySQL entirely when checking the integrity error.

Tim

Sorry, there was somebody joining.

Sage

I think I'm not sure if it's possible, but a possibly better way to do this is to check that MySQL actually uses a an implicit default and maybe have that as a feature flag like for example uses implicit default for refers or some sort of flag right and then in the test you actually do check that the inserted

Column like includes that implicit default rather than just skipping this case specifically

Tim

Yeah. third party packages with database backend, implementing a database backend, which is the one that I can think of as Adam Johnson 's MySQL one. Or is that MySQL?

Sage

Because MySQL is built in and the Django MySQL package, it doesn't add the database backend, it just adds like f features that are specific to MySQL, sort of like Django Contrip, Postgres. Was

Tim

Ryan. I was thinking of MySQL Jenkins. But yeah, I also said Adam Johnson and I've written yeah. Anyway, yeah. I thank you for the clarification on Adam Johnson's package. So I if we're trying to make that that experience better, if you the I'm trying to st think of the case where If someone else has a if they're implementing this for a vendor where this would run because they don't have MySQL 's connection. vendor, this rate does not raise the exception. Is it possible to catch that and ignore it or like how would what is that

Sage

Yeah, that's that's a good point. So what happens is that the test will fail because it doesn't raise this the exception and we are asserting that it actually erases. So it's unfortunate But yeah, so they will have to explicitly skip this test or mark it as expected failure, which is not ideal. So Yeah, either we break this down into separate cases. For example with the default and without the default and then so that at least you can break it down into two separate methods. But Yeah, I think ideally

you would have a feature flag here. But usually if it's like a very specific quirk We don't add it until someone makes a case for it. Like they submit a PR. Hey, can you make this into a feature flag, please?

Tim

Okay. Yeah, it doesn't seem like it's used a lot, but it is used. So I think Yeah. Okay. Anyway, so I'm I'm okay with not mentioning this. I think I would really like to have this in main and rather than getting it bogged down on this particular topic.

Sage

I think well I don't know if there has already been conversations on GitHub about it.

Tim

No, it doesn't I think we missed part of that code earlier. I suppose we could check the other PR see if Did this handle

Speaker 1

I think they yeah, they use the same approach

Tim

Yeah. Yeah, I think I'm leaning with wait for somebody to implement it. They can ask a question. It doesn't seem like there's a lot of cases like I don't think this is something Django needs to start fixing or like start addressing. Oh. Oh right, that's yeah. Okay.

Sage

Yeah, it is quite we have quite a few places where we do that check, so it's not uncommon.

Tim

Okay. Good old Oracle.

Sage

Yeah, if you if you count how many vendor-specific checks, Oracle might be the winner.

1:01:07 Interactive Test Coverage

Tim

Fun, yes. Okay, so we have operation default and then it confirms this is what we did earlier of confirming that it was 15 coming back, which is good. So if all those tests actually pass, that's a win. Yeah. Does this Where are we? You said not operations. Oh, this is the wrong PR. Getting lost. Is there a what is test loader? No, I don't think there actually is a test for so Sage

earlier you pointed out Do we need to test something for each one of these cases? That it translates into the right thing. But I wonder if that's part of the questioner Ask not null. Yeah, where's the test for ask not null removal? Is this I might be missing something.

Sage

So this can we have a look at task commands again? So that's so yeah, it tests that with option three, it raises system exit, which means it quits. And with option one, you provide the default and Yeah, it doesn't test the second one, I think. Oh wait. Is it w what about yeah, what never mind. Yeah, I don't think it tests the second one

You would need another mock dot patch for built-in's input with return value of two.

Tim

I wonder if there's Is there another test that does this? That does something similar. Interactive not null addition, not null removable. No, I don't think there is. Yeah.

Sage

I mean, I guess we can see how it t how the test for the adding a new field with without Sorry, adding a new non-nullable field is which should be a few lines above this. I think it's

Tim

this little

Sage

bit.

Tim

So two quits, one provide defaults.

Sage

And those are the only options. So yeah, ideally we should also have Assert some assertions for when you choose the second option for this one. Yeah.

Tim

So no, there is another one that so this is the not null alteration. Three one. Okay, so this is what it's mocked at or modeled after.

Sage

Yeah. I'm not sure if it's possible to test. I think output don't get value.

Tim

Because then It should be a successful exit from the command.

Sage

Yeah And I'm not sure if there's a way to capture the so if you look at the questioner. py file Oh I forget the yeah I think it's yeah. So I think for the second option yeah it just returns the

Tim

Not provided.

Sage

Not provided. I'm not sure if it's possible to check that value is. I I'm not sure if you have access to that value from the test.

Tim

It does seem like we need to have a test here though in questioner tests. So like we have one test asked, not null, not provided. So I think that's what we're missing is this test.

Sage

yeah, that's exactly it. Yeah.

Tim

Okay. So let's add that to the notes.

Speaker 1

Can I ask you something?

Sage

Yep.

Speaker 1

Inside the test command, there's a comment that says no message appears if try run

Sage

Right. Yeah, so if you run When you run the make migrations command, you can have there's the dry run option and I think it doesn't have any output if you use it. Like it I mean it's not going to ask you what to do, I think So yeah, we can try that maybe.

Tim

I wanted to show what the description was first, which just show what migrations would be made, don't actually write them. So Wait. Oh. Interesting. Okay.

Sage

Yeah, so that's going to be the same behavior as the previous one where So it's like the option two where it would just proceed with making the migration, but in this case the migration file will not actually be created.

Tim

Are either of you familiar with use cases where dry run is common, like commonly used? Like I

Sage

I think a common use case is to run it on CI to make sure that any model changes already have their corresponding migrations created.

Tim

Minecraft.

Sage

So yeah, if you if you change the model without creating the migrations. That's bad because yeah your migrations do not reflect the final state of your model and you would want to detect that on your CI so you can run the dry run migrations on CI.

Tim

Okay. That makes sense. Anything else down that avenue, Rafaela?

Speaker 1

sorry?

Tim

Anything else on that topic that you wanted to ask or discuss?

Speaker 1

No, I was wondering because the I think This is the test about the questionnaire. And then we just see that the option three is tested, option one is tested, but then I notice this type So I was curious about this.

Tim

Gotcha. Yeah, so I because we just found like, hey, this method isn't tested, I'm now curious, like what are the other changes doing? So this deconstruct method on so this is the remove field. Not provided. Okay. So I'm going to imagine this is very similar to oh this is the field operation. Very similar to the add field operation of default not provided.

I do remember seeing this in test operations at the very end. It was confirming the deconstruct. Yeah, it was testing if the deconstruct works. I find it interesting sometimes of how the I never know how to write my tests of should I have a test for each single evaluation or should I combine the tests? Like so like in this case It's testing that the database like it actually the database actually macro migrates backwards and like is there an error or not? We test whether the default works and then it's also testing deconstruct and like this deconstruct seems a little out of place. But also there's probably

tens of thousands of tests in Django. Like, do we want to add another one? I

Sage

Yeah, I think one of the other considerations is that to check the deconstruct, you will need to do the same setup like

Tim

oh

Sage

and it's yeah it's just not worth it sometimes

Tim

that makes sense okay that answers my question then of yeah, no there's no read to no reason to run the same test setup, but

Sage

okay.

Tim

Okay. This is backwards. So this is what we were testing earlier. I remember seeing a question from Sarah Boyce on how does database default work with this? And I don't Where is that? Oh, maybe it was oh apparently it was Lily.

Sage

I think there's another thread somewhere I forgot.

Tim

see if we can find

Sage

further down below, I think. Did you know that if you press Alt click on the show resolved, Yeah, if you press Yeah, it's going to open every single thread.

Tim

I did not know that.

Sage

Yeah, so it's possible to control F from that point.

Tim

So this was brought up Is this a marked I don't think this one's marked as resolved. So I think they Okay. I'm just trying to figure out like now with expanding all of them, which are the relevant conversations, which ones aren't No, so that was marked resolved. So there should be a test.

Sage

I think there's another one. There's another thread down below. I'm not sure why it's not I can see it on my I think it's the first review from Lily.

Tim

This one?

Sage

Yeah. Yeah, exactly. Yeah, yeah. That

Tim

re-adds database defaults. Can we? I think we might want to go check to see how adding a database default works. Like how do we test that? Like do we actually inspect I mean

Sage

I think

Tim

I guess it would be part of the remove field, wouldn't it? That wouldn't be part of this.

Sage

Yeah. Yeah, it well I think so the way you would add a db default is at the point of the add field operation right and then when you do a remove field it should look at the dbdefault before the removal happens and make sure that it's actually used And I think it might already be handled because hm Well, it's just going to reuse the previous definition.

Tim

Yeah. Right. Yeah, this has that that shouldn't be related that shouldn't be related to this PR. I think when they added support for db dot default, that's when you would need to cover remote field. I wonder if you Would you ever have a default and a different DB default? I mean not logically you shouldn't, but Do we break on that?

Sage

That's a good question. So

Tim

now I've

Sage

I'm not sure if Django allows you to do that.

Tim

Yeah. SQL Lite support Db default

Sage

Oh, so I think I'm currently evading the documentation. So you can have both db default and field default. And the plain default will take precedence when you create instances in Python code, but db default will still be set at the database level or when you add for things like when you add a new field in a migration.

Tim

Oh shoot, I did this backwards. Please no one take any notes on how to manage your database through this example. Not what you're supposed to do. So now we've added Okay, it didn't give us the warning because we have the default set, which is cool. That's something we did not test. Adding okay. That all makes sense.

Default ten at all. Okay That is kind of interesting. It is all right. So it's using 10. What did we So default was zero, db default is 10. So it's going to I don't know if that really matters to us, but it looks like it's going to be using the database defaults for the migration.

Sage

Yeah, because the default the old default, not the DB one, it's never reflected in the SQL statement as the default clause It previously was just computed in the application level, like on the Django level, and by the time it gets to the SQL query, it's just the value, for example, zero or ten. Yeah

Tim

I suppose if you wanted to over I'm just thinking like eventually someone's gonna be like, no, I don't want the db default, I want the default Can you just set it that way? No. I feel like you should. Right? Maybe it's because it's falsely. No that's interesting.

Sage

I think it's it's consistent with the documentation that says the dp default will still be set of and it will be used for inserting rows or like when adding a new field in a migration because technically when you reverse a remove field that's going to be an add field. So yeah, the plain default is only It only takes precedence when you create instances in the Python code, but when it comes to migrations or if you somehow insert

VRO outside of the ORM. It's still going to be used. Yeah, I'm I'm not entirely familiar with the DB defaults. So I might be wrong.

Tim

We have this wrong. It's it Okay. Alright. Alright. I s I think I'm getting a better understanding of it. I think in the case of someone did want to override this, it's probably gonna be Oof, yeah, that would be weird. Like maybe a separate database and like the separate model state and database. Yeah, that would not be fun. Okay. Anyway, that's a very

Sage

What they could do is update the previous def migrations where the DV default is Add it change the value from there, I suppose. Or yeah, use the separate database and state maybe. I don't know.

Tim

Okay. Anyway. Okay, so we checked that auto detector. Do we need an explicit test for this? Do we have one? I don't think I don't think we have a test explicitly for this, but I don't know if we need one.

Sage

Can we have a look at how the test operation is set up again? Okay.

Tim

So it's going through the happy flow.

Sage

Yeah.

Tim

Not we're not test excuse me. Evaluating all the cases of this if statement. I was wondering if maybe somebody else on this call or on the video in the chat wants to comment in, but we'll see.

Sage

So there is a test auto-detector file, but I don't see it in the PR, so I'm not sure if There should be a test there. This is a very long file, I think.

Tim

Yeah, probably not worth going. So I think The other way to go about is could we find a similar evaluation or similar code and see if it's tested extremely thoroughly or not? Maybe this one instead? I don't know. I'm just guessing at Yeah, that's what we just added. Well, that's the only one that does that.

Okay, so there's some logic here. Added field. I think we'll get another one. My brain's turning to mush.

Sage

Yeah, I'm currently looking at the same code as well, trying to find whether yeah that might be it. Yeah, so I found one second. Test add not null field with

yes, this one.

Tim

Yeah, this looks doesn't look like it's covering every single possibility. So I think we're good. I think the PR is fine, in my opinion. I don't what do you each of you think?

Sage

I would probably have a look at the previews. Like how we initially added for example like adding a field without the default with a null equals false and like whether that has a test in the autodetector. Because yeah, even though we can test it manually, but it would be nice to actually have the test in the auto detector because that's That's a crucial part. That's what links the

test commands, the bit that tested the command line and the bit that tested the operation. because both can still pass if the auto detector didn't work, I think.

Tim

This might be a rough Code change to look through part. So you don't so that's pretty clear. And so this is the PR where database defaults was added. And yeah, it was one test.

I'm not sure if that answers your question, Sage. These two tests from when the

Sage

So I guess what we you can look for, for example, in the test auto-detector, you can look for mentions of the questioner. Yeah, so that would be testing the cases where it would normally very like run the questioner and yeah I think it would take some time but I would go through all these existing tests and then think how essential it is like to have a similar test for

this PR.

Tim

All right. I think we're probably I don't know it how much longer both of you want to go looking at this.

Sage

I do need to take a five minute break in sometime in the next 30 minutes if possible.

Tim

I was actually suggesting maybe we wrap things up now. That

Sage

that works for me.

Tim

Rafael, any last minute questions?

Speaker 1

I think we just cover a lot of with this PR.

Tim

Yeah. Okay. I agree. That that was a really deep dive into the code. I don't think we've gone Looked at the code that much in the past. Or definitely I've never ran this that many things in the terminal for one of these before. Okay, so the notes that we have So we added that one, so that's done. Not deleted. Right. The note about migrating backwards, if we want to try to be a little bit more clear, because that is a potentially advanced topic or like it's not a clear topic at least for beginners

Adding a test for That's similar to ask not null alteration not provided. I think that's probably a requirement. And then we have some extra homework to do yeah. Anything else that we missed?

Sage

I don't think so.

Speaker 1

Maybe just with the dogs, Yeah. But

Sage

oh yeah, that's a good point. Yeah, maybe we can quickly look at the docs preview maybe.

Tim

Okay. Do scroll up

Sage

I think if you control F or read the docs without spaces I had it open. One second, let me

Tim

Yes, please help.

Sage

Oh, where is it?

Tim

I do think the docs change are fairly minimal. Yeah.

Sage

Okay, so this is the build. But I haven't got the exact page up. Should be in reference

Tim

Yeah, it's missing this is this is the old version. Yeah, why didn't this

Oh, let's try that. Change it to the right PR. There we go.

Sage

Yeah, nice It looks different to the actual docs though, because this is the one that's hosted on Read the Doc the Docs.

Tim

Yeah.

Sage

Makes sense to me.

Tim

I think it makes it I think this might be two.

Sage

Two, yeah. That's that's a good point, yeah. Well, I guess it's because there's always already a two before that. So two populate and make.

Tim

Yep, you're right. Raphael, any thoughts on this?

Speaker 1

I try to read room once or twice, so I think it's probably clear. I don't know if it's clear because I know what is going on or because it but i think it's also easy to alter it.

Tim

Okay.

Sage

Yeah, I think the first sentence saying bear in mind having that in the documentation I think justifies having more explanation in the message.

Tim

Because

Sage

it's something that's that may not be immediately clear.

Tim

Yeah, I wonder if this actually like Rather than having the first part of the sentence be bear in mind that when, like should this be when reversing this mind when reversing a remove field operation, this is actually adding a field to the model where like it might be easier to pick out visually if you're skimming it

Speaker 1

Okay, we are also reviewed because I saw in the review just last pre three row that it was updated because the first the first sentence is it was from it was just a bill already there.

Sage

Oh, yeah.

Tim

Adding so Ryan says adding a field back to the model Yeah. This is also like weird. When reversed, it's not doing anything to the model, it's doing it to the table. And like even down here it says recreated column. So should this be actually This is actually adding a column back to the table.

Sage

I think if we want to evaluate the field versus column terminology I'll I would have to read through the other operations docs to make sure it's consistent. Maybe there is no.

Tim

Oh yeah, because we call it remove field, which makes sense. But at the same time, if we're saying what actually happens

Sage

We're either saying about we're we're either talking about what actually happens or what the reverse operation is in terms of operations and models, fields and so on. So yeah, it's documentation is not easy.

Tim

No, no, it is not. I have another comment, another thought. It will ask for a default value to populate. So it will It will ask to provide a default value, so instead it will ask for a default value to populate. Take both of these out.

I do think the latter probably makes a little bit more sense.

Sage

And more concise as well.

Tim

That is from our chat and I do not know how to credit you, but thank you. Oh okay. Sweet. Okay, so ignoring the field and model concept here, I do like Ryan 's opinion of saying adding a field back to the to the model. Do we want to suggest changing this part? I don't I wasn't sure where you both of you landed on starting a sentence like that.

Speaker 1

Yeah, I think the starting of this sentence is strange, but I don't know if there's any. Any other thoughts in mind when he was hard to that? Maybe we can just propose a subtraction. I don't know if this maybe this could also be related with this PR.

Sage

I don't mind changing the first sentence, but I also don't mind leaving it as is. So yeah. But I do agree that I think the fact that the existing documentation has that emphasis on what happens when you reverse the operation. I think that justifies adding more explanation in the common line

Tim

In the command output you mean?

Sage

Yeah.

Tim

Okay. Yeah. That's that's actually a really good argument that yeah. Do you want to leave that comment, Sage?

Sage

Sure, yeah.

Tim

All right, cool. So I'm gonna put you down. Sage will do this. Rafael, do you want to add, since I already added a comment, do you want to add a comment about adding a test and test questionnaire similar to test not ask. So sorry, I'm trying to pull this up.

Speaker 1

Test questioner.

Tim

So the test questioner file has a test here under the questioner tests. And this is for not null alteration not provided, which is what sorry, I'm Where is that? There was no that I mean I'm all over the place right now. Okay, so in questioner there's this not provided choice, which we don't have a test covering that. And so

ask not null removal is very similar to I'm in the wrong file. So like this one is very similar to our not null removal and In test questioner, there's a test for this not provided case that wasn't covered in test command. Did I explain that well enough?

Speaker 1

Nope.

Tim

I saw the smile.

Speaker 1

Okay.

Tim

No, nope, I have not explained this well. Alright, so we have test commands here. Let me We have comments here of like which so it prompts the user for like which input they should enter and like what happens. And so this test covers three and one, but there's nothing for two, which is ignore for now, make this potentially non-reversible, which is the not provided. But that test should exist on test questioner of like this. Like test not is it

I think this is like what we need to add.

Speaker 1

But we have the this test inside the test command just is similar, no, or no?

Tim

No, it well it's similar in that it's testing the other two returns. from like when the user gets prompted. It tests for one and three, but we don't have a test for two. And so we need to add this test explicitly to test questioner to cover that option. And it would match how we do things for when we alter a field that's not nullable, which is this other test method.

Speaker 1

Okay, can I can I read you what I wrote to understand if it's understandable.

Tim

Yes, one second, let me finish this Okay, yeah. If do you wanna put it in the chat?

Speaker 1

Oh yeah. Father. So

Tim

Inside the test questioner. We should write a similar test. I would add inside the test questioner. py file. And we should write a subtest similar to yeah, that method name to cover the cover The

Speaker 1

the case the case from

Tim

Yeah or the case, yeah. I to cover not provided case.

Speaker 1

To cover the not private

Tim

case. Yeah.

Speaker 1

Games where

Tim

That discussion we had earlier of adding a field to the model that that is not even in the changes here. So probably shouldn't be making that suggestion then. That is not yeah, not our place. All right. Check. Got that. All right. I think that's everything. Inside test question, we should write a test similar to

test SNAT and I'll cover. Yeah, I think that's thorough enough that they will understand But that points them in the right direction, points out that hey, we're testing the not provided case. I think they will understand like, hey, these are very two similar paths.

Sage

Yeah, I agree.

Tim

Okay. So then when both of you are ready to submit those comments.

Sage

Oh, we do eat now?

Tim

Yeah, yes.

Sage

Okay.

Tim

So are we both ready? So there's a little thing, Sage, that I don't know if you've seen the end. Of how these space reviewer episodes go, but it

Sage

Oh yeah.

Tim

Yeah. So there's when you push review or submit, you gotta scream. Sorry, everybody else.

Sage

Explicit mention. Oh so that's in Okay.

Speaker 1

Sorry team, this time I just accidentally pushed the comment button before Everyone. I will join you anyway for the for the celebration.

Tim

Yes, celebration. Not nervous at all.

Sage

All right. I'm ready.

Tim

All right. I'm just gonna submit this without approved. All right. Rafael, you want to count us down?

Speaker 1

Three?

Tim

So after one it's a scream.

Speaker 1

Sorry I was I won't understand if I have to come down.

Tim

Yes please.

Speaker 1

Three, two, one

Tim

Well, okay. I think that's everything. We have a couple extra notes that we'll add in. I think Yeah, I don't know. I I'm not sure if we have any anything else to say.

Sage

I just want to say I really enjoyed this. It's been great and you both did a great job. Like especially you, Tim, like trying out all these on a on a actu on an actual project like running things. Yeah it's it's really useful to see the issue and like replicate it and verify that the fix actually works. So yeah thank you for doing that.

Speaker 1

If I if I can I would like to also thank you Sage because you really know where some where we can find something that it could be useful for example the where to find the inside the test or for example the flag if it's needed. It was very I really I really enjoyed it also because you did a great a great suggestion for us.

Sage

Thank you. Yeah

Tim

Yeah, I don't know if we can have the on the slide of we're not all experts. I think yeah you were answering quite a few expert level questions there, Sage. I think yeah

Sage

, oh that's What spending too much time on the code base of TP? But yeah, we'll we'll get there eventually. So

Speaker 1

with your lead

Tim

Yeah. All right. Well thank you everyone. If anyone's watching this in as a contributor to Django and you want to be a navigator for DjangoNot Space, please reach out to contact at DjangoNot. space. We are always looking for more navigators for Django or third-party packages within Django. But other than that, yeah. Take care. We'll see you next time.

Sage

All right. Thank you, everyone. Bye-bye.