Skip to content

Commit 1a8c02a

Browse files
committed
fix: Fix Oracle CLI examples failing in GitHub Actions
- Fix BaseCliCommand::buildConfigFromEnv() to check PDODB_SERVICE_NAME/PDODB_SID for Oracle instead of PDODB_DATABASE - Add PDODB_SERVICE_NAME and PDODB_SID to environment variables list in buildConfigFromEnv() - Add setEnvFromConfig() helper function to set environment variables from config array - Update Oracle examples (04-07) to use setEnvFromConfig() for CLI commands - Fix 04-dump-restore.php to set SQLite PDODB_PATH before calling setEnvFromConfig() - Fix 01-ddl.php to handle foreign key constraints for MySQL/MariaDB This fixes the issue where Oracle examples failed in GitHub Actions because environment variables were set but buildConfigFromEnv() required PDODB_DATABASE which Oracle doesn't use (it uses PDODB_SERVICE_NAME or PDODB_SID instead).
1 parent 6d87b1a commit 1a8c02a

File tree

7 files changed

+74
-28
lines changed

7 files changed

+74
-28
lines changed

examples/11-schema/01-ddl.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@
2121
$schema = $db->schema();
2222

2323
// Drop table if exists (cleanup)
24+
// For MySQL/MariaDB, disable foreign key checks to avoid constraint violations
25+
if ($driver === 'mysql' || $driver === 'mariadb') {
26+
$db->rawQuery("SET FOREIGN_KEY_CHECKS=0");
27+
}
2428
$schema->dropTableIfExists('users');
29+
if ($driver === 'mysql' || $driver === 'mariadb') {
30+
$db->rawQuery("SET FOREIGN_KEY_CHECKS=1");
31+
}
2532

2633
// Example 1: Create table with ColumnSchema fluent API
2734
echo "1. Creating table with ColumnSchema fluent API:\n";

examples/11-schema/04-dump-restore.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@
2828
$dbPath = sys_get_temp_dir() . '/pdodb_dump_example_' . uniqid() . '.sqlite';
2929
$db = new PdoDb('sqlite', ['path' => $dbPath]);
3030
// Ensure CLI commands use the same SQLite database file
31+
putenv('PDODB_DRIVER=sqlite');
3132
putenv('PDODB_PATH=' . $dbPath);
3233
} else {
3334
$db = createExampleDb();
35+
// Set environment variables from config for CLI commands
36+
// This ensures BaseCliCommand can access the same database configuration
37+
$config = getExampleConfig();
38+
setEnvFromConfig($config);
3439
}
3540
$driver = $db->connection?->getDriverName() ?? 'sqlite';
3641
$schema = $db->schema();
3742

38-
// Set driver and non-interactive mode for CLI commands
39-
// This ensures BaseCliCommand loads config from examples/config.{driver}.php or environment variables
40-
putenv('PDODB_DRIVER=' . $driver);
4143
putenv('PDODB_NON_INTERACTIVE=1');
4244

4345
// Create CLI application

examples/11-schema/05-monitoring.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
// Create database connection
3131
$db = createPdoDbWithErrorHandling($driver, $config);
3232

33+
// Set environment variables from config for CLI commands
34+
setEnvFromConfig($config);
35+
putenv('PDODB_NON_INTERACTIVE=1');
36+
3337
// Create a test table
3438
$schema = $db->schema();
3539
$schema->dropTableIfExists('monitor_test');

examples/11-schema/06-cache-management.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
// Create cache instance
3131
$cache = new ArrayCache();
3232

33-
// Set cache environment variables for CLI commands to use the same cache
33+
// Set environment variables from config for CLI commands
34+
setEnvFromConfig($config);
3435
putenv('PDODB_CACHE_ENABLED=true');
3536
putenv('PDODB_CACHE_TYPE=array');
3637
putenv('PDODB_NON_INTERACTIVE=1');

examples/11-schema/07-repository-service-generation.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,10 @@
3333
echo "================================================\n\n";
3434
echo "Driver: {$driver}\n\n";
3535

36-
// Set non-interactive mode for CLI commands
36+
// Set environment variables from config for CLI commands
37+
setEnvFromConfig($config);
3738
putenv('PDODB_NON_INTERACTIVE=1');
3839

