Search code examples
mysqlruby-on-railssqlitehas-and-belongs-to-manydata-migration

Ruby On Rails data migration from sqlite3 to mysql regarding to many to many relations


I have a sqlite3 database in my rails app which has a development.sqlite3 file. I want to convert the database to mysql.Here are the steps I take but still I have problems :

1.First I add and install gem 'seed_dump' to add the data into my seed.rb (By running rake db:seed:dump) because I really need to migrate my data .

2.Change the database.yml configuration to mysql setting.

development:
  adapter: mysql2
  encoding: utf8
  database: MyDB
  username: root
  password: ****
production:
  adapter: mysql2
  encoding: utf8
  database: MyDB
  username: root
  password: ****

3.Run rails db:create then rails db:schema:load.

4.Then loading the data from seed by running rake db:seed:dump

The problem is my many to many relations data (which has a table in db schema ) can't be imported in mysql from my seed.rb.

The thing I wanna know is that is there any other safe way to migrate my data from sqlite3 to mysql instead of writing them into seed.rb and then read them ?


Solution

  • As I was looking for another way to resolve the issue I've found what is declared here. I followed the steps facing to another error of mysql : Data is too long for column summary . To skip this error I turned off mysql strict mode like this in database.yml:

    config/database.yml

    development:
      adapter: mysql2
      encoding: utf8
      database: myDB
      username: root
      password: ****
      host: localhost
      strict: false
    
    
    production:
      adapter: mysql2
      encoding: utf8
      database: myDB
      username: root
      password: **** 
      host: localhost
      strict: false
    

    This solution is more clear than using gem 'seed_dump' which has problems with has and belongs to many relations!