Skip to main content

django-db-portability

Catches Django code written for one database that will break when ported to another, and provides runtime helpers for the one divergence static analysis can't catch: Oracle silently treats '' as NULL, PostgreSQL doesn't.

This does not try to make Django fully database-agnostic — Django's ORM already handles the common cases (pagination, joins, sequences). It targets the specific, well-documented gaps that leak through: source-DB-only contrib modules, raw SQL with source-DB-only syntax, and the empty-string/NULL trap.

Checks are organized by (source, target) pair. Only postgres -> oracle is implemented today — see src/db_portability/checks/. Adding another pair (e.g. mysql -> oracle) means writing a sibling module and registering it; dbp-scan's --from/--to picks it up automatically once it exists.

Install

pip install django-db-portability          # lint checks only
pip install "django-db-portability[django]" # + runtime helpers (fields/managers)

1. Static checks

flake8 plugin

Runs automatically once installed — flake8 picks up plugins from flake8.extension entry points. Always runs the postgres -> oracle checks (flake8 plugins have no natural way to expose a --from/--to pair):

flake8 --select=DBP myproject/
Code Flags
DBP001 Postgres-only field (ArrayField, HStoreField, CITextField, range fields, ...)
DBP002 Postgres full-text search (SearchVector, SearchQuery, TrigramSimilarity, ...)
DBP003 Postgres-only aggregate (ArrayAgg, StringAgg, BoolAnd, ...)
DBP004 .extra() — raw SQL fragment, needs manual review
DBP005 Raw SQL (RunSQL, cursor.execute, .raw()) containing Postgres-only syntax (ON CONFLICT, RETURNING, ILIKE, :: casts, ...)
DBP006 CharField/TextField(unique=True, blank=True) without null=True — the NULL/empty-string trap below
DBP007 Other Postgres-only contrib modules (indexes, constraints, operations)
DBP008 .distinct(*fields) — PostgreSQL's DISTINCT ON, unsupported on every other backend
DBP009 CharField without max_length — fine on Postgres, fails Oracle's system check (fields.E120)
DBP010 .annotate() combining an aggregate (Count, Sum, ...) with a Subquery-based annotation — the resulting GROUP BY contains a subquery expression, which Oracle rejects (ORA-22818), even if the queryset is only ever used as the value inside another .update(col=Subquery(...))

DBP0xx is reserved for postgres -> oracle. A future pair gets its own block (DBP1xx, DBP2xx, ...) so codes stay stable as pairs are added.

Add to your CI lint step or setup.cfg:

[flake8]
select = E,F,DBP

dbp-scan — readable terminal output

flake8 --select=DBP prints one flat line per finding, which turns into an unreadable wall of text on a real project. dbp-scan runs the same checks but groups findings by file and colorizes them, and lets you pick the --from/--to pair (postgres -> oracle today):

dbp-scan myproject/                         # postgres -> oracle (default)
dbp-scan --from postgres --to oracle myproject/
dbp-scan --quiet myproject/                 # summary line only
dbp-scan --no-color myproject/ > report.txt

It skips migrations/, .venv, .git, __pycache__, node_modules, .tox, build, and dist by default (--exclude NAME adds more), and exits 1 if any issues were found — same convention as flake8, so it's safe to use as a CI gate too. An unregistered pair (e.g. --from mysql) exits 2 with the list of pairs that are actually implemented.

2. The NULL / empty-string trap

Oracle coerces '' to NULL for VARCHAR2/CLOB columns. PostgreSQL does not. Django's own convention — "never set null=True on CharField" — is exactly what makes the two backends disagree: identical code, identical input, different stored value depending on which DATABASES alias the query hits. It's data-dependent, so it won't show up as a test failure until you have the right data.

Option A — swap the field type

from db_portability.fields import PortableCharField

class Widget(models.Model):
    code = PortableCharField(max_length=20, null=True, blank=True)

PortableCharField/PortableTextField normalize '' → None in Python before the value reaches either database, so both backends store and return the same thing. This requires null=True — that's intentional.

Option B — can't change the field? Query both cases explicitly

from db_portability.managers import empty_or_null_q

Widget.objects.filter(empty_or_null_q("code"))

or use the manager mixin:

from db_portability.managers import PortableManager

class Widget(models.Model):
    code = models.CharField(max_length=20, blank=True)
    objects = PortableManager()

Widget.objects.empty_or_null("code")
Widget.objects.exclude_empty_or_null("code")

Development

python -m venv .venv
.venv\Scripts\pip install -e ".[test]"
.venv\Scripts\pytest

Metadata

Release files for django-db-portability 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-db-portability 0.1.3
File Size Uploaded Provenance Actions
django_db_portability-0.1.3.tar.gz 14.7 kB With provenance Details for django_db_portability-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-db-portability 0.1.3
File Interpreter ABI Platform Actions
django_db_portability-0.1.3-py3-none-any.whl Python 3 none any Details for django_db_portability-0.1.3-py3-none-any.whl

Total release size: 28.2 kB

Release files / django_db_portability-0.1.3.tar.gz

Download URL django_db_portability-0.1.3.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a181846017f094ea3e14bf6d64d9b6342bed6621bf0dd949f8902c35caca6fb8
BLAKE2b-256 checksum
How to use checksums
192e20d0a238ce52ded57c2c1eedae65345765f1447b34d9bc02de41f0366d2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.

Transparency log

Release files / django_db_portability-0.1.3-py3-none-any.whl

Download URL django_db_portability-0.1.3-py3-none-any.whl
Size 13.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4b6c0eaa6a2c7a3c5d55ee19355e176e0222d7b7e097f360eada43d62480934d
BLAKE2b-256 checksum
How to use checksums
ed00450d8fe55d1b0aaecf929ad82c2f94a0d142281273c6a772fa9e7d86d2ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.

Transparency log

Security for django-db-portability 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_db_portability-0.1.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page