-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better compat with Model and Django Migrations #31
base: master
Are you sure you want to change the base?
Conversation
5a74a4e
to
f227d54
Compare
@danni I've fixed all test errors. The following issues remain in makemigrations:
|
This makes the type more compatible with the Model class
This does not need a db connection, nor does it need migrations to already have run. So we can do this early.
Double underscore causes errors in lookups
f227d54
to
635fbe8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very clever, but I keep wondering about the approach. Perhaps before merging this it would be worth raising a ticket in Django to see if they're interested in the feature at all, and if they are, how they would approach making migrations work.
If we do go forward with this approach we'll need to test what it's like to move existing code onto this approach, i.e. will we make a huge mess of people's migration graphs.
def deconstruct(self): | ||
name, path, args, kwargs = super().deconstruct() | ||
|
||
name = path.replace("postgres_composite_types.composite_type.", "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels a bit fragile. Also the shadowing makes it hard to appreciate what will happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. I am not sure how else to do it though. Postgres' migration generation system is poor in terms of customizability.
{ | ||
field.name: field.value_to_string(value) | ||
for field in self._composite_type_model._meta.fields | ||
if field.name != DummyField.name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isinstance
?
fields = { | ||
field.name: field.formfield() | ||
for field in fields or model._meta.fields | ||
if field.name != DummyField.name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isinstance
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember now, but IIRC that didn't work because of the way Django does all this.
sql_create_type(self.Meta.db_type, self.Meta.fields, schema_editor) | ||
) | ||
self.Meta.model.register_composite(schema_editor.connection) | ||
composite_type_created.send( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just because?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I culdn't get this to work at all. I am not sure what benefit it actually brings to the table, though.
This branch is an experiment, and it's currently broken.
I mentioned this in #8:
So this is where I'm at. The two main changes are
db_type
is nowdb_table
(and this is still required; breaking change), and_meta.fields
is now the same structure as for normal fields, which is a list of field instances; their name comes from field.name.I've had to use a custom manager that always returns empty querysets, as the tests surfaced that some django code (such as the db serialization code) will query all models. Types should always be empty for this.
This doesn't solve #8 because something like AlterType hasn't been implemented, but it makes it a lot easier to tackle.
Also, because I replaced the migration operation with one that doesn't rely on the class itself, I have had to drop support for the
composite_type_created
signal. IMO this is not a useful signal anyway - any push back?Currently, I'm fighting with the following test error:
I'm still investigating what's happening here.