django-postgres-extra
django-postgres-extra copied to clipboard
Add support for model tables on different Postgres schemas
Problem
Unable to use the command pgpartition if the model table is using a schema other than public
The model definition is something like this
class MonthTimeseriesLong(PostgresPartitionedModel):
"""
... fields
"""
class PartitioningMeta:
method = PostgresPartitioningMethod.RANGE
key = ["timestamp"]
class Meta:
...
db_table = 'timeseries"."tpa_ts_timeseries_long_month'
...
Migrations are working good and the tables are created in the right schema whern migrate is executed. However, pgpartition command fails after proposed partitions are accepted:
No table is found because the query made inside the method get_partitioned_tables of PostgresIntrospection returns only the table name and the following strings are being compared:
'timeseries"."tpa_ts_timeseries_long_month' == 'tpa_ts_timeseries_long_month'
Solution
Split by "." the table name defined on the model and trim remaining quotes in order to perform the comparison.
Test results
tox
benchmarks
Django doesn't have proper support for database schemas, see: https://code.djangoproject.com/ticket/6148
I am happy to merge limited support for this, but this implementation leaves a lot to be desired:
- Breaks on table names that need escaping/quoting.
- Breaks when the table name or schema name is properly quoted (
'"schema"."table"
). - Breaks when two tables in different schemas have the same name.
- No tests to prevent this from breaking in the future.
You'd have to use something like PostgreSQL parse_ident
to do this properly. There are also other introspection methods that don't support this properly. I don't think just this patch is sufficient to fully make this work.