Skip to content

Instantly share code, notes, and snippets.

@bersace
Created May 12, 2015 12:48
Show Gist options
  • Save bersace/0a433a502182f8eaf805 to your computer and use it in GitHub Desktop.
Save bersace/0a433a502182f8eaf805 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Exécuter avec :
#
# DJANGO_SETTINGS_MODULE= python only.py
#
# Requiert django 1.6 et djqmixin
#
import sys
import imp
from django.conf import settings
APP_NAME = 'web.web'
sys.modules['web'] = imp.new_module('web')
sys.modules[APP_NAME] = sys.modules[__name__]
sys.modules[__name__].__name__ = APP_NAME
settings.configure(
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
},
},
DEBUG=True,
)
from django.db import models, connection
from django.core.management.color import no_style
import djqmixin
try:
import ipdb as pdb
except ImportError:
import pdb
class MyManager(models.Manager):
pass
class MyModel(models.Model):
uuid = models.CharField("UUID", unique=True, max_length=255)
cdate = models.DateTimeField('Creation date')
# CETTE LIGNE PROVOQUE L'ERREUR:
objects = djqmixin.Manager()
def main():
# Création du schéma
sql, _ = connection.creation.sql_create_model(MyModel, no_style(), set())
for statement in sql:
connection.cursor().execute(statement)
# Enregistrement des requêtes
connection.use_debug_cursor = True
list(MyModel.objects.filter(uuid__isnull=False).only('uuid').all())
# Vérification
for qry in connection.queries:
sql = qry['sql'].lower()
if 'select' in sql and ' where ' not in sql:
print >>sys.stderr, sql
raise RuntimeError("Bad query executed")
print >>sys.stderr, (
"No spurious queries in %d query \\o/"
% (len(connection.queries),)
)
try:
main()
except Exception, e:
if 0:
_, _, tb = sys.exc_info()
pdb.post_mortem(tb)
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment