How can I drop all tables from a database using manage.py and command line? Is there any way to do that executing manage.py with appropriate parameters so I can execute it from a .NET application?
How can I drop all tables from a database using manage.py and command line? Is there any way to do that executing manage.py with appropriate parameters so I can execute it from a .NET application?
If you are using psql and have django-more 2.0.0 installed, you can do
manage.py reset_schema
Here's an example Makefile to do some nice things with multiple settings files:
test:
python manage.py test --settings=my_project.test
db_drop:
echo 'DROP DATABASE my_project_development;' | ./manage.py dbshell
echo 'DROP DATABASE my_project_test;' | ./manage.py dbshell
db_create:
echo 'CREATE DATABASE my_project_development;' | ./manage.py dbshell
echo 'CREATE DATABASE my_project_test;' | ./manage.py dbshell
db_migrate:
python manage.py migrate --settings=my_project.base
python manage.py migrate --settings=my_project.test
db_reset: db_drop db_create db_migrate
.PHONY: test db_drop db_create db_migrate db_reset
Then you can do things like:
$ make db_reset
This answer is for postgresql DB:
Run: echo 'drop owned by some_user' | ./manage.py dbshell
NOTE: some_user is the name of the user you use to access the database, see settings.py file:
default_database = {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'somedbname',
'USER': 'some_user',
'PASSWORD': 'somepass',
'HOST': 'postgresql',
'PORT': '',
}
I would recommend you to install django-extensions and use python manage.py reset_db command. It does exactly what you want.
Drops all tables and recreates them:
python manage.py sqlclear app1 app2 appN | sed -n "2,$p" | sed -n "$ !p" | sed "s/";/" CASCADE;/" | sed -e "1s/^/BEGIN;/" -e "$s/$/COMMIT;/" | python manage.py dbshell
python manage.py syncdb
Explanation:
manage.py sqlclear - "prints the DROP TABLE SQL statements for the given app name(s)"
sed -n "2,$p" - grabs all lines except first line
sed -n "$ !p" - grabs all lines except last line
sed "s/";/" CASCADE;/" - replaces all semicolons (;) with (CASCADE;)
sed -e "1s/^/BEGIN;/" -e "$s/$/COMMIT;/" - inserts (BEGIN;) as first text, inserts (COMMIT;) as last text
manage.py dbshell - "Runs the command-line client for the database engine specified in your ENGINE setting, with the connection parameters specified in your USER, PASSWORD, etc., settings"
manage.py syncdb - "Creates the database tables for all apps in INSTALLED_APPS whose tables have not already been created"
Dependencies:
Credits:
@Manoj Govindan and @Mike DeSimone for sqlclear piped to dbshell
@jpic for 'sed "s/";/" CASCADE;/"'
A solution to remove database and migrations manually.
at the same level of manage.py, create clean.py
import os
def recursor(dirpath):
# print(dirpath)
delfiles = []
deldirs = []
with os.scandir(dirpath) as l1:
for e1 in l1:
if not e1.is_file():
with os.scandir(e1.path) as l2:
for e2 in l2:
if e2.name == 'migrations':
with os.scandir(e2.path) as l3:
for e3 in l3:
if not e3.name == '__init__.py':
print(e3.path)
if e3.is_file():
delfiles.append(e3.path)
else:
deldirs.append(e3.path)
with os.scandir(e3.path) as l4:
for e4 in l4:
delfiles.append(e4)
yn = input('are you sure to delete all the files above?(y/n)')
if yn == 'y':
for dp in delfiles:
os.remove(dp)
for dp in deldirs:
os.rmdir(dp)
recursor(os.path.dirname(os.path.realpath(__file__)))
delete db.sqlite3 file and run clean.py