Skip to content

Commit

Permalink
updating the database enforcing iban type definition and switching from
Browse files Browse the repository at this point in the history
a one show initial migration to a pre migration check
  • Loading branch information
Chedi committed Jun 24, 2015
1 parent 1d96e43 commit 97df801
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
1 change: 1 addition & 0 deletions django_iban/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = 'django_iban.apps.IBANFieldConfig'
48 changes: 48 additions & 0 deletions django_iban/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from os.path import join
from os.path import dirname
from os.path import abspath
from django.db import connection
from itertools import chain
from django.apps import AppConfig
from django.conf import settings
from django.db.models.signals import pre_migrate

from django_iban.fields import IBANField


class IBANFieldConfig(AppConfig):
name = 'django_iban'
verbose_name = 'Django IBAN Field'

def ready(self):
pre_migrate.connect(self.enforce_database_constraint_callback)

def enforce_database_constraint_callback(self, sender, **kwargs):
database_backend = settings.DATABASES['default']['ENGINE']
enforcing_status = any([
iban_field.enforce_database_constraint
for iban_field in chain.from_iterable([
model._meta.fields for model in sender.get_models()])
if isinstance(iban_field, IBANField)])

if database_backend in (
'django.db.backends.postgresql_psycopg2',
'django.contrib.gis.db.backends.postgis'
) and enforcing_status:
cursor = connection.cursor()
base_path = dirname(abspath(__file__))

cursor.execute("select * from pg_available_extensions where name = 'plpythonu'")
if cursor.rowcount == 1:
cursor.execute(
"""
SELECT p.proname FROM pg_catalog.pg_namespace n JOIN pg_catalog.pg_proc p
ON p.pronamespace = n.oid
WHERE n.nspname = 'public' AND p.proname = 'is_valid_iban';
""")

if cursor.rowcount != 1:
with open(join(base_path, 'pg_iban_validator.sql')) as iban_type_definition:
cursor.execute(iban_type_definition.read())
else:
raise Exception('django_iban requested database enforcing but plpython extension not installed!')
31 changes: 0 additions & 31 deletions django_iban/migrations/0001_initial.py

This file was deleted.

Empty file removed django_iban/migrations/__init__.py
Empty file.

0 comments on commit 97df801

Please sign in to comment.