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

Add migration to remove duplicate Customers & Subscriptions #12100

Closed
allouis opened this issue Aug 3, 2020 · 3 comments
Closed

Add migration to remove duplicate Customers & Subscriptions #12100

allouis opened this issue Aug 3, 2020 · 3 comments
Assignees

Comments

@allouis
Copy link
Contributor

allouis commented Aug 3, 2020

We want to add a unique constraint to the stripe id columns, but we have seen instances of duplicate entries, which would cause the constraint to fail - we should first run a migration to clean these up so that we can be certain the unique constraint migration will work.

estimated 1 day of work

@allouis allouis self-assigned this Aug 3, 2020
allouis added a commit to allouis/Ghost that referenced this issue Aug 4, 2020
refs TryGhost#12100

We want to add unique constraints to the subscription_id and customer_id
fields in the members_stripe_customers_subscriptions and
members_stripe_customers tables respectively. In order to do this safely
we must first ensure that no duplicate entries exist.
@allouis
Copy link
Contributor Author

allouis commented Aug 5, 2020

As we are adding foreign key constraints as well as unique constraints, we must also ensure the database is in the correct state to accept that, which means no orphaned records in the members_stripe_customers or members_stripe_customers_subscriptions table. The PR linked to this issue has been updated to handle that.

@allouis
Copy link
Contributor Author

allouis commented Aug 5, 2020

Also - as the migrations we are writing to add the new constraints is only being run for MySQL databases, the cleanup migrations will also only be run for MySQL!

allouis added a commit that referenced this issue Aug 6, 2020
refs #12100

For performance reasons we want to add foreign key and unique constraints
to the members_stripe_* tables so we can utilised cascading deletes and 
joins across the tables when querying.

In order to do this we must first ensure that:
- There are no duplicate entries in the `subscription_id` or `customer_id` columns
- There are no orphaned rows in the subscription or customers tables

If the first is not true, the unique constraint will fail, and if the second is not true,
the foreign key constraint will fail.

As we are only adding the indexes to existing MySQL databases at this point, the
cleanup migrations will also only be done for existing MySQL databases too.

The migrations for removing orphaned rows splits the deletion into a `SELECT`
followed by a `WHERE IN` to avoid the database "optimising" the query into a
`JOIN` which ends up taking much longer due to the lack of indexes.
@allouis
Copy link
Contributor Author

allouis commented Aug 6, 2020

Closed in d338497 🎉

@allouis allouis closed this as completed Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants