- February 24, 2009
- sql test "initial
- 2 (after 2 ratings)
A management.py loading customized SQL feeding it raw to the database backend.
Just put it as
management.py in your app and put whatever SQL you want run after syncdb in
backend_driver is skipped the SQL will be loaded no matter database backend.
Since it is run after syncdb it will also be run for test.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import os, sys from django.db.models import signals from django.db import connection, transaction from django.conf import settings def load_customized_sql(app, created_models, verbosity=2, **kwargs): app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) custom_files = [os.path.join(app_dir, "custom.%s.sql" % settings. DATABASE_ENGINE), os.path.join(app_dir, "custom.sql")] for custom_file in custom_files: if os.path.exists(custom_file): print "Loading customized SQL for %s" % app.__name__ fp = open(custom_file, 'U') cursor = connection.cursor() try: cursor.execute(fp.read().decode(settings.FILE_CHARSET)) except Exception, e: sys.stderr.write("Couldn't execute custom SQL for %s" % app. __name__) import traceback traceback.print_exc() transaction.rollback_unless_managed() else: transaction.commit_unless_managed() signals.post_syncdb.connect(load_customized_sql)
More like this
- Serialize a model instance by chriswedgwood 1 week, 6 days ago
- Automatically setup raw_id_fields ForeignKey & OneToOneField by agusmakmun 9 months, 2 weeks ago
- Crispy Form by sourabhsinha396 10 months, 1 week ago
- ReadOnlySelect by mkoistinen 10 months, 2 weeks ago
- Verify events sent to your webhook endpoints by santos22 11 months, 2 weeks ago