39-
// Export database config to environment for CLI commands
40-
putenv('PDODB_DRIVER=' . $driver);
41-
if (isset($config['host'])) {
42-
putenv('PDODB_HOST=' . $config['host']);
43-
}
44-
if (isset($config['port'])) {
45-
putenv('PDODB_PORT=' . (string)$config['port']);
46-
}
47-
if (isset($config['database'])) {
48-
putenv('PDODB_DATABASE=' . $config['database']);
49-
}
50-
if (isset($config['username'])) {
51-
putenv('PDODB_USERNAME=' . $config['username']);
52-
}
53-
if (isset($config['password'])) {
54-
putenv('PDODB_PASSWORD=' . $config['password']);
55-
}
56-
if (isset($config['path'])) {
57-
putenv('PDODB_PATH=' . $config['path']);
58-
}
59-
6040
// Create database connection
6141
$db = createPdoDbWithErrorHandling($driver, $config);
6242

examples/helpers.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,51 @@ function getCurrentDriver(PdoDb $db): string
245245
return $connection->getDriverName();
246246
}
247247

248+
/**
249+
* Set environment variables from database configuration for CLI commands
250+
*
251+
* This function ensures that CLI commands can access the same database configuration
252+
* that was used to create the PdoDb instance. It reads the configuration array
253+
* and sets all necessary PDODB_* environment variables.
254+
*
255+
* @param array<string, mixed> $config Database configuration array
256+
*/
257+
function setEnvFromConfig(array $config): void
258+
{
259+
if (isset($config['driver'])) {
260+
putenv('PDODB_DRIVER=' . $config['driver']);
261+
}
262+
if (isset($config['host'])) {
263+
putenv('PDODB_HOST=' . $config['host']);
264+
}
265+
if (isset($config['port'])) {
266+
putenv('PDODB_PORT=' . (string)$config['port']);
267+
}
268+
if (isset($config['username'])) {
269+
putenv('PDODB_USERNAME=' . $config['username']);
270+
}
271+
if (isset($config['password'])) {
272+
putenv('PDODB_PASSWORD=' . $config['password']);
273+
}
274+
if (isset($config['database'])) {
275+
putenv('PDODB_DATABASE=' . $config['database']);
276+
}
277+
if (isset($config['dbname'])) {
278+
putenv('PDODB_DATABASE=' . $config['dbname']);
279+
}
280+
if (isset($config['charset'])) {
281+
putenv('PDODB_CHARSET=' . $config['charset']);
282+
}
283+
if (isset($config['path'])) {
284+
putenv('PDODB_PATH=' . $config['path']);
285+
}
286+
// Oracle-specific
287+
if (isset($config['service_name'])) {
288+
putenv('PDODB_SERVICE_NAME=' . $config['service_name']);
289+
}
290+
if (isset($config['sid'])) {
291+
putenv('PDODB_SID=' . $config['sid']);
292+
}
293+
}
294+
248295

src/cli/BaseCliCommand.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ protected static function buildConfigFromEnv(string $driver): ?array
219219
$envVars = [];
220220
// Always use getenv() to ensure we get variables set via putenv()
221221
// $_ENV may not be updated when putenv() is called
222-
$envVarNames = ['PDODB_HOST', 'PDODB_PORT', 'PDODB_DATABASE', 'PDODB_USERNAME', 'PDODB_PASSWORD', 'PDODB_CHARSET', 'PDODB_PATH', 'PDODB_DRIVER'];
222+
$envVarNames = ['PDODB_HOST', 'PDODB_PORT', 'PDODB_DATABASE', 'PDODB_USERNAME', 'PDODB_PASSWORD', 'PDODB_CHARSET', 'PDODB_PATH', 'PDODB_DRIVER', 'PDODB_SERVICE_NAME', 'PDODB_SID'];
223223
foreach ($envVarNames as $envVar) {
224224
$envValue = getenv($envVar);
225225
if ($envValue !== false) {
@@ -228,7 +228,12 @@ protected static function buildConfigFromEnv(string $driver): ?array
228228
}
229229

230230
// Check required variables
231-
if (!isset($envVars['PDODB_DATABASE']) || !isset($envVars['PDODB_USERNAME'])) {
231+
if ($driver === 'oci') {
232+
// Oracle requires username and either SERVICE_NAME or SID
233+
if (!isset($envVars['PDODB_USERNAME']) || (!isset($envVars['PDODB_SERVICE_NAME']) && !isset($envVars['PDODB_SID']))) {
234+
return null;
235+
}
236+
} elseif (!isset($envVars['PDODB_DATABASE']) || !isset($envVars['PDODB_USERNAME'])) {
232237
// SQLite doesn't require username/database
233238
if ($driver !== 'sqlite') {
234239
return null;

0 commit comments

Comments
 (0)