Files
family-hub/backend/reset_database.py

85 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""
Simple script to delete and reinitialize the database.
Run this from the backend directory.
"""
import os
import subprocess
import sys
from pathlib import Path
# Paths
BACKEND_DIR = r'D:\Hosted\familyhub\backend'
DB_PATH = os.path.join(BACKEND_DIR, 'data', 'family_hub.db')
MIGRATIONS_DIR = os.path.join(BACKEND_DIR, 'migrations')
print("=" * 60)
print("Family Hub Database Reset")
print("=" * 60)
# Delete old database
if os.path.exists(DB_PATH):
print(f"\n1. Deleting old database: {DB_PATH}")
os.remove(DB_PATH)
print(" ✓ Database deleted")
else:
print(f"\n1. No existing database found at: {DB_PATH}")
# Run init_db.py
print("\n2. Running database initialization...")
init_script = os.path.join(BACKEND_DIR, 'init_db.py')
# Run the init script
result = subprocess.run(
[sys.executable, init_script],
cwd=BACKEND_DIR,
capture_output=True,
text=True
)
print(result.stdout)
if result.stderr:
print("Errors:", result.stderr)
if result.returncode != 0:
print("\n" + "=" * 60)
print("❌ Database initialization failed!")
print("=" * 60)
sys.exit(1)
# Run migrations
print("\n3. Running database migrations...")
migration_files = sorted([
f for f in os.listdir(MIGRATIONS_DIR)
if f.endswith('.py') and f[0].isdigit()
])
for migration_file in migration_files:
migration_path = os.path.join(MIGRATIONS_DIR, migration_file)
print(f"\n Running {migration_file}...")
result = subprocess.run(
[sys.executable, migration_path],
cwd=BACKEND_DIR,
capture_output=True,
text=True
)
if result.stdout:
# Indent the output
for line in result.stdout.split('\n'):
if line:
print(f" {line}")
if result.returncode != 0:
print(f" ❌ Migration {migration_file} failed!")
if result.stderr:
print(f" Error: {result.stderr}")
else:
print(f" ✅ Migration {migration_file} completed")
print("\n" + "=" * 60)
print("✅ Database reset complete!")
print("=" * 60)
print("\nYou can now restart the backend server.")