-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase-setup.sh
More file actions
195 lines (166 loc) Β· 5.56 KB
/
database-setup.sh
File metadata and controls
195 lines (166 loc) Β· 5.56 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash
# V2 POC Complete Database Setup Script
# Sets up PostgreSQL and MongoDB with all databases and collections
set -e # Exit on any error
echo "π V2 POC Complete Database Setup"
echo "=================================="
echo ""
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log_info() {
echo -e "${BLUE}βΉοΈ $1${NC}"
}
log_success() {
echo -e "${GREEN}β
$1${NC}"
}
log_warning() {
echo -e "${YELLOW}β οΈ $1${NC}"
}
log_error() {
echo -e "${RED}β $1${NC}"
}
# Check if containers are running
check_container() {
local container_name=$1
if ! docker ps | grep -q "$container_name"; then
log_error "$container_name container is not running"
log_info "Please start containers with: docker-compose up -d"
exit 1
fi
}
log_info "Checking container status..."
check_container "postgres"
check_container "mongodb"
log_success "All database containers are running"
# PostgreSQL Setup
echo ""
log_info "Setting up PostgreSQL databases..."
# Wait for PostgreSQL to be ready
log_info "Waiting for PostgreSQL to be ready..."
sleep 3
# Create test database if it doesn't exist
docker exec aq-devsuite-postgres psql -U pocuser -d poc_db -c "
DO \$\$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_database WHERE datname = 'test_db') THEN
CREATE DATABASE test_db OWNER pocuser;
END IF;
END
\$\$;
" 2>/dev/null || docker exec aq-devsuite-postgres createdb -U pocuser test_db 2>/dev/null || true
# Create additional tables in main database
docker exec aq-devsuite-postgres psql -U pocuser -d poc_db -c "
-- Create users table if not exists
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create worlds table if not exists
CREATE TABLE IF NOT EXISTS worlds (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
description TEXT,
owner_id INTEGER REFERENCES users(id),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create stories table if not exists
CREATE TABLE IF NOT EXISTS stories (
id SERIAL PRIMARY KEY,
title VARCHAR(200) NOT NULL,
content TEXT,
world_id INTEGER REFERENCES worlds(id),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create ai_cost_logs table if not exists
CREATE TABLE IF NOT EXISTS ai_cost_logs (
id SERIAL PRIMARY KEY,
model_name VARCHAR(100) NOT NULL,
prompt_tokens INTEGER DEFAULT 0,
completion_tokens INTEGER DEFAULT 0,
total_tokens INTEGER DEFAULT 0,
estimated_cost DECIMAL(10,6) DEFAULT 0.0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Insert sample data
INSERT INTO users (username, email, password_hash)
VALUES ('demo_user', 'demo@example.com', 'hashed_password')
ON CONFLICT (username) DO NOTHING;
INSERT INTO worlds (name, description, owner_id)
VALUES ('Demo Fantasy World', 'A sample world for testing', 1)
ON CONFLICT DO NOTHING;
"
log_success "PostgreSQL setup completed"
# MongoDB Setup
echo ""
log_info "Setting up MongoDB databases..."
# Execute MongoDB setup
if [ -f "mongo-config/setup.js" ]; then
docker exec aq-devsuite-mongodb mongosh --username mongoadmin --password mongopass123 --authenticationDatabase admin < mongo-config/setup.js
log_success "MongoDB setup completed"
else
log_warning "MongoDB setup script not found, creating databases manually..."
# Create databases manually if setup.js doesn't exist
docker exec aq-devsuite-mongodb mongosh --username mongoadmin --password mongopass123 --authenticationDatabase admin --eval "
// Create main application database
use('poc_mongo_db');
db.createCollection('users');
db.createCollection('worlds');
db.createCollection('stories');
// Create test database
use('poc_test_db');
db.createCollection('test_data');
// Create analytics database
use('poc_analytics_db');
db.createCollection('api_logs');
print('MongoDB databases created successfully');
"
fi
# Verification
echo ""
log_info "Verifying database setup..."
# Check PostgreSQL databases
PG_DATABASES=$(docker exec aq-devsuite-postgres psql -U pocuser -t -c "SELECT datname FROM pg_database WHERE datname IN ('poc_db', 'test_db');" | tr -d ' ' | grep -v '^$')
echo "PostgreSQL databases:"
for db in $PG_DATABASES; do
log_success " π $db"
done
# Check MongoDB databases
echo "MongoDB databases:"
docker exec aq-devsuite-mongodb mongosh --username mongoadmin --password mongopass123 --authenticationDatabase admin --quiet --eval "
db.adminCommand('listDatabases').databases.forEach(db => {
if (db.name.startsWith('poc_')) {
console.log(' π', db.name);
}
});
"
echo ""
log_success "Database setup completed successfully!"
echo ""
echo "π Access Information:"
echo " π PostgreSQL:"
echo " β’ PgAdmin: http://localhost:5050"
echo " β’ Direct: localhost:5432"
echo " β’ Username: pocuser"
echo " β’ Password: pocpass"
echo ""
echo " π MongoDB:"
echo " β’ Mongo Express: http://localhost:8082"
echo " β’ Direct: localhost:27017"
echo " β’ Username: mongoadmin"
echo " β’ Password: mongopass123"
echo ""
echo " β‘ Redis:"
echo " β’ Redis Commander: http://localhost:8084"
echo " β’ Direct: localhost:6379"
echo ""
echo "π All databases are ready for development!"