Skip to content

Commit

Permalink
Merge pull request #228 from lzgirlcat/develop
Browse files Browse the repository at this point in the history
fix postgres_ext.arrayfield params, fix default param edgecase
  • Loading branch information
klen authored Aug 7, 2023
2 parents 46bd29d + 38885bc commit 6c29412
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion peewee_migrate/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)

import peewee as pw
from playhouse import postgres_ext
from playhouse.reflection import Column as VanilaColumn

if TYPE_CHECKING:
Expand Down Expand Up @@ -57,6 +58,17 @@ def dtf_to_params(field: pw.DateTimeField) -> TParams:
return params


def arrayf_to_params(f: postgres_ext.ArrayField):
inner_field: pw.Field = f._ArrayField__field
module = FIELD_MODULES_MAP.get(inner_field.__class__.__name__, "pw")
return {
"field_class": f"{module}.{inner_field.__class__.__name__}",
"field_kwargs": repr(Column(inner_field).get_field_parameters()),
"dimensions": f.dimensions,
"convert_values": f.convert_values
}


FIELD_TO_PARAMS: Dict[Type[pw.Field], Callable[[Any], TParams]] = {
pw.CharField: lambda f: {"max_length": f.max_length},
pw.DecimalField: lambda f: {
Expand All @@ -67,6 +79,7 @@ def dtf_to_params(field: pw.DateTimeField) -> TParams:
},
pw.ForeignKeyField: fk_to_params,
pw.DateTimeField: dtf_to_params,
postgres_ext.ArrayField: arrayf_to_params,
}


Expand Down Expand Up @@ -127,7 +140,11 @@ def get_field_parameters(self, *, change=False) -> TParams:
params["null"] = self.nullable

if self.field.default is not None and not callable(self.field.default):
params["default"] = repr(self.field.db_value(self.field.default))
value = self.field.db_value(self.field.default)
if isinstance(value, pw.WrappedNode):
params["default"] = str(value.node)
else:
params["default"] = repr(value)

return params

Expand Down

0 comments on commit 6c29412

Please sign in to comment.