-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
128 lines (106 loc) · 4.1 KB
/
Copy pathrun.py
File metadata and controls
128 lines (106 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from app import create_app, db
from app.models import User, Project, Task, TimeEntry, Risk, Department, UserRole, TaskAssignment
import sqlite3
app = create_app()
@app.shell_context_processor
def make_shell_context():
return {
'db': db,
'User': User,
'Project': Project,
'Task': Task,
'TimeEntry': TimeEntry,
'Risk': Risk,
'Department': Department,
'UserRole': UserRole,
'TaskAssignment': TaskAssignment
}
@app.cli.command("create-tables")
def create_tables():
"""Create all database tables."""
db.create_all()
print("Database tables created.")
@app.cli.command("fix-database")
def fix_database():
"""Fix missing department_id column in user table."""
# Get the database path from config
db_path = app.config['SQLALCHEMY_DATABASE_URI'].replace('sqlite:///', '')
# Direct SQLite connection to add the missing column
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Check if department table exists, create if not
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='department'")
if not cursor.fetchone():
print("Creating department table...")
cursor.execute("""
CREATE TABLE department (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
description VARCHAR(500)
)
""")
# Check if department_id column exists in user table
cursor.execute("PRAGMA table_info(user)")
columns = [column[1] for column in cursor.fetchall()]
if 'department_id' not in columns:
print("Adding department_id column to user table...")
cursor.execute("ALTER TABLE user ADD COLUMN department_id INTEGER")
conn.commit()
conn.close()
# Create initial departments
with app.app_context():
departments = [
Department(name="Engineering", description="Software development and engineering"),
Department(name="Marketing", description="Marketing and communications"),
Department(name="Finance", description="Financial management and accounting"),
Department(name="HR", description="Human resources and talent management"),
Department(name="Operations", description="Day-to-day business operations")
]
for dept in departments:
existing = Department.query.filter_by(name=dept.name).first()
if not existing:
db.session.add(dept)
db.session.commit()
print("Database fixed successfully.")
@app.cli.command("fix-migrations")
def fix_migrations():
"""Fix migrations with multiple heads by merging them."""
from flask_migrate import current, merge_heads
revisions = current(directory='migrations')
if len(revisions) > 1:
print(f"Found multiple heads: {revisions}")
merge_heads('migrations', revisions)
print("Heads merged. Run 'flask db upgrade' to apply migrations.")
else:
print("No multiple heads found.")
@app.cli.command("reset-migrations")
def reset_migrations():
"""Reset migrations by creating a fresh initial migration."""
import os
import shutil
# Backup the database
if os.path.exists('app.db'):
shutil.copy('app.db', 'app.db.backup')
print("Database backed up to app.db.backup")
# Remove migrations folder
if os.path.exists('migrations'):
shutil.rmtree('migrations')
print("Migrations folder removed")
# Initialize migrations
from flask_migrate import init, migrate, upgrade
init('migrations')
print("Migrations initialized")
# Create migration
migrate(message='Initial database setup')
print("Initial migration created")
# Apply migration
upgrade()
print("Migration applied")
print("Migration reset complete")
if __name__ == '__main__':
import os
# للتطوير المحلي
if os.environ.get('FLASK_ENV') == 'production':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)), debug=False)
else:
app.run(debug=True)