DEV Community

Diogo Viana
Diogo Viana

Posted on • Originally published at diogo.dev on

Quick Rails Tip: Change Database Adapter

The magic command

Rails introduced a command a while back that simplifies database adapter changes.

rails db:system:change --to=postgresql
Enter fullscreen mode Exit fullscreen mode

What does this command do

  1. Updates the Gemfile - Adds/removes the necessary gems
  2. Modifies the database.yml - Configures the new connections
  3. Generates the migrations - Creates the migration files for the new database
  4. Updates the configurations - Adjusts the schema.rb and other files

More practical examples

# Changing from SQLite to PostgreSQL
rails db:system:change --to=postgresql

# Changing to MySQL
rails db:system:change --to=mysql

# Going back to SQLite
rails db:system:change --to=sqlite3
Enter fullscreen mode Exit fullscreen mode

Important considerations

  • Always backup! - Backup your data before making changes
  • Test in development - Never test in production
  • Check data types - Some types may need adjustments
  • Dependencies - Make sure you have the database installed on your machine (or Dockerfile)

Conclusion

Rails is full of hidden gems, and this is one of them. I think it's super useful, especially when you start a small project with SQLite and then want to migrate to PostgreSQL, for example.

Top comments (0)