0

I am using rails 5.1. We noticed that our views in our test database kept disappearing after running migrations.

Our database is a bit funky: we have views that depend on other views. My assumption is that when a migration is run: in attempt to update the test database, rails is attempting to recreate those views based off of the schema.rb but is having issues due to the views depending on each other, so rails just stops trying to recreate the views altogether.

Is there a way, perhaps, to have rails not worry about the views at all during migrations? That way rails would stop deleting the views in the test database.

It is odd: the views are just fine in the development database. The migrations only delete the views in the test database.

Neil
  • 4,578
  • 14
  • 70
  • 155
  • 1
    Sort of related: [**Rails, PostgreSQL, and History Triggers**](https://stackoverflow.com/a/47879896/479863) and [**Can I write PostgreSQL functions on Ruby on Rails?**](https://stackoverflow.com/a/31955422/479863). – mu is too short May 04 '18 at 21:12

1 Answers1

1

In Rails, schema.rb does not include views by default. For testing it is typical that a new database will be initialized with schema.rb and then migrations later than that will be run. So possibly, it looks like the views are being destroyed, but actually the database is being created from scratch and rather than being destroyed, the views are never created.

Even if you are explicitly creating the views in migrations, they will not be included in schema.rb. So any views in migrations earlier than that file is created, will be missing.

There are various ways around this. One is to not us the schema.rb file when generating test databases, and instead migrate from scratch each time. Depending on past migrations though, this could slow down tests unacceptably.

Mori
  • 27,279
  • 10
  • 68
  • 73