4

I have a new version of a website that needs to pull some information from the legacy version of a website. The old and new databases have the same tables, fields, etc. The old website is running Rails 3. The new website is running Rails 5.

I want to pull some records (let's call them Orders) from the old database into the new database.

My first attempt was to create a model class and establish a connection using ActiveRecord like so:

class OldOrder < ActiveRecord::Base
  establish_connection(
    adapter:  "mysql2",
    host:     "old_server",
    username: "user",
    password: "password",
    database: "database"
  )
end

and then call upon the records like so:

OldOrder.where(order_number: "whatever").each do |order|
  # and so on
end

and so on.

Unfortunately, whenever I run any command on OldOrder, I receive the following error message:

*** RuntimeError Exception: Your version of MySQL (5.0.77) is too old. Active Record supports MySQL >= 5.1.10.

So I figured I have two options: upgrade the mysql on the old server (which would be undesirable due to the nature of the legacy server), or get the data some other way.

Searching StackOverflow for potential answers, I tried using raw sql:

 sql = "Select * from ... your sql query here"
 records_array = ActiveRecord::Base.connection.execute(sql)

But since it uses ActiveRecord::Base to run the sql, it also failed.

Is there any way to connect to this old database within Rails without using ActiveRecord, or should I be looking for alternatives?

Thank you for your time!

5
  • See this question/answer: stackoverflow.com/questions/17311199/… Commented Jan 11, 2019 at 15:42
  • Thanks for your response! I have seen that answer, but unfortunately it establishes the connection with the second database using "ActiveRecord::Base.establish_connection", and my issue it that ActiveRecord itself does not work with my eons-old legacy database. Commented Jan 11, 2019 at 15:54
  • 1
    I see. In that case you will probably either have to: 1. Install the old mysql gem to connect (probably not an option if you're on a newer version of Rails) 2. Create a dump file from the old database, import it into a newer version of MySQL, and connect to that Commented Jan 11, 2019 at 16:37
  • 1
    Hm, good question. How about adding the ruby sequel gem? github.com/jeremyevans/sequel You could use that to hit the old DB and use ActiveRecord to hit the new one. Commented Jan 11, 2019 at 18:21
  • The sequel gem works a treat, thank you! Commented Jan 14, 2019 at 10:57

1 Answer 1

1

Following Dave Slutzkin's comment, I installed the sequel gem and connected to the old database in much the same way:

DB = Sequel.connect(
  adapter: :mysql2,
  host: 'old_server',
  user: 'user',
  password: 'password',
  database: 'database'
)

I can then call statements on this DB object in much the same way that ActiveRecord can, following sequel's documentation.

old_orders = DB[:orders]
old_orders.where(whatever: "whatever").each do |old_order|
   # Create new order
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.