From 16720caf19e7881fb6f7fd990e6daecad6ff070b Mon Sep 17 00:00:00 2001 From: Mark Gregory Date: Wed, 11 Nov 2026 11:16:35 +0000 Subject: [PATCH] db2 driver updates --- DB2-RELEASE-VERIFICATION.md | 31 ++ app/Database/Db2/Db2SchemaGrammar.php | 7 +- ...emaGrammar.php.bak-stage08-20261111-111313 | 355 ++++++++++++++++++ .../Feature/Db2/ReleaseCompatibilityTest.php | 66 ++++ ...bilityTest.php.bak-stage08-20261111-111244 | 63 ++++ ...bilityTest.php.bak-stage08-20261111-111313 | 63 ++++ 6 files changed, 584 insertions(+), 1 deletion(-) create mode 100644 DB2-RELEASE-VERIFICATION.md create mode 100644 app/Database/Db2/Db2SchemaGrammar.php.bak-stage08-20261111-111313 create mode 100644 tests/Feature/Db2/ReleaseCompatibilityTest.php create mode 100644 tests/Feature/Db2/ReleaseCompatibilityTest.php.bak-stage08-20261111-111244 create mode 100644 tests/Feature/Db2/ReleaseCompatibilityTest.php.bak-stage08-20261111-111313 diff --git a/DB2-RELEASE-VERIFICATION.md b/DB2-RELEASE-VERIFICATION.md new file mode 100644 index 0000000..ba431e7 --- /dev/null +++ b/DB2-RELEASE-VERIFICATION.md @@ -0,0 +1,31 @@ +# Db2 driver release verification + +Verified 11 November 2026 with Laravel 13.25.0, PHP 8.5.11, PDO_ODBC on +macOS, and Db2 LUW 12.1.5.0 (fix pack 0). + +| Run | Passed | Assertions | +| --- | ---: | ---: | +| Release smoke test | 1 | 8 | +| Db2 integration suite | 45 | 139 | +| Full Laravel suite | 78 | 220 | + +The smoke test creates a random Db2 schema, runs all five application migration +files, and uses an authenticated Laravel HTTP test to render the security +settings page with a Db2-backed passkey. It verifies the `failed_jobs.failed_at` +default, rolls back the migration batch, checks that the migration history is +empty, and drops the disposable schema. The Db2 account needs CREATE SCHEMA +and permissions to manage objects in that schema. The full suite uses SQLite +for the general Laravel feature tests and Db2 for its integration tests. + +```sh +composer validate --no-check-publish --no-interaction +APP_ENV=testing php artisan test --do-not-cache-result tests/Feature/Db2/ReleaseCompatibilityTest.php +APP_ENV=testing php artisan test --do-not-cache-result tests/Feature/Db2 +APP_ENV=testing php artisan test --do-not-cache-result +``` + +A skipped Db2 test does not verify compatibility. This matrix covers one +Db2 LUW and Laravel combination. It does not certify other releases, Db2 for +i or z/OS, or browser-based WebAuthn enrolment and authentication. See the +[schema compatibility notes](DB2-SCHEMA-COMPATIBILITY.md) and +[query compatibility notes](DB2-QUERY-COMPATIBILITY.md) for supported cases and limits. diff --git a/app/Database/Db2/Db2SchemaGrammar.php b/app/Database/Db2/Db2SchemaGrammar.php index fbd728e..3fae730 100644 --- a/app/Database/Db2/Db2SchemaGrammar.php +++ b/app/Database/Db2/Db2SchemaGrammar.php @@ -141,6 +141,9 @@ class Db2SchemaGrammar extends Grammar return $schema === null ? $this->wrapValue($name) : $this->wrapValue($schema).'.'.$this->wrapValue($name); } + /** + * Compile a Db2 column declaration for CREATE TABLE or ADD COLUMN. + */ private function columnDefinition(Fluent $column): string { $sql = $this->wrap($column).' '.$this->getType($column); @@ -151,7 +154,9 @@ class Db2SchemaGrammar extends Grammar if (! $column->nullable) { $sql .= ' NOT NULL'; } - if ($column->default !== null) { + if ($column->useCurrent && in_array($column->type, ['timestamp', 'dateTime'], true)) { + $sql .= ' DEFAULT CURRENT TIMESTAMP'; + } elseif ($column->default !== null) { $sql .= ' DEFAULT '.$this->getDefaultValue($column->default); } diff --git a/app/Database/Db2/Db2SchemaGrammar.php.bak-stage08-20261111-111313 b/app/Database/Db2/Db2SchemaGrammar.php.bak-stage08-20261111-111313 new file mode 100644 index 0000000..fbd728e --- /dev/null +++ b/app/Database/Db2/Db2SchemaGrammar.php.bak-stage08-20261111-111313 @@ -0,0 +1,355 @@ +getAddedColumns() as $column) { + $columns[] = $this->columnDefinition($column); + if ($column->autoIncrement) { + $primaryKeys[] = $column->name; + } + } + + $explicitPrimary = collect($blueprint->getCommands())->contains('name', 'primary'); + if ($primaryKeys !== [] && ! $explicitPrimary) { + $columns[] = sprintf( + 'PRIMARY KEY (%s)', + $this->columnize($primaryKeys) + ); + } + + return sprintf( + 'CREATE TABLE %s (%s)', + $this->wrapTable($blueprint), + implode(', ', $columns) + ); + } + + public function compileAdd(Blueprint $blueprint, Fluent $command) + { + return 'ALTER TABLE '.$this->wrapTable($blueprint).' ADD COLUMN '.$this->columnDefinition($command->column); + } + + public function compileChange(Blueprint $blueprint, Fluent $command) + { + $column = $command->column; + if ($column->autoIncrement) { + throw new \RuntimeException('Changing a Db2 identity column is unsupported.'); + } + + $columns = array_column($this->connection->getSchemaBuilder()->getColumns($blueprint->getTable()), null, 'name'); + $current = $columns[$column->name] ?? null; + if ($current === null || $current['auto_increment']) { + throw new \RuntimeException('Db2 change requires an existing non-identity column.'); + } + + $prefix = 'ALTER TABLE '.$this->wrapTable($blueprint).' ALTER COLUMN '.$this->wrap($column); + $type = $this->getType($column); + $statements = []; + + if (strcasecmp($current['type'], $type === 'TIMESTAMP' ? 'TIMESTAMP(6)' : $type) !== 0) { + $statements[] = $prefix.' SET DATA TYPE '.$type; + } + if ($current['nullable'] !== (bool) $column->nullable) { + $statements[] = $prefix.($column->nullable ? ' DROP NOT NULL' : ' SET NOT NULL'); + } + if ($column->default !== null) { + $statements[] = $prefix.' SET DEFAULT '.$this->getDefaultValue($column->default); + } elseif ($current['default'] !== null) { + $statements[] = $prefix.' DROP DEFAULT'; + } + + return $statements; + } + + public function compileDropColumn(Blueprint $blueprint, Fluent $command) + { + $columns = array_map(fn ($column) => 'DROP COLUMN '.$this->wrap($column), $command->columns); + + return 'ALTER TABLE '.$this->wrapTable($blueprint).' '.implode(' ', $columns); + } + + public function compileDrop(Blueprint $blueprint, Fluent $command) + { + return 'DROP TABLE '.$this->wrapTable($blueprint); + } + + public function compileDropIfExists(Blueprint $blueprint, Fluent $command) + { + return 'DROP TABLE IF EXISTS '.$this->wrapTable($blueprint); + } + + public function compileRename(Blueprint $blueprint, Fluent $command) + { + [$schema] = $this->connection->getSchemaBuilder()->parseSchemaAndTable($blueprint->getTable()); + [$targetSchema, $target] = $this->connection->getSchemaBuilder()->parseSchemaAndTable($command->to); + if ($targetSchema !== null && $targetSchema !== ($schema ?? $this->connection->getConfig('schema'))) { + throw new \RuntimeException('Db2 cannot rename a table across schemas.'); + } + + return 'RENAME TABLE '.$this->wrapTable($blueprint).' TO '.$this->wrapTable($target); + } + + public function compileRenameColumn(Blueprint $blueprint, Fluent $command) + { + return 'ALTER TABLE '.$this->wrapTable($blueprint) + .' RENAME COLUMN '.$this->wrap($command->from).' TO '.$this->wrap($command->to); + } + + public function compileDropForeign(Blueprint $blueprint, Fluent $command) + { + return 'ALTER TABLE '.$this->wrapTable($blueprint).' DROP FOREIGN KEY '.$this->wrap($command->index); + } + + public function compileDropPrimary(Blueprint $blueprint, Fluent $command) + { + return 'ALTER TABLE '.$this->wrapTable($blueprint).' DROP PRIMARY KEY'; + } + + public function compileDropUnique(Blueprint $blueprint, Fluent $command) + { + return $this->compileDropIndex($blueprint, $command); + } + + public function compileDropIndex(Blueprint $blueprint, Fluent $command) + { + return 'DROP INDEX '.$this->wrapIndex($blueprint, $command->index); + } + + public function compileRenameIndex(Blueprint $blueprint, Fluent $command) + { + return 'RENAME INDEX '.$this->wrapIndex($blueprint, $command->from) + .' TO '.$this->wrapValue($command->to); + } + + private function wrapIndex(Blueprint $blueprint, string $name): string + { + [$schema] = $this->connection->getSchemaBuilder()->parseSchemaAndTable($blueprint->getTable()); + + return $schema === null ? $this->wrapValue($name) : $this->wrapValue($schema).'.'.$this->wrapValue($name); + } + + private function columnDefinition(Fluent $column): string + { + $sql = $this->wrap($column).' '.$this->getType($column); + + if ($column->autoIncrement) { + $sql .= ' GENERATED BY DEFAULT AS IDENTITY'; + } + if (! $column->nullable) { + $sql .= ' NOT NULL'; + } + if ($column->default !== null) { + $sql .= ' DEFAULT '.$this->getDefaultValue($column->default); + } + + return $sql; + } + + protected function typeBigInteger(Fluent $column) + { + return 'BIGINT'; + } + + protected function typeInteger(Fluent $column) + { + return 'INTEGER'; + } + + protected function typeSmallInteger(Fluent $column) + { + return 'SMALLINT'; + } + + protected function typeString(Fluent $column) + { + return 'VARCHAR(' . ($column->length ?: 255) . ')'; + } + + protected function typeChar(Fluent $column) + { + return 'CHAR('.$column->length.')'; + } + + protected function typeUuid(Fluent $column) + { + return 'CHAR(36)'; + } + + protected function typeTime(Fluent $column) + { + if ($column->precision !== null && $column->precision !== 0) { + throw new \InvalidArgumentException('Db2 TIME does not support fractional-second precision.'); + } + + return 'TIME'; + } + + protected function typeText(Fluent $column) + { + return 'CLOB(1M)'; + } + + protected function typeLongText(Fluent $column) + { + return 'CLOB(2M)'; + } + + protected function typeBoolean(Fluent $column) + { + return 'SMALLINT'; + } + + protected function typeTimestamp(Fluent $column) + { + return $column->precision === null ? 'TIMESTAMP' : 'TIMESTAMP('.$column->precision.')'; + } + + protected function typeDateTime(Fluent $column) + { + return $this->typeTimestamp($column); + } + + protected function typeDate(Fluent $column) + { + return 'DATE'; + } + + protected function typeBinary(Fluent $column) + { + return 'BLOB(1M)'; + } + + protected function typeDecimal(Fluent $column) + { + return sprintf( + 'DECIMAL(%d,%d)', + $column->total ?? 8, + $column->places ?? 2 + ); + } + + public function compilePrimary(Blueprint $blueprint, Fluent $command) + { + return sprintf( + 'ALTER TABLE %s ADD CONSTRAINT %s PRIMARY KEY (%s)', + $this->wrapTable($blueprint), + $this->wrapValue($command->index), + $this->columnize($command->columns) + ); + } + + protected function typeMediumText(Fluent $column) + { + return 'CLOB(16M)'; + } + + public function compileUnique(Blueprint $blueprint, Fluent $command) + { + return sprintf( + 'CREATE UNIQUE INDEX %s ON %s (%s)', + $this->wrapIndex($blueprint, $command->index), + $this->wrapTable($blueprint), + $this->columnize($command->columns) + ); + } + + public function compileIndex(Blueprint $blueprint, Fluent $command) + { + return sprintf( + 'CREATE INDEX %s ON %s (%s)', + $this->wrapIndex($blueprint, $command->index), + $this->wrapTable($blueprint), + $this->columnize($command->columns) + ); + } + + protected function typeJson(\Illuminate\Database\Schema\ColumnDefinition $column) + { + return 'CLOB(1M)'; + } + + protected function typeJsonb(\Illuminate\Database\Schema\ColumnDefinition $column) + { + return 'CLOB(1M)'; + } + + protected function typeTinyText(\Illuminate\Database\Schema\ColumnDefinition $column) + { + return 'VARCHAR(255)'; + } + + public function compileInsertOrIgnore( + \Illuminate\Database\Query\Builder $query, + array $values + ) { + if (empty($values)) { + return ''; + } + + if (!is_array(reset($values))) { + $values = [$values]; + } + + $columns = array_keys(reset($values)); + + $table = $this->wrapTable($query->from); + + $sourceColumns = implode( + ', ', + array_map(fn ($column) => $this->wrap($column), $columns) + ); + + $rows = []; + + foreach ($values as $row) { + $rows[] = '(' . implode( + ', ', + array_fill(0, count($columns), '?') + ) . ')'; + } + + $source = implode(', ', $rows); + + $conditions = []; + + foreach ($columns as $column) { + $wrapped = $this->wrap($column); + + $conditions[] = "target.$wrapped = source.$wrapped"; + } + + return sprintf( + 'MERGE INTO %s AS target + USING (VALUES %s) AS source (%s) + ON %s + WHEN NOT MATCHED THEN + INSERT (%s) + VALUES (%s)', + $table, + $source, + $sourceColumns, + implode(' AND ', $conditions), + $sourceColumns, + implode( + ', ', + array_map( + fn ($column) => 'source.' . $this->wrap($column), + $columns + ) + ) + ); + } +} diff --git a/tests/Feature/Db2/ReleaseCompatibilityTest.php b/tests/Feature/Db2/ReleaseCompatibilityTest.php new file mode 100644 index 0000000..c0e518b --- /dev/null +++ b/tests/Feature/Db2/ReleaseCompatibilityTest.php @@ -0,0 +1,66 @@ +db(); + $namespace = 'STAGE8_'.strtoupper(bin2hex(random_bytes(6))); + $connection = 'db2_release_test'; + $previous = DB::getDefaultConnection(); + $admin->statement('CREATE SCHEMA '.$namespace); + + try { + config(['database.connections.'.$connection => array_replace( + config('database.connections.db2_local'), + ['schema' => $namespace] + )]); + DB::setDefaultConnection($connection); + $this->artisan('migrate', ['--database' => $connection, '--force' => true])->assertSuccessful(); + + $db = DB::connection($connection); + $schema = $db->getSchemaBuilder(); + $this->assertTrue($schema->hasColumn('users', 'two_factor_confirmed_at')); + + $user = User::factory()->create(); + $db->table('passkeys')->insert([ + 'user_id' => $user->getKey(), + 'name' => 'Phase 8 passkey', + 'credential_id' => 'phase8-fixture', + 'credential' => '{}', + 'created_at' => '2026-11-11 08:47:26.123456', + 'last_used_at' => '2026-11-11 09:47:26.654321', + ]); + + $this->actingAs($user) + ->withSession(['auth.password_confirmed_at' => time()]) + ->get(route('security.edit')) + ->assertOk() + ->assertSee('Phase 8 passkey'); + + $columns = array_column($schema->getColumns('failed_jobs'), null, 'name'); + $this->assertStringContainsString('CURRENT', strtoupper((string) $columns['failed_at']['default'])); + + $this->artisan('migrate:rollback', ['--database' => $connection, '--force' => true])->assertSuccessful(); + $this->assertSame(['migrations'], array_column($schema->getTables(), 'name')); + $this->assertSame(0, (int) $db->table('migrations')->count()); + } finally { + DB::setDefaultConnection($previous); + DB::purge($connection); + + $schema = $admin->getSchemaBuilder(); + foreach ($schema->getTables($namespace) as $table) { + $admin->statement('DROP TABLE '.$table['schema_qualified_name']); + } + $admin->statement('DROP SCHEMA '.$namespace.' RESTRICT'); + } + } +} diff --git a/tests/Feature/Db2/ReleaseCompatibilityTest.php.bak-stage08-20261111-111244 b/tests/Feature/Db2/ReleaseCompatibilityTest.php.bak-stage08-20261111-111244 new file mode 100644 index 0000000..fc0d2b3 --- /dev/null +++ b/tests/Feature/Db2/ReleaseCompatibilityTest.php.bak-stage08-20261111-111244 @@ -0,0 +1,63 @@ +db(); + $namespace = 'STAGE8_'.strtoupper(bin2hex(random_bytes(6))); + $connection = 'db2_release_test'; + $previous = DB::getDefaultConnection(); + $admin->statement('CREATE SCHEMA '.$namespace); + + try { + config(['database.connections.'.$connection => array_replace( + config('database.connections.db2_local'), + ['schema' => $namespace] + )]); + DB::setDefaultConnection($connection); + $this->artisan('migrate', ['--database' => $connection, '--force' => true])->assertSuccessful(); + + $db = DB::connection($connection); + $schema = $db->getSchemaBuilder(); + $this->assertTrue($schema->hasColumn('users', 'two_factor_confirmed_at')); + + $user = User::factory()->create(); + $db->table('passkeys')->insert([ + 'user_id' => $user->getKey(), + 'name' => 'Phase 8 passkey', + 'credential_id' => 'phase8-fixture', + 'credential' => '{}', + 'created_at' => '2026-11-11 08:47:26.123456', + 'last_used_at' => '2026-11-11 09:47:26.654321', + ]); + + $this->actingAs($user) + ->withSession(['auth.password_confirmed_at' => time()]) + ->get(route('security.edit')) + ->assertOk() + ->assertSee('Phase 8 passkey'); + + $columns = array_column($schema->getColumns('failed_jobs'), null, 'name'); + $this->assertStringContainsString('CURRENT', strtoupper((string) $columns['failed_at']['default'])); + + $this->artisan('migrate:rollback', ['--database' => $connection, '--force' => true])->assertSuccessful(); + $this->assertSame(['migrations'], array_column($schema->getTables(), 'name')); + $this->assertSame(0, (int) $db->table('migrations')->count()); + } finally { + DB::setDefaultConnection($previous); + DB::purge($connection); + + $schema = $admin->getSchemaBuilder(); + foreach ($schema->getTables($namespace) as $table) { + $schema->drop($table['schema_qualified_name']); + } + $admin->statement('DROP SCHEMA '.$namespace.' RESTRICT'); + } + } +} diff --git a/tests/Feature/Db2/ReleaseCompatibilityTest.php.bak-stage08-20261111-111313 b/tests/Feature/Db2/ReleaseCompatibilityTest.php.bak-stage08-20261111-111313 new file mode 100644 index 0000000..9a4baf7 --- /dev/null +++ b/tests/Feature/Db2/ReleaseCompatibilityTest.php.bak-stage08-20261111-111313 @@ -0,0 +1,63 @@ +db(); + $namespace = 'STAGE8_'.strtoupper(bin2hex(random_bytes(6))); + $connection = 'db2_release_test'; + $previous = DB::getDefaultConnection(); + $admin->statement('CREATE SCHEMA '.$namespace); + + try { + config(['database.connections.'.$connection => array_replace( + config('database.connections.db2_local'), + ['schema' => $namespace] + )]); + DB::setDefaultConnection($connection); + $this->artisan('migrate', ['--database' => $connection, '--force' => true])->assertSuccessful(); + + $db = DB::connection($connection); + $schema = $db->getSchemaBuilder(); + $this->assertTrue($schema->hasColumn('users', 'two_factor_confirmed_at')); + + $user = User::factory()->create(); + $db->table('passkeys')->insert([ + 'user_id' => $user->getKey(), + 'name' => 'Phase 8 passkey', + 'credential_id' => 'phase8-fixture', + 'credential' => '{}', + 'created_at' => '2026-11-11 08:47:26.123456', + 'last_used_at' => '2026-11-11 09:47:26.654321', + ]); + + $this->actingAs($user) + ->withSession(['auth.password_confirmed_at' => time()]) + ->get(route('security.edit')) + ->assertOk() + ->assertSee('Phase 8 passkey'); + + $columns = array_column($schema->getColumns('failed_jobs'), null, 'name'); + $this->assertStringContainsString('CURRENT', strtoupper((string) $columns['failed_at']['default'])); + + $this->artisan('migrate:rollback', ['--database' => $connection, '--force' => true])->assertSuccessful(); + $this->assertSame(['migrations'], array_column($schema->getTables(), 'name')); + $this->assertSame(0, (int) $db->table('migrations')->count()); + } finally { + DB::setDefaultConnection($previous); + DB::purge($connection); + + $schema = $admin->getSchemaBuilder(); + foreach ($schema->getTables($namespace) as $table) { + $admin->statement('DROP TABLE '.$table['schema_qualified_name']); + } + $admin->statement('DROP SCHEMA '.$namespace.' RESTRICT'); + } + } +}