# Auto migrate test database schema

**URL:** <https://meta.discourse.org/t/auto-migrate-test-database-schema/150786>\
**Category:** Development\
**Created:** [May 8, 2020, 4:27am UTC](https://meta.discourse.org/t/auto-migrate-test-database-schema/150786 "2020-05-08T04:27:56Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![xrav3nz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/xrav3nz/32/76894_2.png) [@xrav3nz](https://meta.discourse.org/u/xrav3nz)\
**Post date:** [May 8, 2020, 4:27am UTC](https://meta.discourse.org/t/auto-migrate-test-database-schema/150786/1 "2020-05-08T04:27:56Z")

</div>

Related: [Db:drop, create & migrate behavior with RAILS\_ENV=development - #2 by taylorthurlow - A May Of WTFs - Ruby on Rails Discussions](https://discuss.rubyonrails.org/t/db-drop-create-migrate-behavior-with-rails-env-development/74435/2)

Not sure if we have explored this before, but Rails can automatically maintain test databse schema with `ActiveRecord::Migration.maintain_test_schema!` ([method definition](https://github.com/rails/rails/blob/148bb058bc0b93501296c2337651fa68b8b49f65/activerecord/lib/active_record/migration.rb#L632-L636), [in `rails/test_help`](https://github.com/rails/rails/blob/9ecbd64cd93af0c7efe3141e93f85e5c263e00d0/railties/lib/rails/test_help.rb#L16-L21), [in latest `rspec/rails` helper generator](https://github.com/rspec/rspec-rails/blob/148f7ce473e0f9e36018e9a7ca79aad4af0a6d14/lib/generators/rspec/install/templates/spec/rails_helper.rb#L25-L34))

Here is a diff to incorporate it into Discourse’s `spec/rails_helper`:

[https://github.com/xrav3nz/discourse/commit/90ac78ad3b64c07ddaf86ccc883c87c0dd727348](https://github.com/xrav3nz/discourse/commit/90ac78ad3b64c07ddaf86ccc883c87c0dd727348)

**However** , it fails on my Docker-based development setup:

```plaintext
ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: must be owner of database discourse_test

```

This is because `maintain_test_schema!` utilizes `db:test:prepare`, and tries to drop the test database first, to which (I believe) the `discourse` user on Docker-based setup does not have access.

Could someone **with a native dev setup** could try it out? If it works, maybe the change is worth considering?

---

_[View the full topic](https://meta.discourse.org/t/auto-migrate-test-database-schema/150786)._
