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

I made an app in Ruby on Rails and now I want to get it hosted. However, they require that I use MySQL and I set it up using sqLite3. Is there any way to convert it to use MySQL?

Step 0
To be safe, I recommend experimenting a bit with this technique in a virtual machine. Save yourself a bunch of heartache and build a virtual machine, check out your code, and have a safe playground that you can throw away if tragedy strikes.
Step 1
Make a backup copy of your database.yml file.
(from your application root)
cp config/database.yml config.database.yml.sqlite3
Step 2
Make a backup copy of your data
For Rails 3, install the YAML DB gem: https://github.com/ludicast/yaml_db
by running
gem install yaml_db
and then add to your Gemfile.
gem 'yaml_db'
For Rails 2.x install the YAML DB plugin:
script/plugin install git://github.com/adamwiggins/yaml_db.git
Run the dump task
rake db:dump
Step 3
Update your config/database.yml file. You 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 sure to update the values surrounded by asterix as appropriate for your platform! The socket value is only good for Mac OSX using MacPorts. Most flavors of linux do not require this value.
Step 5
If you have some errors in the following step, you might have to install the mysql or mysql2 gem:
sudo gem install mysql
or
sudo gem install mysql2
Have rake create your database
rake db:create
rake db:schema:load
Step 6
Use YamlDb to reload your data into MySql
rake db:load

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

Check Taps. I've successfully converted a Mysql database to Postgres with it --it should support SQLite.
Edit: Including working link from cony's comment here.

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

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'.

Related

How do I change rails' default database from sqlite3 to postgres, across all new projects?

Apologies for the rookie question. I would like to make postgresql my default for all new rails apps. I'm aware of the command:
rails new my_app --database=postgresql
...but I have an irrational dislike for sqlite3 and for typing this extra command. I want my rails apps to love postgres monogamously, without me telling them they shouldn't hook up with sqlite3 first. How do I go about this?
I use rbenv (again, irrationally) to manage my ruby versions. Thanks in advance.
Create a .railsrc file in your HOME directory and put your db override there
# ~/.railsrc
--database=postgresql
You can add all other overrides that you might want to use, like --skip-test-unit or the like.
This file will be applied each time you run a rails new command.
you can change default database by changing database.yml according to given file and don't forget to add pg gem in route gemfile like this gem 'pg'
development:
adapter: postgresql
encoding: utf8
database: project_development
pool: 5
username:
password:
test: &TEST
adapter: postgresql
encoding: utf8
database: project_test
pool: 5
username:
password:
production:
adapter: postgresql
encoding: utf8
database: project_production
pool: 5
username:
password:
if this did'nt help you see the rails cast Migrating to PostgreSQL

Postgresql with Ruby on Rails

I want to migrate from sqlite3 to postgresql. After doing search I found that I should change my database.yml to somthing like that:
adapter: postgresql
encoding: unicode
database: [insert your dev database name]
pool: 5
username: [insert your user name]
password:
But I don't know what to provide in place of database, username and password. Because i don't remember creating any of them. I just created my rails app and migrations , and thats it.
To configure your rails project with postgres database
do the below steps.
provide your database details in the database.yml file
eg:
development:
username: postgres
database: ur-db-dev
password: pass
encoding: UTF8
adapter: postgresql
timeout: 500
pool: 5
add postgres gem in your Gemfile
gem 'pg'
then,
Do bundle install to install postgres gem
rake db:create to create your database
rake db:migrate to migrate your migration files

Cannot connect to Postgresql server, locally

I have a Rails app that has been using sqlite3 for the DB. Deployed to Heroku. Then find out that Heroku recommends switching to PostgreSQL. So now I'm trying to switch over without any luck. Was trying to use this Railscasts video for help.
I installed Homebrew. Installed PostgreSQL via Homebrew. After installation, there was no mention of creating a username or password for postgres.
I edited my Gemfile to
gem 'pg'
for both production and development and did bundle install.
I edited my database.yml file to this:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
production:
adapter: postgresql
database: isement_production
encoding: unicode
pool: 5
timeout: 5000
Like Ryan says to do in the video, I try this command:
rake db:create:all
Only to get this error:
could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I do some more searching, and see that some tutorials show username and password included in the database.yml file. I then find out how to setup a user for Postgresql
After entering in the command $ createuser joe, I was never given the options that the docs say you'll be asked. Such as "Shall the new role be a superuser? (y/n)" So really not sure if the user was created, but there wasn't any errors either.
So, I'm assuming, after creating the user "joe", I reedited my database.yml file to include the user field I just created:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
username: joe
password:
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
Only to still get the same error of not being able to connect.
I've ran the command
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
to make sure the server is running as well.
Just in case it's needed, when I run
which psql
I receive this:
/usr/local/bin/psql
Is there something that I'm missing? The "database name" part of the database.yml file. This is supposed to be a database already created somewhere, or does this file create the database when I run the rake db:create:all command? I'm assuming the latter, so the name of the database doesn't matter?
Lazy option: Add host: localhost to your database.yml.
Only slightly less lazy option: Uninstall and reinstall the pg gem.
What's going on here? There are a number of ways for Postgres to already exist on your system, and the pg gem will use their pg_config output and build for their needs if you install the gem before installing your own copy of Postgres.
In your case, it was built for the version included with some releases of Mac OS X, which uses a socket file at /var/pgsql_socket.
when you are installing Postgres via Homebrew it will add your username to postgres with an empty password.
Just open a command prompt and type
whoami
Then change your database.yml to your mac user name (whatever is returned from whoami) with empty password.
Maybe add host: localhost
to you database.yml

Change from SQLite to PostgreSQL in a fresh Rails project

