Rails and testing against legacy databases

Everything,

In some part of the technical binding.

I created a small, small Rails application that relies on some external databases (not defined in local migrations) to increase information about key applications. I am sitting to write some tests, and I am struggling to find a suitable solution to support this external data.

For example, let's say I have a DailyWidgetViews model in my new / current Rails application. At the table level, it looks like this:

id, widget_id, date, views, timestamps 

Let's say that widgets live in a separate database. There are tools for working with this script in Rails. For example, when defining a Widget, you can do something like:

 class Widget << ActiveRecord::Base establish_connection("some_legacy_db_name") set_table_name :crazy_widget_table_name 

Now in DailyWidgetViews I can build belongs_to sympathy, and all is well.

However, what to do when the time comes for testing ???

Say I have a DailyWidgetViews resource and you want to test the / index action and return a Widget association for each DailyWidgetView. What is the recommended strategy for defining and interacting with this external data? Even if I mock everything, test: unit and rspec expect to find a definition for Widget in the test database. In addition, a few bits of caching optimization in the application also expects to find some data here (it may be empty) when loading the application.

Some possible options that I see:

  • Create a separate project that consists only of migrations. These migrations have a minimum minimum to support an outdated scheme. Run this project when you just need to configure the circuit. Change everything in the tests so that we never interact with this database.

  • Export obsolete schema and hack db: setting to load the schema into the corresponding test dbs before starting testing. Spread everything as in # 1.

I am inclined to option number 1, but I hoped that someone had dealt with this problem before. Is anyone

Thanks in advance!

+4
source share
1 answer

All you really need to do to do rake db: test: prepare the right thing is to get the correct circuit in the schema.rb file.

So, if you want to make this file correctly, you have a couple of options - maybe more, but they come to mind.

  • Just edit the desired schema information. I would not recommend this, as this file will be modified if you ever need to migrate from scratch to dev / prod.
  • Create a migration that sets up the tables you need, and then just rely on RAILS_ENV in your environment and Rails.env in your application to take care to specify it in the correct database.
0
source

Source: https://habr.com/ru/post/1385237/


All Articles