-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_database.py
More file actions
64 lines (54 loc) · 1.96 KB
/
setup_database.py
File metadata and controls
64 lines (54 loc) · 1.96 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
#!/usr/bin/env python3
"""
Database setup script for TRACTools.
Creates the database and instance directory with proper permissions.
"""
import os
import sys
from pathlib import Path
def setup_database():
"""Set up the database and directories."""
print("TRACTools Database Setup")
print("=======================")
# Create instance directory
instance_dir = Path('instance')
instance_dir.mkdir(mode=0o755, exist_ok=True)
print(f"✅ Created instance directory: {instance_dir.absolute()}")
# Check permissions
if not os.access(instance_dir, os.W_OK):
print(f"❌ Instance directory is not writable: {instance_dir.absolute()}")
print("Run: sudo chown $USER:$USER instance && chmod 755 instance")
return False
# Test database creation
db_path = instance_dir / 'tractools.db'
try:
# Try to create and write to the database file
import sqlite3
conn = sqlite3.connect(str(db_path))
conn.execute("CREATE TABLE test_table (id INTEGER)")
conn.execute("DROP TABLE test_table")
conn.close()
print(f"✅ Database path is writable: {db_path}")
# Remove test database
if db_path.exists():
db_path.unlink()
except Exception as e:
print(f"❌ Cannot create database at {db_path}: {e}")
return False
print("\n✅ Database setup successful!")
print("\nNext steps:")
print("1. Run: flask db init")
print("2. Run: flask db migrate -m 'Initial migration'")
print("3. Run: flask db upgrade")
print("4. Or run: flask init-db # For initial setup without migrations")
return True
if __name__ == '__main__':
try:
success = setup_database()
sys.exit(0 if success else 1)
except KeyboardInterrupt:
print("\n\nSetup cancelled by user.")
sys.exit(1)
except Exception as e:
print(f"\n❌ Setup failed: {e}")
sys.exit(1)