Skip to content

Add match support for DatabasesOnly or TablesOnly #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2022
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Based on a great work of creators:https://github.com/noplay/python-mysql-repli

**Note:** Resolve these issues:

- Add regular expression matching support for `DatabasesOnly` or `TablesOnly` of `Config`.
- Resolve [krowinski/php-mysql-replication#94](https://github.com/krowinski/php-mysql-replication/issues/94), change static config properties to non-static.
- Add retry feature.
```php
Expand Down
15 changes: 13 additions & 2 deletions src/MySQLReplication/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function getTableCacheSize(): int

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

public function getDatabasesOnly(): array
Expand All @@ -187,7 +187,18 @@ public function getDatabasesOnly(): array

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

private static function matchNames(string $subject, array $names): bool
{
foreach ($names as $name) {
if (preg_match("/$name/", $subject)) {
return true;
}
}

return false;
}

public function getTablesOnly(): array
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public function shouldCheckDataBasesOnly(): void

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

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

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

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

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

/**
Expand Down