forked from serversideup/docker-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-db-connection.php
More file actions
143 lines (120 loc) · 5.11 KB
/
Copy pathtest-db-connection.php
File metadata and controls
143 lines (120 loc) · 5.11 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
<?php
/**
* Test Laravel Database Connection
*
* This script tests if the Laravel application can connect to its configured database.
* It's designed to be called from shell scripts during container initialization.
*
* Usage: php test-db-connection.php /path/to/app/base/dir [migration_mode] [database_connection]
*
* Arguments:
* app_base_dir - Path to Laravel application root
* migration_mode - Migration mode: 'default', 'fresh', or 'refresh' (optional, defaults to 'default')
* database_connection - Name of the database connection to test (optional, defaults to 'default')
*
* Exit codes:
* 0 - Success: Database is ready and accessible
* 1 - Failure: Database connection failed or other error
*
* @package serversideup/php
*/
use Illuminate\Support\Facades\DB;
// Validate arguments
if ($argc < 2 || $argc > 4) {
fwrite(STDERR, "Usage: php test-db-connection.php /path/to/app/base/dir [migration_mode] [database_connection]\n");
exit(1);
}
$appBaseDir = $argv[1];
$migrationMode = $argc >= 3 ? $argv[2] : 'default';
$databaseConnection = $argc >= 4 ? $argv[3] : null;
// Validate migration mode
$validModes = ['default', 'fresh', 'refresh'];
if (!in_array($migrationMode, $validModes)) {
fwrite(STDERR, "Error: Invalid migration mode '{$migrationMode}'. Must be one of: " . implode(', ', $validModes) . "\n");
exit(1);
}
// Validate that the app base directory exists
if (!is_dir($appBaseDir)) {
fwrite(STDERR, "Error: App base directory does not exist: {$appBaseDir}\n");
exit(1);
}
// Validate that required Laravel files exist
$vendorAutoload = "{$appBaseDir}/vendor/autoload.php";
$bootstrapApp = "{$appBaseDir}/bootstrap/app.php";
if (!file_exists($vendorAutoload)) {
fwrite(STDERR, "Error: Composer autoload file not found: {$vendorAutoload}\n");
exit(1);
}
if (!file_exists($bootstrapApp)) {
fwrite(STDERR, "Error: Laravel bootstrap file not found: {$bootstrapApp}\n");
exit(1);
}
// Bootstrap Laravel
try {
require $vendorAutoload;
$app = require_once $bootstrapApp;
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
} catch (Exception $e) {
fwrite(STDERR, "Error bootstrapping Laravel: {$e->getMessage()}\n");
exit(1);
}
// Test database connection
try {
// Use specific database connection if provided
$connection = $databaseConnection ? DB::connection($databaseConnection) : DB::connection();
$driver = $connection->getDriverName();
// SQLite special handling
if ($driver === 'sqlite') {
$dbPath = $connection->getDatabaseName();
// Handle in-memory SQLite databases
if ($dbPath === ':memory:') {
fwrite(STDOUT, "SQLite in-memory database detected - ready\n");
exit(0);
}
$dbDirectory = dirname($dbPath);
// Check if database file already exists
if (file_exists($dbPath)) {
fwrite(STDOUT, "SQLite database file exists: {$dbPath}\n");
exit(0);
}
// Database file doesn't exist - check if directory exists and is writable
if (!is_dir($dbDirectory)) {
fwrite(STDERR, "SQLite database directory does not exist: {$dbDirectory}\n");
fwrite(STDERR, "Please create the directory before running migrations.\n");
fwrite(STDERR, "Example: mkdir -p {$dbDirectory}\n");
exit(1);
}
if (!is_writable($dbDirectory)) {
fwrite(STDERR, "SQLite database directory is not writable: {$dbDirectory}\n");
fwrite(STDERR, "Please check directory permissions.\n");
exit(1);
}
// For 'fresh' and 'refresh' modes, the database file must already exist
if ($migrationMode === 'fresh' || $migrationMode === 'refresh') {
fwrite(STDERR, "SQLite database file does not exist: {$dbPath}\n");
fwrite(STDERR, "Migration mode '{$migrationMode}' requires the database file to exist.\n");
fwrite(STDERR, "Either:\n");
fwrite(STDERR, " 1. Create the database (ensure it has read and write permissions for your user): touch {$dbPath}\n");
fwrite(STDERR, " 2. Use AUTORUN_LARAVEL_MIGRATION_MODE=default to let Laravel create it\n");
exit(1);
}
// Directory exists and is writable - migrations can create the database file
fwrite(STDOUT, "SQLite database directory is ready - migrations will create database\n");
exit(0);
}
// Test connection for other database drivers
$connection->getPdo();
if ($connection->getDatabaseName()) {
$connectionName = $databaseConnection ? " ({$databaseConnection})" : '';
fwrite(STDOUT, "Database connection successful ({$driver}){$connectionName}\n");
exit(0);
} else {
fwrite(STDERR, "Database name not found\n");
exit(1);
}
} catch (Exception $e) {
$connectionName = $databaseConnection ? " ({$databaseConnection})" : '';
fwrite(STDERR, "Database connection error{$connectionName}: {$e->getMessage()}\n");
exit(1);
}