Remove deprecated files and templates: Delete unused files including deployment scripts, environment configurations, and various HTML templates to streamline the project structure. This cleanup enhances maintainability and reduces clutter in the codebase.
This commit is contained in:
81
utils/db_rebuild.py
Executable file
81
utils/db_rebuild.py
Executable file
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
import sys
|
||||
import importlib.util
|
||||
|
||||
# Add the parent directory to path so we can import the app
|
||||
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
from app import app, db_path, create_default_categories
|
||||
from models import db, User, Category
|
||||
|
||||
def rebuild_database():
|
||||
"""Completely rebuilds the database by dropping and recreating all tables."""
|
||||
with app.app_context():
|
||||
print(f"Database path: {db_path}")
|
||||
|
||||
# Back up existing database if it exists
|
||||
if os.path.exists(db_path):
|
||||
backup_path = db_path + '.backup'
|
||||
try:
|
||||
import shutil
|
||||
shutil.copy2(db_path, backup_path)
|
||||
print(f"Backed up existing database to {backup_path}")
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not create backup - {str(e)}")
|
||||
|
||||
# Ensure directory exists
|
||||
os.makedirs(os.path.dirname(db_path), exist_ok=True)
|
||||
|
||||
# Drop all tables and recreate them
|
||||
print("Dropping all tables...")
|
||||
db.drop_all()
|
||||
|
||||
print("Creating all tables...")
|
||||
db.create_all()
|
||||
|
||||
# Create admin user
|
||||
print("Creating admin user...")
|
||||
admin = User(
|
||||
username='admin',
|
||||
email='admin@example.com',
|
||||
is_admin=True,
|
||||
created_at=datetime.utcnow()
|
||||
)
|
||||
admin.set_password('admin')
|
||||
db.session.add(admin)
|
||||
|
||||
# Create regular user
|
||||
print("Creating regular user...")
|
||||
user = User(
|
||||
username='user',
|
||||
email='user@example.com',
|
||||
is_admin=False,
|
||||
created_at=datetime.utcnow()
|
||||
)
|
||||
user.set_password('user')
|
||||
db.session.add(user)
|
||||
|
||||
try:
|
||||
# Commit to generate user IDs
|
||||
db.session.commit()
|
||||
print("Users created successfully!")
|
||||
|
||||
# Create default categories
|
||||
print("Creating default categories...")
|
||||
create_default_categories()
|
||||
|
||||
print("Database rebuild completed successfully!")
|
||||
return True
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
print(f"Error during database rebuild: {str(e)}")
|
||||
raise
|
||||
|
||||
if __name__ == "__main__":
|
||||
rebuild_database()
|
||||
Reference in New Issue
Block a user