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: 17 additions & 2 deletions src/MySQLReplication/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function __construct(
public array $custom,
public float $heartbeatPeriod,
public string $slaveUuid,
private array $tablesRegex = [],
private array $databasesRegex = [],
) {
}

Expand Down Expand Up @@ -103,13 +105,26 @@ public function validate(): void

public function checkDataBasesOnly(string $database): bool
{
return $this->databasesOnly !== [] && !in_array($database, $this->databasesOnly, true);
return ($this->databasesOnly !== [] && !in_array($database, $this->databasesOnly, true))
|| ($this->databasesRegex !== [] && !self::matchNames($database, $this->databasesRegex));
}


public function checkTablesOnly(string $table): bool
{
return $this->tablesOnly !== [] && !in_array($table, $this->tablesOnly, true);
return ($this->tablesOnly !== [] && !in_array($table, $this->tablesOnly, true))
|| ($this->tablesRegex !== [] && !self::matchNames($table, $this->tablesRegex));
}

private static function matchNames(string $name, array $patterns): bool
{
foreach ($patterns as $pattern) {
if (preg_match($pattern, $name)) {
return true;
}
}

return false;
}

public function checkEvent(int $type): bool
Expand Down
20 changes: 19 additions & 1 deletion src/MySQLReplication/Config/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class ConfigBuilder

private array $databasesOnly = [];

private array $tablesRegex = [];

private array $databasesRegex = [];

private string $mariaDbGtid = '';

private int $tableCacheSize = 128;
Expand Down Expand Up @@ -143,6 +147,18 @@ public function withDatabasesOnly(array $databasesOnly): self
return $this;
}

public function withDatabasesRegex(array $databasesRegex): self
{
$this->databasesRegex = $databasesRegex;
return $this;
}

public function withTablesRegex(array $tablesRegex): self
{
$this->tablesRegex = $tablesRegex;
return $this;
}

public function withMariaDbGtid(string $mariaDbGtid): self
{
$this->mariaDbGtid = $mariaDbGtid;
Expand Down Expand Up @@ -194,7 +210,9 @@ public function build(): Config
$this->tableCacheSize,
$this->custom,
$this->heartbeatPeriod,
$this->slaveUuid
$this->slaveUuid,
$this->tablesRegex,
$this->databasesRegex,
);
}
}
6 changes: 6 additions & 0 deletions tests/Unit/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public function testShouldCheckDataBasesOnly(): void

$config = (new ConfigBuilder())->withDatabasesOnly(['foo'])->build();
self::assertTrue($config->checkDataBasesOnly('bar'));

$config = (new ConfigBuilder())->withDatabasesRegex(['/^foo_.*/'])->build();
self::assertFalse($config->checkDataBasesOnly('foo_123'));
}

public function testShouldCheckTablesOnly(): void
Expand All @@ -112,6 +115,9 @@ public function testShouldCheckTablesOnly(): void

$config = (new ConfigBuilder())->withTablesOnly(['foo'])->build();
self::assertTrue($config->checkTablesOnly('bar'));

$config = (new ConfigBuilder())->withTablesRegex(['/^foo_.*/'])->build();
self::assertFalse($config->checkTablesOnly('foo_123'));
}

public function testShouldCheckEvent(): void
Expand Down
Loading