# 'bundle exec rake db:migrate' taking a long time due to calendar migration

**URL:** <https://meta.discourse.org/t/bundle-exec-rake-db-migrate-taking-a-long-time-due-to-calendar-migration/287434>\
**Category:** Self-hosting\
**Created:** [December 4, 2023, 1:58pm UTC](https://meta.discourse.org/t/bundle-exec-rake-db-migrate-taking-a-long-time-due-to-calendar-migration/287434 "2023-12-04T13:58:11Z")\
**Posts on this page:** 1\
**Showing post:** 32

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [December 5, 2023, 3:28pm UTC](https://meta.discourse.org/t/bundle-exec-rake-db-migrate-taking-a-long-time-due-to-calendar-migration/287434/32 "2023-12-05T15:28:46Z")

</div>

```plaintext
      DELETE
        FROM calendar_events ce
      WHERE
        ce.id IN (SELECT DISTINCT(ce3.id) FROM calendar_events ce2
                  LEFT JOIN calendar_events ce3 ON ce3.user_id = ce2.user_id AND ce3.description = ce2.description
                  WHERE ce2.start_date >= (ce3.start_date - INTERVAL '1 days')
                    AND ce2.start_date <= (ce3.start_date + INTERVAL '1 days')
                    AND ce2.timezone IS NOT NULL
                    AND ce3.timezone IS NULL
                    AND ce3.id != ce2.id
                    AND ce2.post_id IS NULL
                    AND ce3.post_id IS NULL
        )
    SQL

```

So that looks like at least an `O(n^2)` operation, right? There are 69,724,384 rows in that table, so that doesn’t look like a very good idea.

@Sikamikanico. If you want the calednar plugin back, I think the thing to do is to just delete all calendar events and you can start over. The other thing we could do would be to run that query on the active server and see if it ever finishes.

The root cause, I think, is a bug in the plugin that duplicated events when a timezone changed.

The calendar\_events table is 11GB:

```plaintext
discourse=# SELECT pg_size_pretty( pg_total_relation_size('calendar_events') );
 pg_size_pretty 
----------------
 11 GB
(1 row)

```

Here are the biggest tables:

```plaintext
       relation | total_size 
----------------------+------------
 calendar_events | 11 GB
 post_timings | 6884 MB
 posts | 2292 MB
 user_auth_token_logs | 1240 MB
 user_actions | 1055 MB
(5 rows)

```

Only `calendar_events` is clearly bizarre.

---

_[View the full topic](https://meta.discourse.org/t/bundle-exec-rake-db-migrate-taking-a-long-time-due-to-calendar-migration/287434)._
