public
Created

dropping tables in django database

  • Download Gist
gistfile1.py
Python
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 27 28 29 30
#!/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 );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.