Skip to content
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

Database error #145

Open
coreymckrill opened this issue May 19, 2015 · 8 comments
Open

Database error #145

coreymckrill opened this issue May 19, 2015 · 8 comments

Comments

@coreymckrill
Copy link

We are periodically getting errors like this in our logs on the Theme Foundry site:

May 19 08:57:36 ttf-prod php-error.log: [19-May-2015 15:57:36 UTC]
WordPress database error Duplicate entry '68398' for key 'PRIMARY' for query INSERT INTO `wp_memberful_mapping` ( `wp_user_id`, `member_id`, `last_sync_at` ) VALUES ( 11454, 68398, '1432051056' ) made by
require('wp-blog-header.php'),
require_once('wp-load.php'),
require_once('/var/www/thethemefoundry.com/releases/20150519144316/wp-config.php'),
require_once('/var/www/thethemefoundry.com/wp-config.php'), require_once('wp-settings.php'),
do_action('plugins_loaded'),
call_user_func_array,
memberful_wp_endpoint_filter,
Memberful_Wp_Endpoint_Webhook->process,
memberful_wp_sync_member_from_memberful,
memberful_wp_sync_member_account,
Memberful_User_Map->map,
Memberful_User_Map->ensure_mapping_is_correct,
Memberful_User_Mapping_Repository->create_mapping
@dstrojny dstrojny modified the milestone: 5-30-15 Fixes May 26, 2015
@robertuniqid
Copy link
Contributor

I'm not sure how this is related to the WP Plugin, these saves are happening on your server, right ?

@dstrojny
Copy link
Contributor

@coreymckrill Could you provide some more color on this? We're struggling to understand what part of the plugin might be causing this problem on our setup.

@coreymckrill
Copy link
Author

It was happening more frequently for a while, but the last time it happened was on May 19 (probably right before I created this issue). Our Papertrail logs only go back to the 13th, so I don't have any other examples right now.

My interpretation of the error message is that for some reason it is trying to add a Member ID to the wp_memberful_mapping table that already exists. Since the member_id field is a primary key for that table, it can't have duplicates. So somehow the plugin is failing to detect that the user account already exists before it tries to create a new one. Since it's happening so sporadically, my guess is that this is occurring when a user logs into the site, triggering the plugin's user sync function.

@robertuniqid
Copy link
Contributor

@coreymckrill

I can notice a function in there.
This is a short natural course of the issue

    public function map( $member, array $context = array() ) {
        $existing_user_with_members_email = get_user_by( 'email', $member->email );

        $mapping_from_member = $this->repository()->find_user_member_is_mapped_to( $member );
        $mapping_from_user   = $this->repository()->find_member_user_is_mapped_to( $existing_user_with_members_email );

       //......
   }
        $sql =
            'SELECT `mem`.`wp_user_id`, `mem`.`member_id` '.
            'FROM `'.Memberful_User_Mapping_Repository::table().'` AS `mem`'.
            'WHERE `mem`.`member_id` = %d';

File path : wp-content/plugins/memberful-wp/src/user/map.php

I think this is happening if that user had his email address changed... OR if there's 2 users with the same email address ?

@coreymckrill
Copy link
Author

Ok, we just had another instance of the error:

WordPress database error Duplicate entry '69126' for key 'PRIMARY' for query INSERT INTO `wp_memberful_mapping` ( `wp_user_id`, `member_id`, `last_sync_at` ) VALUES ( 11509, 69126, '1432799242' )

The rest of the call stack is the same as above. I looked up the 69126 user ID in TTF's Memberful instance, and it is a new customer who just purchased their first item around the same time that the error occurred. In the WP database, the user_registered field contains roughly the same date/time that the purchase was made, so the user account was not already there.

Perhaps the map() function is somehow getting called twice in a row in some instances?

I'm not sure where this gets us, but it's more information to add to the puzzle.

@robertuniqid
Copy link
Contributor

Is the error itself a blocker ? Or more like an notice, looking into solutions right now, and want to figure exactly what happens. @coreymckrill

@coreymckrill
Copy link
Author

It's a PHP error, but it's clearly not crashing our site. It may be terminating the active request, but I don't know of a good way to test that. The user data is also successfully making it into the database (perhaps because it was inserted without error before the request that throws the error). We haven't received any customer reports of things crashing out on them. So it may be that this is a fairly inconsequential issue...

Here's something interesting: the function that is actually throwing the error mentions that this might happen because of a race condition:

https://github.com/memberful/memberful-wp/blob/develop/src/user/map.php#L390-L417

@dstrojny dstrojny modified the milestone: 5-30-15 Fixes Jun 3, 2015
@dstrojny
Copy link
Contributor

This is related to #153, and it should referenced as such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants