-1

I'm using Rails 5 with Minitest 5.9.

I have some views in my application for complex queries.

Everytime I create a new migration my test database removes my views and I have to recreate them.

How do I automate the view recreation?

David Silva Smith
  • 11,498
  • 11
  • 67
  • 91
  • You can use plain old SQL in a migration which creates the views. There is also a gem called [Scenic](https://robots.thoughtbot.com/announcing-scenic--versioned-database-views-for-rails) which makes database views more of a first class citizen. – max Jan 06 '17 at 12:21
  • I do use SQL in the migration but every time I do a new migration it runs the migration and during that process clears out my views so I have to recreate them. I also tried https://github.com/anykeyh/rails_db_views which made views first class citizens and worked great, but it conflicted with another gem so I stopped. Thanks for the tip on Scenic I'll have a look. – David Silva Smith Jan 06 '17 at 14:53

1 Answers1

1

I had a similar problem. We had some triggers on our DB, which could not be created by rails migration.

The solution we followed is using seeds. We defined our triggers in db/seeds.rb, and before running the tests, we ran rake db:reset. This dropped the DB, created it again, loaded the schema, and ran what's in db/seeds.rb.

Hope you can use the same.

31piy
  • 23,323
  • 6
  • 47
  • 67
  • 1
    Thanks! I might end up doing that. Right now our seeds db has data in it for dev and prod. I guess I could put conditional logic in there for what to do depending on the env... – David Silva Smith Jan 06 '17 at 10:09