kinda working safe point
This commit is contained in:
parent
b9a82af12f
commit
6dda02141e
31 changed files with 4302 additions and 2937 deletions
59
app/utils/reset_db.py
Normal file
59
app/utils/reset_db.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
"""
|
||||
Database reset utility - USE WITH CAUTION
|
||||
This will delete and recreate your entire database
|
||||
"""
|
||||
import os
|
||||
import logging
|
||||
from flask import Flask
|
||||
from .. import create_app, db
|
||||
from ..models import User, Folder, File, Download
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def reset_database():
|
||||
"""Reset the entire database and recreate tables"""
|
||||
app = create_app()
|
||||
|
||||
with app.app_context():
|
||||
try:
|
||||
# Drop all tables
|
||||
logger.warning("Dropping all database tables...")
|
||||
db.drop_all()
|
||||
logger.info("All tables dropped successfully")
|
||||
|
||||
# Recreate tables based on models
|
||||
logger.info("Recreating database tables...")
|
||||
db.create_all()
|
||||
logger.info("Database tables created successfully")
|
||||
|
||||
# Create admin user if needed
|
||||
if User.query.filter_by(username='admin').first() is None:
|
||||
admin = User(username='admin', email='admin@example.com', is_admin=True)
|
||||
admin.password = 'adminpassword' # Set to a secure password in production
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
logger.info("Created admin user")
|
||||
|
||||
# Clear uploads folder
|
||||
uploads_folder = app.config['UPLOAD_FOLDER']
|
||||
if os.path.exists(uploads_folder):
|
||||
logger.info(f"Clearing uploads folder: {uploads_folder}")
|
||||
for file in os.listdir(uploads_folder):
|
||||
file_path = os.path.join(uploads_folder, file)
|
||||
try:
|
||||
if os.path.isfile(file_path):
|
||||
os.unlink(file_path)
|
||||
elif os.path.isdir(file_path):
|
||||
import shutil
|
||||
shutil.rmtree(file_path)
|
||||
except Exception as e:
|
||||
logger.error(f"Error removing {file_path}: {e}")
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Error resetting database: {e}")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
reset_database()
|
Loading…
Add table
Add a link
Reference in a new issue