Skip to content

Commit a3462d9

Browse files
authored
Merge pull request #6 from Moln/develop
Add match support for `DatabasesOnly` or `TablesOnly`
2 parents 4a06bc9 + aaab664 commit a3462d9

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Based on a great work of creators:https://github.com/noplay/python-mysql-repli
1414

1515
**Note:** Resolve these issues:
1616

17+
- Add regular expression matching support for `DatabasesOnly` or `TablesOnly` of `Config`.
1718
- Resolve [krowinski/php-mysql-replication#94](https://github.com/krowinski/php-mysql-replication/issues/94), change static config properties to non-static.
1819
- Add retry feature.
1920
```php

src/MySQLReplication/Config/Config.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function getTableCacheSize(): int
177177

178178
public function checkDataBasesOnly(string $database): bool
179179
{
180-
return [] !== $this->getDatabasesOnly() && !in_array($database, $this->getDatabasesOnly(), true);
180+
return [] !== $this->getDatabasesOnly() && !self::matchNames($database, $this->getDatabasesOnly());
181181
}
182182

183183
public function getDatabasesOnly(): array
@@ -187,7 +187,18 @@ public function getDatabasesOnly(): array
187187

188188
public function checkTablesOnly(string $table): bool
189189
{
190-
return [] !== $this->getTablesOnly() && !in_array($table, $this->getTablesOnly(), true);
190+
return [] !== $this->getTablesOnly() && !self::matchNames($table, $this->getTablesOnly());
191+
}
192+
193+
private static function matchNames(string $subject, array $names): bool
194+
{
195+
foreach ($names as $name) {
196+
if (preg_match("/$name/", $subject)) {
197+
return true;
198+
}
199+
}
200+
201+
return false;
191202
}
192203

193204
public function getTablesOnly(): array

tests/Unit/Config/ConfigTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ public function shouldCheckDataBasesOnly(): void
9393

9494
$config = (new ConfigBuilder())->withDatabasesOnly(['foo'])->build();
9595
self::assertTrue($config->checkDataBasesOnly('bar'));
96+
97+
$config = (new ConfigBuilder())->withDatabasesOnly(['foo_.*'])->build();
98+
self::assertFalse($config->checkDataBasesOnly('foo_123'));
9699
}
97100

98101
/**
@@ -111,6 +114,9 @@ public function shouldCheckTablesOnly(): void
111114

112115
$config = (new ConfigBuilder())->withTablesOnly(['foo'])->build();
113116
self::assertTrue($config->checkTablesOnly('bar'));
117+
118+
$config = (new ConfigBuilder())->withTablesOnly(['foo_.*'])->build();
119+
self::assertFalse($config->checkDataBasesOnly('foo_123'));
114120
}
115121

116122
/**

0 commit comments

Comments
 (0)