-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import os | ||
import shutil | ||
from pathlib import Path | ||
|
||
|
||
def delete_migration_files(app_name): | ||
migrations_folder = Path(app_name) / "migrations" | ||
|
||
# Delete all files except '__init__.py' | ||
for file in migrations_folder.iterdir(): | ||
if file.name == "__pycache__": | ||
shutil.rmtree(file.resolve()) | ||
elif file.name != "__init__.py": | ||
file.unlink() | ||
|
||
|
||
def delete_sqlite_db(db_path): | ||
db_path = Path(db_path) | ||
if db_path.exists(): | ||
db_path.unlink() | ||
|
||
|
||
def confirm_action(): | ||
confirmation = input( | ||
"This action will hard-reset all your migrations and delete your database. Are you sure you want to continue? (yes/no): " | ||
).lower() | ||
return confirmation == "yes" | ||
|
||
|
||
if __name__ == "__main__": | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings") | ||
if not confirm_action(): | ||
print("Script execution aborted.") | ||
exit() | ||
|
||
# Find Django app names in the current project directory | ||
django_apps = [ | ||
path.parent.parent.name for path in Path(".").glob("**/migrations/__init__.py") | ||
] | ||
|
||
for app_name in django_apps: | ||
# Delete migration files for each app | ||
delete_migration_files(app_name) | ||
|
||
# Delete SQLite DB using a relative path | ||
db_path = "db.sqlite3" # Relative path from the current working directory | ||
delete_sqlite_db(db_path) |