# Issues migrating test database

**URL:** https://meta.discourse.org/t/issues-migrating-test-database/135876
**Category:** Development
**Created:** [December 12, 2019, 9:01pm UTC](https://meta.discourse.org/t/issues-migrating-test-database/135876 "2019-12-12T21:01:31Z")
**Posts on this page:** 9
**Page:** 1

<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 12, 2019, 9:01pm UTC](https://meta.discourse.org/t/issues-migrating-test-database/135876/1 "2019-12-12T21:01:31Z")

</div>

Thanks, @Mittineague!

After a while, that made sense. I even wrote a spec, but even before I added my spec (and when I reverted to before I added any code), specs fail because:

```plaintext
An error occurred while loading ./plugins/discourse-prometheus/spec/middleware/metrics_spec.rb.
Failure/Error: Group.find_by(id: id)

ActiveRecord::StatementInvalid:
  PG::UndefinedTable: ERROR: relation "groups" does not exist
  LINE 8: WHERE a.attrelid = '"groups"'::regclass
                              ^
# (eval):24:in `async_exec'
# (eval):24:in `async_exec'
# ./app/models/group.rb:529:in `lookup_group'
# ./app/models/group.rb:513:in `block in ensure_automatic_groups!'
# ./app/models/group.rb:512:in `each_key'
# ./app/models/group.rb:512:in `ensure_automatic_groups!'
# (eval):3:in `block (2 levels) in run_file'
# ./spec/rails_helper.rb:79:in `<top (required)>'
# ./plugins/discourse-prometheus/spec/middleware/metrics_spec.rb:3:in `require'
# ./plugins/discourse-prometheus/spec/middleware/metrics_spec.rb:3:in `<top (required)>'
# ------------------
# --- Caused by: ---
# PG::UndefinedTable:
# ERROR: relation "groups" does not exist
# LINE 8: WHERE a.attrelid = '"groups"'::regclass
# ^
# (eval):24:in `async_exec'
No examples found.

```

That seems to hit for nearly every spec in the site. I think that I properly did `git reset --hard upstream/master` to get to what should be working and then deleted and re-created the database but I’m still getting that error.

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [December 13, 2019, 1:35am UTC](https://meta.discourse.org/t/issues-migrating-test-database/135876/2 "2019-12-13T01:35:26Z")

</div>

I admit I’m not the best at reading error messages. I looked at what file I thought they were referencing but didn’t see any “group(s)” line.

If this is your own, it might be something I struggle with some. ie. _When pluralization means more than one or when it is convention_

That is, you may be thinking “group_s_” when the syntax wants “group” without the “s”.

Similar, sometimes code wants specific case eg. “Group” vs. “group”.

I would like to say I know, but I don’t. I suggest “monkey see, monkey do” coding unless anyone that knows better chimes in.

---

<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 13, 2019, 12:14pm UTC](https://meta.discourse.org/t/issues-migrating-test-database/135876/3 "2019-12-13T12:14:02Z")

</div>

I didn’t do anything with groups (group doesn’t appear on my plugin) and those specs fail all over discourse. I’ll do one more clone of the master to see if somehow my fork. Thanks again!

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [December 13, 2019, 12:18pm UTC](https://meta.discourse.org/t/issues-migrating-test-database/135876/4 "2019-12-13T12:18:07Z")

</div>

Looks like you need to migrate your test database

```plaintext
RAILS_ENV=test bin/rake db:migrate

```

If that doesn’t work, scrap it and start again

```plaintext
RAILS_ENV=test bin/rake db:drop
RAILS_ENV=test bin/rake db:create
RAILS_ENV=test bin/rake db:migrate

```

Make double sure you put `RAILS_ENV=test`, otherwise you’ll accidentally delete your development database (I managed to do that by accident this week 🤦‍♂️)

---

<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 13, 2019, 12:20pm UTC](https://meta.discourse.org/t/issues-migrating-test-database/135876/5 "2019-12-13T12:20:31Z")

</div>

> [@david](#):
>
> Make double sure you put `RAILS_ENV=test`

Maybe that’s my problem! Maybe I dropped development and not test. Thanks. Ooh, and I need to install a fresh development environment in my laptop before I head to the airport.

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [December 13, 2019, 12:53pm UTC](https://meta.discourse.org/t/issues-migrating-test-database/135876/6 "2019-12-13T12:53:29Z")

</div>

I just added a check for pending migrations at the beginning of the spec suite, so it should be easier to identify this kind of issue in future

[https://github.com/discourse/discourse/commit/f47909a2b6cfec50e3fdd5027b284621c5f0a6cc](https://github.com/discourse/discourse/commit/f47909a2b6cfec50e3fdd5027b284621c5f0a6cc)

---

<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 13, 2019, 5:10pm UTC](https://meta.discourse.org/t/issues-migrating-test-database/135876/7 "2019-12-13T17:10:16Z")

</div>

Thanks, David! I saw that it prints a warning now. I don’t quite understand why it was so difficult to get the test database migrated, but after I did a bunch of destroy,create,migrate and then the `RAILS_ENV=test bin/rake db:migrate ` you recommended, I managed to see that my spec is broken! Now, I can fix it and I’m on my way to learning how to write specs! Thanks again for your push, @Mittineague.

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [December 13, 2019, 5:14pm UTC](https://meta.discourse.org/t/issues-migrating-test-database/135876/8 "2019-12-13T17:14:34Z")

</div>

> [@pfaffman](#):
>
> I don’t quite understand why it was so difficult to get the test database migrated

The context behind my commit is that I found a bug where

```plaintext
RAILS_ENV=test rake db:drop db:create db:migrate

```

doesn’t include post\_deploy migrations. You have to run `db:migrate` by itself.

We’ll definitely get that fixed, but in the meantime this warning should reduce the frustration!

---

<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: [March 2, 2021, 4:44pm UTC](https://meta.discourse.org/t/issues-migrating-test-database/135876/9 "2021-03-02T16:44:20Z")

</div>

Has that been fixed? It looks like a `./bin/rake db:drop db:create` drops and creates the test database, but it seems that `./bin/rake db:migrate` doesn’t also do the migrations on test.
