[ACCEPTED]-Convert a Ruby on Rails app from sqlite to MySQL?-ruby-on-rails

Accepted answer
Score: 119

Step 0

To be safe, I recommend experimenting a 20 bit with this technique in a virtual machine. Save 19 yourself a bunch of heartache and build 18 a virtual machine, check out your code, and 17 have a safe playground that you can throw 16 away if tragedy strikes.

Step 1

Make a backup copy 15 of your database.yml file.

(from your application 14 root)

cp config/database.yml config.database.yml.sqlite3

Step 2

Make a backup copy of your data

For 13 Rails 3, install the YAML DB gem: https://github.com/ludicast/yaml_db

For Rails 12 2.x install the YAML DB plugin:

script/plugin install git://github.com/adamwiggins/yaml_db.git

Run the dump 11 task

rake db:dump

Step 3

Update your config/database.yml file. You 10 will find entries like

development:
  adapter: sqlite3
  database: db/development.sqlite3
  timeout: 5000
test:
  adapter: sqlite3
  database: db/test.sqlite3
  timeout: 5000
production:
  adapter: sqlite3
  database: db/production.sqlite3
  timeout: 5000

Change them to

development:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: **myapp_development**
  pool: 5
  username: **root**
  password: **supersecretpassword**
  **socket: /opt/local/var/run/mysql5/mysqld.sock**
test:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: **myapp_test**
  pool: 5
  username: **root**
  password: **supersecretpassword**
  socket: **/opt/local/var/run/mysql5/mysqld.sock**

production:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: **myapp_production**
  pool: 5
  username: **root**
  password: **supersecretpassword**
  socket: **/opt/local/var/run/mysql5/mysqld.sock**

Be 9 sure to update the values surrounded by 8 asterix as appropriate for your platform! The 7 socket value is only good for Mac OSX using 6 MacPorts. Most flavors of linux do not 5 require this value.

Step 5

If you have some errors 4 in the following step, you might have to 3 install the mysql gem:

sudo gem install mysql

Have rake create your 2 database

rake db:create
rake db:schema:load

Step 6

Use YamlDb to reload your data into 1 MySql

rake db:load

Score: 7

As long as you have not written any SQL 6 statements that run in sqlLite3 and not 5 MySQL (which you won't have if all your 4 database access is via ActiveRecord and 3 ActiveRecord migrations) then all you need 2 to do is change the database adapter in 1 your database.yml config file.

Score: 6

Check Taps. I've successfully converted a Mysql 3 database to Postgres with it --it should 2 support SQLite.

Edit: Including working link 1 from cony's comment here.

Score: 5

If there's no data to migrate, simply update 3 database.yml and run 'rake db:schema:load' in 2 the new environment. (NOT db:migrate which 1 should only be used for incremental migrations!)

Score: 5
myproject  user$ cd
user   $ rails new myproject -d mysql

Say 'no' for all question but for Overwrite .../myproject/config/*database.yml*? (enter "h" for help) [Ynaqdh] say 'yes'.

0

More Related questions