Last active
December 15, 2019 14:37
-
-
Save Sheikh2Imran/642501bacafb4d338702a0d561786a21 to your computer and use it in GitHub Desktop.
Clean all postgres database model By running script
This file contains hidden or 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
| from django.db import connections, OperationalError | |
| # Drop all tables from a given database | |
| """ | |
| python manage.py runscript clean_database_tables | |
| """ | |
| def run(): | |
| primary_db = connections['default'] | |
| try: | |
| primary_conn = primary_db.cursor() | |
| except OperationalError as err: | |
| print(err) | |
| return None | |
| try: | |
| primary_conn.execute( | |
| "SELECT table_schema,table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_schema,table_name") | |
| rows = primary_conn.fetchall() | |
| for row in rows: | |
| print("dropping table: ", row[1]) | |
| primary_conn.execute("drop table " + row[1] + " cascade") | |
| primary_conn.close() | |
| primary_conn.close() | |
| except Exception as err: | |
| print(err) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment