The magic command
Rails introduced a command a while back that simplifies database adapter changes.
rails db:system:change --to=postgresql
What does this command do
- Updates the Gemfile - Adds/removes the necessary gems
- Modifies the database.yml - Configures the new connections
- Generates the migrations - Creates the migration files for the new database
- 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
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)