-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathinit-db.sh
More file actions
executable file
·135 lines (113 loc) · 3.28 KB
/
Copy pathinit-db.sh
File metadata and controls
executable file
·135 lines (113 loc) · 3.28 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
129
130
131
132
133
134
135
#!/bin/bash
# Database initialization script
set -e
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
VENV_DIR="$SCRIPT_DIR/venv"
# Activate venv if it exists
if [ -d "$VENV_DIR" ]; then
source "$VENV_DIR/bin/activate"
fi
# Detect Python command
if command -v python &> /dev/null; then
PYTHON_CMD="python"
elif command -v python3 &> /dev/null; then
PYTHON_CMD="python3"
else
echo -e "${RED}[ERROR]${NC} Python not found."
exit 1
fi
# Parse arguments
USE_SQLITE=false
DROP_EXISTING=false
while [[ $# -gt 0 ]]; do
case $1 in
--sqlite)
USE_SQLITE=true
shift
;;
--drop)
DROP_EXISTING=true
shift
;;
--help)
cat << EOF
${GREEN}Database Initialization Script${NC}
Usage: $0 [OPTIONS]
Options:
--sqlite Use SQLite instead of PostgreSQL
--drop Drop existing tables before creating (CAUTION!)
--help Show this help message
Examples:
# Initialize PostgreSQL database (default)
$0
# Initialize SQLite database
$0 --sqlite
# Drop and recreate all tables (WARNING: deletes all data!)
$0 --drop
EOF
exit 0
;;
*)
echo -e "${RED}[ERROR]${NC} Unknown option: $1"
exit 1
;;
esac
done
echo ""
echo -e "${GREEN}╔════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ Database Initialization Script ║${NC}"
echo -e "${GREEN}╚════════════════════════════════════════╝${NC}"
echo ""
# Set database URL
if [ "$USE_SQLITE" = true ]; then
export DATABASE_URL="sqlite:///data.db"
echo -e "${BLUE}[INFO]${NC} Using SQLite database: data.db"
else
echo -e "${BLUE}[INFO]${NC} Using PostgreSQL (from config.py)"
fi
# Create initialization Python script
INIT_SCRIPT=$(cat <<'PYTHON_EOF'
import sys
sys.path.insert(0, '.')
from app.app import app
from app.db import db
from app.models.user import UserModel
from app.models.store import StoreModel
from app.models.item import ItemModel
with app.app_context():
if DROP_EXISTING:
print("⚠️ Dropping all existing tables...")
db.drop_all()
print("✓ Tables dropped")
print("Creating database tables...")
db.create_all()
print("✓ Tables created successfully")
# Check what tables exist
inspector = db.inspect(db.engine)
tables = inspector.get_table_names()
print(f"\n✓ Database initialized with {len(tables)} tables:")
for table in tables:
print(f" - {table}")
PYTHON_EOF
)
# Run initialization
if [ "$DROP_EXISTING" = true ]; then
echo -e "${YELLOW}[WARNING]${NC} --drop flag specified. This will DELETE ALL DATA!"
read -p "Are you sure you want to continue? (yes/no): " confirm
if [ "$confirm" != "yes" ]; then
echo "Aborted."
exit 0
fi
echo "$INIT_SCRIPT" | DROP_EXISTING=true $PYTHON_CMD
else
echo "$INIT_SCRIPT" | DROP_EXISTING=false $PYTHON_CMD
fi
echo ""
echo -e "${GREEN}✓ Database initialization complete!${NC}"
echo ""