Skip to content

Commit 3c365e8

Browse files
authored
Add regular expression matching support for checkDatabasesOnly or checkTablesOnly (#129)
1 parent a60da9f commit 3c365e8

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/MySQLReplication/Config/Config.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public function __construct(
2727
public array $custom,
2828
public float $heartbeatPeriod,
2929
public string $slaveUuid,
30+
private array $tablesRegex = [],
31+
private array $databasesRegex = [],
3032
) {
3133
}
3234

@@ -103,13 +105,26 @@ public function validate(): void
103105

104106
public function checkDataBasesOnly(string $database): bool
105107
{
106-
return $this->databasesOnly !== [] && !in_array($database, $this->databasesOnly, true);
108+
return ($this->databasesOnly !== [] && !in_array($database, $this->databasesOnly, true))
109+
|| ($this->databasesRegex !== [] && !self::matchNames($database, $this->databasesRegex));
107110
}
108111

109112

110113
public function checkTablesOnly(string $table): bool
111114
{
112-
return $this->tablesOnly !== [] && !in_array($table, $this->tablesOnly, true);
115+
return ($this->tablesOnly !== [] && !in_array($table, $this->tablesOnly, true))
116+
|| ($this->tablesRegex !== [] && !self::matchNames($table, $this->tablesRegex));
117+
}
118+
119+
private static function matchNames(string $name, array $patterns): bool
120+
{
121+
foreach ($patterns as $pattern) {
122+
if (preg_match($pattern, $name)) {
123+
return true;
124+
}
125+
}
126+
127+
return false;
113128
}
114129

115130
public function checkEvent(int $type): bool

src/MySQLReplication/Config/ConfigBuilder.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class ConfigBuilder
3232

3333
private array $databasesOnly = [];
3434

35+
private array $tablesRegex = [];
36+
37+
private array $databasesRegex = [];
38+
3539
private string $mariaDbGtid = '';
3640

3741
private int $tableCacheSize = 128;
@@ -143,6 +147,18 @@ public function withDatabasesOnly(array $databasesOnly): self
143147
return $this;
144148
}
145149

150+
public function withDatabasesRegex(array $databasesRegex): self
151+
{
152+
$this->databasesRegex = $databasesRegex;
153+
return $this;
154+
}
155+
156+
public function withTablesRegex(array $tablesRegex): self
157+
{
158+
$this->tablesRegex = $tablesRegex;
159+
return $this;
160+
}
161+
146162
public function withMariaDbGtid(string $mariaDbGtid): self
147163
{
148164
$this->mariaDbGtid = $mariaDbGtid;
@@ -194,7 +210,9 @@ public function build(): Config
194210
$this->tableCacheSize,
195211
$this->custom,
196212
$this->heartbeatPeriod,
197-
$this->slaveUuid
213+
$this->slaveUuid,
214+
$this->tablesRegex,
215+
$this->databasesRegex,
198216
);
199217
}
200218
}

tests/Unit/Config/ConfigTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public function testShouldCheckDataBasesOnly(): void
9797

9898
$config = (new ConfigBuilder())->withDatabasesOnly(['foo'])->build();
9999
self::assertTrue($config->checkDataBasesOnly('bar'));
100+
101+
$config = (new ConfigBuilder())->withDatabasesRegex(['/^foo_.*/'])->build();
102+
self::assertFalse($config->checkDataBasesOnly('foo_123'));
100103
}
101104

102105
public function testShouldCheckTablesOnly(): void
@@ -112,6 +115,9 @@ public function testShouldCheckTablesOnly(): void
112115

113116
$config = (new ConfigBuilder())->withTablesOnly(['foo'])->build();
114117
self::assertTrue($config->checkTablesOnly('bar'));
118+
119+
$config = (new ConfigBuilder())->withTablesRegex(['/^foo_.*/'])->build();
120+
self::assertFalse($config->checkTablesOnly('foo_123'));
115121
}
116122

117123
public function testShouldCheckEvent(): void

0 commit comments

Comments
 (0)