I have a rails app that's databases are in SQLite (The dev and production). Since I am moving to heroku, I want to convert my database to PostgreSQL.
Anyways, I heard that the local, development, database does not need to be changed from SQLite, so I don't need to change that, however, how do I go about changing the production environment from SQLite to PostgreSQL?
Has anyone ever done this before and can help?
P.S. I'm not sure what exactly this process is called, but I've heard about migrating the database from SQLite to PostgreSQL, is that what needs to be done?
You can change your database.yml to this instead of using the out of the box sqlite one:
development:
adapter: postgresql
encoding: utf8
database: project_development
pool: 5
username:
password:
test: &TEST
adapter: postgresql
encoding: utf8
database: project_test
pool: 5
username:
password:
production:
adapter: postgresql
encoding: utf8
database: project_production
pool: 5
username:
password:
cucumber:
<<: *TEST
The steps below worked for me. It uses the taps gem, created by Heroku and mentioned in Ryan Bates's Railscast #342. There are a few steps but it worked perfectly (even dates were correctly migrated), and it was far easier than the Oracle -> DB2 or SQL Server -> Oracle migrations I have done in the past.
Note that SQLite does not have a user id or password, but the taps gem requires something. I just used the literals "user" and "password".
Create the Postgres database user for the new databases
$ createuser f3
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y
EDIT - Updated command below - use this instead
$ createuser f3 -d -s
Create the required databases
$ createdb -Of3 -Eutf8 f3_development
$ createdb -Of3 -Eutf8 f3_test
Update the Gemfile
gem 'sqlite3'
gem 'pg'
gem 'taps'
$ bundle
Update database.yml
#development:
# adapter: sqlite3
# database: db/development.sqlite3
# pool: 5
# timeout: 5000
development:
adapter: postgresql
encoding: unicode
database: f3_development
pool: 5
username: f3
password:
#test:
# adapter: sqlite3
# database: db/test.sqlite3
# pool: 5
# timeout: 5000
test:
adapter: postgresql
encoding: unicode
database: f3_test
pool: 5
username: f3
password:
Start the taps server on the sqlite database
$ taps server sqlite://db/development.sqlite3 user password
Migrate the data
$ taps pull postgres://f3#localhost/f3_development http://user:password#localhost:5000
Restart the Rails webserver
$ rails s
Cleanup the Gemfile
#gem 'sqlite3'
gem 'pg'
#gem 'taps'
$ bundle
Now its become easy with the single command
bin/rails db:system:change --to=postgresql
Since you're moving to heroku, you can use taps to do this:
heroku db:push
This will push your local development sqlite data to production, and heroku will automagically convert to postgres for you.
This should also work to push a production sqlite db to heroku, but it's not tested.
RAILS_ENV=production heroku db:push
you will also need to add the line "gem 'pg'" to your gemfile, 'pg' being the current postgres gem for Rails.
Simply update the config/database.yml file:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: projectname_development
test:
<<: *default
database: projectname_test
production:
<<: *default
database: projectname_production
username:
password:
The above is what's generated when you run:
$ rails new projectname --database=postgresql --skip-test-unit
Also add this to your Gemfile:
gem 'pg'
Just Update you datatbase.yml
development: &development
adapter: postgresql
database: Your_database_name
username: user_name
password: password
host: localhost
schema_search_path: public
min_messages: warning
test:
<<: *development
database: test_database_name
production:
<<: *development
database: production_db_name
We are using rails and the basic standards should be follow like DRY, Convention over Configuration etc.. so in above code we are not repeating same code again and again.
It's been mentioned above me, but I don't have enough reputation as a lurker to be able to upvote it. In the hopes of drawing a little more attention for Rails newbies reading this answer:
you will also need to add the line "gem 'pg'" to your gemfile, 'pg' being the current postgres gem for Rails.
^^^ This is a key piece in addition to the database.yml file described in the selected answer to migrate your Rails app to Postgres.
After replacing gem 'sqlite3 with gem pg in the gemfile, I kept getting the sqlite3 error when pushing to Heroku master because I forgot to commit the updated gemfile. Simply doing the following solved this:
git add .
git commit -m 'heroku push'
heroku create
git push heroku master
This is how I have mine setup. If you are only using MRI and not Jruby you can skip the logic in the adapter settings.
defaults: &defaults
adapter: <%= RUBY_ENGINE == 'ruby' ? 'postgresql' : 'jdbcpostgresql' %>
encoding: unicode
pool: 5
timeout: 5000
development:
database: project_development
<<: *defaults
test:
database: project_test
<<: *defaults
production:
database: project_production
<<: *defaults
You can try following:
sqlite3 development.db .dump | psql dbname username
or try with sqlitetopgscript:
http://trac-hacks.org/browser/sqlitetopgscript/0.10/sqlite2pg
A possible solution (not for heroku) it's to use yaml.db from:
http://www.railslodge.com/plugins/830-yaml-db
Today I had the same issue. I'm working on Rails 4.2.8. The solution was specify the pg gem version, in my case, 0.18.4.

SQLite and Ruby on Rails on PC

I am using instant rails which makes use of SQLite and I am unable to connect to the database. I have been using a tutorial that uses MySQL and I have been unable to find instructions for SQLite. Any suggestions?
You could install sqlite.
Or, assuming you have mysql installed, you could change your config/database.yml file to use mysql instead of sqlite.
(From using rails -d mysql testapp)
development:
adapter: mysql
encoding: utf8
reconnect: false
database: awesome_development
pool: 5
username: root
password:
host: localhost
Update
To use sqlite3, you need the sqlite gem and to set up your rails database.yml to use it. The default config rails generates uses sqlite and looks like this
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
to install, here are some good looking instructions

Resources