Moln / php-mysql-replication

Pure PHP Implementation of MySQL replication protocol. This allow you to receive event like insert, update, delete with their data and raw SQL queries.
MIT License
3 stars 3 forks source link

Regexp support checkTablesOnly gives unexpected results as it's not strict per default #17

Open cyppe opened 9 months ago

cyppe commented 9 months ago

Hello!

Not sure if it's just me. But I am using laravel-trigger library that is using your package. And I noticed I got events on tables not listed in my config for tablesOnly.

So i tried to just modify the code to output logs and realized it's due to the regexp not being strict as default.

So the modified version below with log output, gives this result:

Log: Table: af_products matches af_products_bike_cache

Code:

 private static function matchNames(string $subject, array $names): bool
    {
        foreach ($names as $name) {
            if (preg_match("/$name/", $subject)) {
                Log::info("Table: ". $name . " matches " . $subject);  // ADDED FOR DEBUGGING
                return true;
            }
        }

        return false;
    }

I have worked around the issue by defining strict regexp in my config for the laravel-trigger package.

From:

TRIGGER_TABLES=af_static,af_static_translate,af_brands,af_brands_images,ma_campaigns,ma_campaigns_products,af_categories,af_products_to_manufacturers_models_years,af_manufacturers_models,af_manufacturers_models_years,af_images,af_manufacturers,language,af_product_package,af_products,af_properties,af_properties_option,trustpilot_product_review_reviews_log,trustpilot_product_review_reviews_summary,af_storage,af_storage_templates,af_translation_new,af_products_images_to_products

To: TRIGGER_TABLES=^af_static$,^af_static_translate$,^af_brands$,^af_brands_images$,^ma_campaigns$,^ma_campaigns_products$,^af_categories$,^af_products_to_manufacturers_models_years$,^af_manufacturers_models$,^af_manufacturers_models_years$,^af_images$,^af_manufacturers$,^language$,^af_product_package$,^af_products$,^af_properties$,^af_properties_option$,^trustpilot_product_review_reviews_log$,^trustpilot_product_review_reviews_summary$,^af_storage$,^af_storage_templates$,^af_translation_new$,^af_products_images_to_products$

But maybe it should be strict per default if there is no regexp pattern in the string?

Again - my issue is resolved but it was a bit surprise so maybe you can consider doing something.

cyppe commented 9 months ago

Worth noting is that also database name is passing through the same method, so if you have for example two databases with names :

per default it will also listen on company_backup if you define "company" as databasesOnly and forget to set it as ^company$

Probably it's a bit more rare case, but just fyi. At least good to document it.