Question

I am attempting to migrate legacy data from a MS SQL database into my Rails Application. I have added configuration in freetds which is connecting properly. In my Gemfile, I have added the tiny_tds and activerecord-sqlserver-adapter, respectively.

I have created a file to house the classes from the legacy database to translate 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 am just trying to get the rake task to 'connect' to the legacy database.

When I try 'rake users', I get:

rake aborted!
database configuration does not specify adapter

It looks to me like I have clearly specified the adapter. What is the proper way to configure this?

Also as a side question, in my 'classes' file for legacy database tables, should all of those tables mirror the 'new' Rails database schema? Ideally I want to be able to simply connect to various tables in the legacy database and fit them into the new database schema where needed. The associations in the old do not match the new, nor do the naming conventions.

Any help is appreciated. Thanks.

Update

Still experiencing this error. Sadly, the only threads I could find that have had the same error were odd spacing issues in the database.yml file. So I actually took the time to go through and make sure all of the spacing matched my other configurations. Given that it is a vague error I am not really even sure what to check past the instructions for setting up activerecord-sqlserver-adapter.

Was it helpful?

Solution

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

'You can try connecting to your DB like so, this works for us on that branch on Rails 4:'

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

This solved my issue. Hope it helps someone in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top