diff --git a/src/MySQLReplication/Config/Config.php b/src/MySQLReplication/Config/Config.php index e37e65f..8542531 100644 --- a/src/MySQLReplication/Config/Config.php +++ b/src/MySQLReplication/Config/Config.php @@ -27,6 +27,8 @@ public function __construct( public array $custom, public float $heartbeatPeriod, public string $slaveUuid, + private array $tablesRegex = [], + private array $databasesRegex = [], ) { } @@ -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 diff --git a/src/MySQLReplication/Config/ConfigBuilder.php b/src/MySQLReplication/Config/ConfigBuilder.php index e86e9d9..10ac290 100644 --- a/src/MySQLReplication/Config/ConfigBuilder.php +++ b/src/MySQLReplication/Config/ConfigBuilder.php @@ -32,6 +32,10 @@ class ConfigBuilder private array $databasesOnly = []; + private array $tablesRegex = []; + + private array $databasesRegex = []; + private string $mariaDbGtid = ''; private int $tableCacheSize = 128; @@ -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; @@ -194,7 +210,9 @@ public function build(): Config $this->tableCacheSize, $this->custom, $this->heartbeatPeriod, - $this->slaveUuid + $this->slaveUuid, + $this->tablesRegex, + $this->databasesRegex, ); } } diff --git a/tests/Unit/Config/ConfigTest.php b/tests/Unit/Config/ConfigTest.php index 9e137b1..6f3924f 100644 --- a/tests/Unit/Config/ConfigTest.php +++ b/tests/Unit/Config/ConfigTest.php @@ -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 @@ -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