Skip to content

Instantly share code, notes, and snippets.

@lqez
Created December 26, 2011 07:12
Show Gist options
  • Save lqez/1520683 to your computer and use it in GitHub Desktop.
Save lqez/1520683 to your computer and use it in GitHub Desktop.
dropping tables in django database
#!/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