Created
December 26, 2011 07:12
-
-
Save lqez/1520683 to your computer and use it in GitHub Desktop.
dropping tables in django database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import settings | |
fn = 'drop_tables.sql' | |
try: | |
os.remove( fn ) | |
except: | |
pass | |
print 'Preparing sql commands for dropping...' | |
os.system( 'echo "SET FOREIGN_KEY_CHECKS=0;" >> %s' % ( fn ) ) | |
for app in reversed(settings.INSTALLED_APPS): | |
appname = app.split('.')[-1] | |
print '\t', appname | |
res = os.system( './manage.py sqlclear %s >> %s' % ( appname, fn ) ) | |
os.system( 'echo "SET FOREIGN_KEY_CHECKS=1;" >> %s' % ( fn ) ) | |
f = open( fn, 'r' ) | |
c = 0 | |
for l in f: | |
if l.find('DROP TABLE') >= 0: | |
c += 1 | |
f.close() | |
print c, 'tables will be dropped.' | |
os.system( './manage.py dbshell < drop_tables.sql' ); | |
os.remove( fn ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment