# Migration Issue Around De-duping Poll Votes

**URL:** <https://meta.discourse.org/t/migration-issue-around-de-duping-poll-votes/269653>\
**Category:** Bug\
**Tags:** polls\
**Created:** [June 26, 2023, 3:38pm UTC](https://meta.discourse.org/t/migration-issue-around-de-duping-poll-votes/269653 "2023-06-26T15:38:25Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![sweetbeems](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@sweetbeems](https://meta.discourse.org/u/sweetbeems)\
**Post date:** [June 26, 2023, 3:38pm UTC](https://meta.discourse.org/t/migration-issue-around-de-duping-poll-votes/269653/1 "2023-06-26T15:38:26Z")

</div>

We [noticed an issue](https://forums.learnnatively.com/t/light-novel-book-club-starting-on-june-26/3893/123?u=brandon) where our polls where we allowed multiple votes seemed to dedupe recently and another [forum site](https://community.wanikani.com/t/now-voting-absolute-beginners-book-club-now-reading-the-wolf-of-the-small-forest/34698/1793?u=sweetbeems) also experienced the issue. I briefly took a look at this [recent migration](https://github.com/discourse/discourse/blob/main/plugins/poll/db/migrate/20230614041219_delete_duplicate_poll_votes.rb) and I can’t help but think it deduped all poll votes:

```plaintext
class DeleteDuplicatePollVotes < ActiveRecord::Migration[7.0]
  def up
    execute <<~SQL
      DELETE FROM poll_votes
      WHERE (poll_id, user_id, updated_at) NOT IN (
        SELECT poll_id, user_id, MAX(updated_at)
        FROM poll_votes
        GROUP BY poll_id, user_id
      )
    SQL
  end

  def down
    raise ActiveRecord::IrreversibleMigration
  end
end

```

I’ll do more investigation tomorrow (it’s late here), but since the sql seemed pretty clear to wipe all duplicates, just wanted to note. I did confirm I have that migration file in my docker app, so I’m assuming it ran. Would love second opinions on this.

**UPDATE**

So, I’ve confirmed that there are no multi votes that were created before I last updated to 3.1.0.beta5, indicating that it was most likely the update that wiped the multi-votes. You can check for multi-votes yourself by entering the discourse app and running the query below.

```plaintext
# enter discourse app
sudo ./launcher enter app

# enter postgres console
sudo -u postgres psql discourse

```

Query for multi votes ordered by creation date:

```plaintext
SELECT poll_id, user_id, created_at
FROM poll_votes
WHERE (poll_id, user_id, created_at) NOT IN (
  SELECT poll_id, user_id, MAX(created_at)
  FROM poll_votes
  GROUP BY poll_id, user_id
)
ORDER BY created_at;

```

You should see that the oldest created\_at occurred after the last time you updated.

> **Aside - Should I not have this migration file?**
>
> I’m a little confused how I have this migration file… it appears to have been [committed](https://github.com/discourse/discourse/commit/fcaefc9f2f671a508ce1de398a726740ee4268b6) after beta5? How would I have this file?
> 
> I did confirm the migration ran by checking the schema\_migrations\_details table in postgres:
> 
> ```plaintext
> SELECT name, created_at 
> FROM schema_migration_details 
> WHERE name = 'DeleteDuplicatePollVotes';
> 
> ```

In conclusion, I’m very confident there’s a bug here and that we should remove that migration file before others upgrade. I’m not very experienced in rails or discourse, so I’m not sure how to resolve the issue or if we could retrieve those past votes, but it’s a been a bit of a bummer for our users who lost those votes.

---

_[View the full topic](https://meta.discourse.org/t/migration-issue-around-de-duping-poll-votes/269653)._
