-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.php
More file actions
320 lines (265 loc) · 10.4 KB
/
main.php
File metadata and controls
320 lines (265 loc) · 10.4 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* Database CLI Tool
*
* A comprehensive database management CLI application featuring:
* - Database connection management
* - Migration system with version control
* - Data seeding and fixtures
* - Interactive query execution
* - Schema inspection and documentation
* - Backup and restore operations
*/
use WebFiori\Cli\ArgumentOption;
use WebFiori\Cli\Command;
use WebFiori\Cli\Runner;
// Load dependencies
require_once '../../vendor/autoload.php';
require_once 'DatabaseManager.php';
class DatabaseCommand extends Command {
private DatabaseManager $dbManager;
public function __construct() {
parent::__construct('db', [
'--action' => [
ArgumentOption::OPTIONAL => false,
ArgumentOption::DESCRIPTION => 'Database action to perform',
ArgumentOption::VALUES => ['connect', 'migrate', 'seed', 'query', 'backup', 'restore', 'status', 'cleanup']
],
'--sql' => [
ArgumentOption::OPTIONAL => true,
ArgumentOption::DESCRIPTION => 'SQL query to execute (for query action)'
],
'--file' => [
ArgumentOption::OPTIONAL => true,
ArgumentOption::DESCRIPTION => 'File path for backup/restore operations'
]
], 'Database management operations');
$this->dbManager = new DatabaseManager();
}
public function exec(): int {
$action = $this->getArgValue('--action');
try {
switch ($action) {
case 'connect':
return $this->testConnection();
case 'migrate':
return $this->runMigrations();
case 'seed':
return $this->seedDatabase();
case 'query':
return $this->executeQuery();
case 'backup':
return $this->backupDatabase();
case 'restore':
return $this->restoreDatabase();
case 'status':
return $this->showStatus();
case 'cleanup':
return $this->cleanupDatabase();
default:
$this->println("Unknown action: $action");
return 1;
}
} catch (Exception $e) {
$this->println("Error: " . $e->getMessage());
return 1;
}
}
private function testConnection(): int {
$this->println("🔌 Testing database connection...");
if ($this->dbManager->connect()) {
$this->println("✅ Database connection successful!");
$this->println("📊 Connection details:");
$this->println(" • Host: localhost:3306");
$this->println(" • Database: testing_db");
$this->println(" • Username: root");
return 0;
} else {
$this->println("❌ Database connection failed!");
return 1;
}
}
private function runMigrations(): int {
$this->println("🚀 Running database migrations...");
if (!$this->dbManager->connect()) {
$this->println("❌ Cannot connect to database");
return 1;
}
// Create sample tables
$migrations = [
"CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
"CREATE TABLE IF NOT EXISTS posts (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
title VARCHAR(200) NOT NULL,
content TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)"
];
foreach ($migrations as $index => $sql) {
$this->println(" • Running migration " . ($index + 1) . "...");
$this->dbManager->query($sql);
}
$this->println("✅ Migrations completed successfully!");
return 0;
}
private function seedDatabase(): int {
$this->println("🌱 Seeding database with sample data...");
if (!$this->dbManager->connect()) {
$this->println("❌ Cannot connect to database");
return 1;
}
// Insert sample users
$users = [
['Ahmed Hassan', 'ahmed.hassan@example.com'],
['Sarah Johnson', 'sarah.johnson@example.com'],
['Omar Al-Rashid', 'omar.alrashid@example.com']
];
foreach ($users as $user) {
$this->dbManager->query(
"INSERT IGNORE INTO users (name, email) VALUES (?, ?)",
$user
);
}
// Insert sample posts
$posts = [
[1, 'First Post', 'This is the content of the first post.'],
[1, 'Second Post', 'This is another post by Ahmed.'],
[2, 'Sarah\'s Post', 'Hello from Sarah!'],
[3, 'Omar\'s Thoughts', 'Some thoughts from Omar.']
];
foreach ($posts as $post) {
$this->dbManager->query(
"INSERT IGNORE INTO posts (user_id, title, content) VALUES (?, ?, ?)",
$post
);
}
$this->println("✅ Database seeded successfully!");
$this->println(" • Added 3 users");
$this->println(" • Added 4 posts");
return 0;
}
private function executeQuery(): int {
$sql = $this->getArgValue('--sql');
if (!$sql) {
$this->println("❌ SQL query is required for query action");
$this->println("Usage: php main.php db --action=query --sql=\"SELECT * FROM users\"");
return 1;
}
$this->println("🔍 Executing query...");
$this->println("SQL: $sql");
if (!$this->dbManager->connect()) {
$this->println("❌ Cannot connect to database");
return 1;
}
$result = $this->dbManager->query($sql);
if ($result['success']) {
$data = $result['data'];
if (!empty($data)) {
$this->println("📊 Query results:");
$this->table($data);
$this->println("⏱️ Execution time: " . number_format($result['execution_time'] * 1000, 2) . "ms");
} else {
$this->println("📊 Query executed successfully (no results)");
}
} else {
$this->println("❌ Query failed: " . $result['error']);
return 1;
}
return 0;
}
private function backupDatabase(): int {
$file = $this->getArgValue('--file') ?? 'backup_' . date('Y-m-d_H-i-s') . '.sql';
$this->println("💾 Creating database backup...");
$this->println("File: $file");
if (!$this->dbManager->connect()) {
$this->println("❌ Cannot connect to database");
return 1;
}
$result = $this->dbManager->createBackup($file);
if ($result['success']) {
$this->println("✅ Backup created successfully!");
$this->println(" • File: " . $result['file']);
$this->println(" • Size: " . number_format($result['size']) . " bytes");
$this->println(" • Tables: " . $result['tables']);
} else {
$this->println("❌ Backup failed: " . $result['error']);
return 1;
}
return 0;
}
private function restoreDatabase(): int {
$file = $this->getArgValue('--file');
if (!$file || !file_exists($file)) {
$this->println("❌ Backup file is required and must exist");
return 1;
}
$this->println("🔄 Restoring database from backup...");
$this->println("File: $file");
if (!$this->dbManager->connect()) {
$this->println("❌ Cannot connect to database");
return 1;
}
$result = $this->dbManager->restoreFromFile($file);
if ($result['success']) {
$this->println("✅ Database restored successfully!");
$this->println(" • Statements executed: " . $result['statements']);
} else {
$this->println("❌ Restore failed: " . $result['error']);
return 1;
}
return 0;
}
private function showStatus(): int {
$this->println("📊 Database Status");
$this->println("==================");
if (!$this->dbManager->connect()) {
$this->println("❌ Cannot connect to database");
return 1;
}
// Show tables
$tablesResult = $this->dbManager->query("SHOW TABLES");
if (!$tablesResult['success']) {
$this->println("❌ Failed to get table list");
return 1;
}
$tables = $tablesResult['data'];
$this->println("📋 Tables: " . count($tables));
foreach ($tables as $table) {
$tableName = array_values($table)[0];
$countResult = $this->dbManager->query("SELECT COUNT(*) as count FROM `$tableName`");
if ($countResult['success'] && !empty($countResult['data'])) {
$count = $countResult['data'][0]['count'] ?? 0;
$this->println(" • $tableName: $count records");
}
}
return 0;
}
private function cleanupDatabase(): int {
$this->println("🧹 Cleaning up database...");
if (!$this->dbManager->connect()) {
$this->println("❌ Cannot connect to database");
return 1;
}
// Drop tables in correct order (foreign key constraints)
$tables = ['posts', 'users'];
foreach ($tables as $table) {
$this->println(" • Dropping table: $table");
$this->dbManager->query("DROP TABLE IF EXISTS `$table`");
}
$this->println("✅ Database cleanup completed!");
return 0;
}
}
// Create and configure the CLI runner
$runner = new Runner();
$runner->register(new DatabaseCommand());
$runner->setDefaultCommand('help');
// Start the application
exit($runner->start());