- 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
- post_migrate handler to load initial SQL after migrating with south by stingydrone 4 years, 9 months ago
- immitating 'real' post_syncdb signal by jango 5 years, 5 months ago
- Unit Tests That Write Fixtures by justquick 6 years, 8 months ago
- Update ContentTypes and Permissions without syncdb by paltman 7 years, 6 months ago
- Run a testcase with custom INSTALLED_APPS by vdboor 2 years, 11 months ago