Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Requires `nowo-tech/transaction-doctrine` ^1.8.1 (fixes GHSA-9wh3-m3m5-jffx and GHSA-65w2-9r4h-848g).

### Fixed

- **Foreign keys kept when collating the identifier columns (audit XI, latent).** `ExactColumnsCollationListener`
(1.20.1) replaced the entry table on `postGenerateSchemaTable` (`setClassTable()`, DBAL 4.5+); ORM 3.7–3.8 then added
the table's foreign keys to the object it had replaced, so any foreign key starting on the entry table, and its index,
would have been missing from the generated schema on MySQL / MariaDB. The entry table has none today, so no schema was
affected. The listener now runs on `postGenerateSchema`, once every foreign key exists: on DBAL 4.5+ the table is
rebuilt with its editor (foreign keys and indexes included) and swapped in with `GenerateSchemaEventArgs::setSchema()`;
on older DBAL the column options are set in place. The platform is the one of the schema tool's entity manager, and a
manager that does not map the entry is left alone. Same collations and fallback warning as before.
`postGenerateSchemaTable()` stays as a deprecated no-op, and the `ManagerRegistry` constructor argument is unused
(optional). Tests: a foreign key and an index on the entry table are kept, foreign keys and indexes with and without
the listener on MySQL 8.4, MySQL 8.0, MySQL 5.7, MariaDB and SQLite platforms, and the created tables on MySQL 8.4 /
MariaDB 11.4.
- The rollup tables (`nowo_http_log_daily`, `nowo_http_log_event_seen`) get their primary key through
`Table::addPrimaryKeyConstraint()` where doctrine/dbal has it (4.3+), with `setPrimaryKey()` kept for older DBAL, so
building the schema no longer raises the `Table::setPrimaryKey()` deprecation (doctrine/dbal#6867). Same key. Test:
no doctrine/dbal#6867 deprecation while the tables are added.

## [1.23.0] - 2026-11-19

### Changed
Expand Down
59 changes: 48 additions & 11 deletions src/Doctrine/ExactColumnsCollationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Nowo\HttpLogBundle\Doctrine;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs;
use Doctrine\Persistence\ManagerRegistry;
use Nowo\HttpLogBundle\Entity\HttpLogEntry;
Expand All @@ -22,40 +24,75 @@
* filters and the indexes do not fold `Bob`, `bób` or `bob ` onto `bob` either. Applied to the generated schema (schema
* tool, `doctrine:migrations:diff`); existing tables need the `ALTER TABLE` of UPGRADING.
*
* Stateless (REQ-RUNTIME-002): the platform is read from the registry on each event.
* 1.23.1 (audit XI): applied on `postGenerateSchema`, once the schema tool has added every foreign key, by table name.
* 1.20.1–1.23.0 replaced the entry table on `postGenerateSchemaTable` (`setClassTable()`, DBAL 4.5+), and ORM 3.7–3.8
* then added the table's foreign keys to the object it had replaced, so any foreign key starting on it (and its index)
* was lost on MySQL / MariaDB (none exist today: latent). Now:
*
* - DBAL 4.5+: the table is rebuilt with its table editor (foreign keys and indexes included) and swapped into a new
* schema given back with `GenerateSchemaEventArgs::setSchema()`;
* - older DBAL: the column's platform options are set in place (no editor exists there).
*
* Stateless (REQ-RUNTIME-002): the platform is the one of the schema tool's entity manager, read on each event; a
* manager that does not map the entry is left alone.
*/
final readonly class ExactColumnsCollationListener
{
/** Columns of the entry table that hold an identifier (those that are mapped get the collation). */
public const COLUMNS = ['user_identifier', 'client_ip', 'provider', 'credential_kind', 'credential_id', 'tenant_key', 'project_key'];

/**
* @param ManagerRegistry|null $doctrine unused since 1.23.1 (the platform comes from the schema tool's entity
* manager); kept for BC
*/
public function __construct(
private ManagerRegistry $doctrine,
?ManagerRegistry $doctrine = null,
private ?LoggerInterface $logger = null,
) {
unset($doctrine);
}

public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $args): void
/**
* Gives the identifier columns of the entry table their exact-match collation in the generated schema, keeping every
* foreign key and index of the table.
*/
public function postGenerateSchema(GenerateSchemaEventArgs $args): void
{
if ($args->getClassMetadata()->getName() !== HttpLogEntry::class) {
$manager = $args->getEntityManager();
if ($manager->getMetadataFactory()->isTransient(HttpLogEntry::class)) {
return;
}
$schema = $args->getSchema();
$name = $manager->getClassMetadata(HttpLogEntry::class)->getTableName();
if (!$schema->hasTable($name)) {
return;
}
$manager = $this->doctrine->getManagerForClass(HttpLogEntry::class);
$collation = $manager instanceof EntityManagerInterface ? ExactCollation::forConnection($manager->getConnection(), $this->logger) : null;
$collation = ExactCollation::forConnection($manager->getConnection(), $this->logger);
if ($collation === null) {
return;
}

$original = $table = $args->getClassTable();
// @phpstan-ignore function.alreadyNarrowedType, function.alreadyNarrowedType (Table::edit() from doctrine/dbal 4.5, setClassTable() from doctrine/orm 3.6)
$editor = method_exists($table, 'edit') && method_exists($args, 'setClassTable');
$original = $table = $schema->getTable($name);
// Table::edit() and Schema::edit() exist only from doctrine/dbal 4.5.
// @phpstan-ignore function.alreadyNarrowedType, function.alreadyNarrowedType
$editor = method_exists(Table::class, 'edit') && method_exists(Schema::class, 'edit');
foreach (self::COLUMNS as $column) {
if ($table->hasColumn($column)) {
$table = ExactCollation::collate($table, $column, $collation, $editor) ?? $table;
}
}
if ($table !== $original) {
$args->setClassTable($table);
$args->setSchema($schema->edit()->dropTable($original->getObjectName())->addTable($table)->create());
}
}

/**
* No longer subscribed (1.23.1, audit XI): replacing the table there lost its foreign keys. Kept for BC; does nothing.
*
* @deprecated since 1.23.1, the collation is applied on postGenerateSchema ({@see self::postGenerateSchema()})
*/
public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $args): void
{
unset($args);
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ services:
arguments:
$logger: '@?logger'
tags:
- { name: doctrine.event_listener, event: postGenerateSchemaTable }
- { name: doctrine.event_listener, event: postGenerateSchema }
- { name: monolog.logger, channel: nowo_http_log }

# REQ-ORM-002: removed by the extension when doctrine.table_prefix and doctrine.table_name_prefix keep their defaults.
Expand Down
29 changes: 27 additions & 2 deletions src/Rollup/RollupTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

namespace Nowo\HttpLogBundle\Rollup;

use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Types;
use Nowo\HttpLogBundle\Doctrine\ExactCollation;
use Nowo\HttpLogBundle\Doctrine\TablePrefixListener;

use function is_string;
use function method_exists;

/**
* Names and DBAL schema of the two rollup tables (HL-1 §8.3): `<table_prefix><table_name_prefix>daily` (one bucket
Expand Down Expand Up @@ -117,7 +120,7 @@ public function addTo(Schema $schema, bool|string $mysql = false): void
$daily->addColumn('duration_ms_max', Types::INTEGER, ['default' => 0]);
$daily->addColumn('first_seen_at', Types::DATETIME_MUTABLE);
$daily->addColumn('last_seen_at', Types::DATETIME_MUTABLE);
$daily->setPrimaryKey(['id']);
$this->primaryKey($daily, 'id');
$daily->addUniqueIndex(self::KEY_COLUMNS, $this->index(self::UNIQUE_KEY));
foreach (self::DAILY_INDEXES as $name => $columns) {
$daily->addIndex($columns, $this->index($name));
Expand All @@ -128,8 +131,30 @@ public function addTo(Schema $schema, bool|string $mysql = false): void
$seen = $schema->createTable($this->eventSeen);
$seen->addColumn('event_id', Types::STRING, ['length' => 32, 'fixed' => true]);
$seen->addColumn('day', Types::DATE_MUTABLE);
$seen->setPrimaryKey(['event_id']);
$this->primaryKey($seen, 'event_id');
$seen->addIndex(['day'], $this->index(self::SEEN_INDEX));
}
}

/**
* Sets the primary key of `$table` on `$column`: through a primary key constraint where DBAL has one (4.3+;
* `Table::setPrimaryKey()` is deprecated there, doctrine/dbal#6867), else with `setPrimaryKey()` (older DBAL). Same
* key either way (`primary`, clustered).
*
* @param non-empty-string $column
*/
private function primaryKey(Table $table, string $column): void
{
// @phpstan-ignore function.alreadyNarrowedType (Table::addPrimaryKeyConstraint() exists only from doctrine/dbal 4.3)
if (method_exists($table, 'addPrimaryKeyConstraint')) {
$table->addPrimaryKeyConstraint(PrimaryKeyConstraint::editor()->setUnquotedColumnNames($column)->create());

return;
}

// doctrine/dbal < 4.3
// @codeCoverageIgnoreStart
$table->setPrimaryKey([$column]);
// @codeCoverageIgnoreEnd
}
}
Loading
Loading