How to connect to MS SQL in Rails

I am trying to transfer stale data from an MS SQL database to my Rails application. I added a configuration to freetds that connects correctly. In my Gemfile, I added tiny_tds and activerecord-sqlserver-adapter , respectively.

I created a file for placing classes from the old database for translation to ActiveRecord:

class LegacyUser < ActiveRecord::Base establish_connection :legacy set_table_name 'users' end . . . 

database.yml

 legacy: adapter: sqlserver mode: odbc dsn: legacy_db_name host: db_host_name database: legacy_db_name port: 1433 username: username password: password 

Then I have rake tasks to convert the data:

legacy.rake

  desc 'migrate users' task :users => :environment do require 'lib/tasks/legacy_classes' LegacyUser.each do |user| begin new_user = User.new new_user.attributes = { :firstname => user.firstname, :lastname => user.lastname, :email => user.email, :created_at => Time.now, :updated_at => Time.now } new_user.save! puts "User #{user.id} successfully migrated" rescue puts "Error migrating #{user.id}" end end 

At this point, I'm just trying to get the rake task to "connect" to the old database.

When I try to use "rake users", I get:

 rake aborted! database configuration does not specify adapter 

It seems to me that I clearly indicated the adapter. What is the correct way to configure it?

Also, as a side question, in my β€œclasses” file for legacy database tables, should all of these tables display a β€œnew” Rails database schema? Ideally, I want to be able to simply connect to various tables in the old database and insert them into the new database schema where necessary. Associations in the old do not correspond to the new, as well as naming conventions.

Any help is appreciated. Thanks.

Update

This error still occurs. Unfortunately, the only threads I could find that had the same error were odd-interval problems in the database.yml file. So I really took the time to go through and make sure that all the intervals match my other configurations. Given that this is a foggy mistake, I’m not even sure what to check past instructions for setting up the activerecord-sqlserver adapter.

+4
source share
1 answer

Got an answer to this here: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/250

'You can try connecting to your database the way it is, this works for us in this thread on Rails 4:'

 class LegacyTable < ActiveRecord::Base establish_connection({ :adapter => "sqlserver", :host => "host", :username => "user", :password => "pass", :database => "dbname" }) 

This solved my problem. Hope this helps someone in the future.

+5
source

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


All Articles