Don't Use Rails Schema! Use This Instead
There are two things to note about
- It is a reflection of the database structure in Ruby.
- It is database-agnostic. This means that no matter what database you use, the syntax and structure will be largely the same.
- Advanced database features are not covered.
schama.rb will be very useful when we are faced with a scenario:
- Our application has thousands of migration files. This causes a lot of wasted time because it takes a long time to set up a new environment.
- The migration files may contain code that is only compatible with the old version of the database. Therefore, it will fail when executed.
rails db:setup is the quickest solution to both (or more) of these problems. This command will build the database from
But what if the database has complex logic that is not captured in
schema.rb? The solution is to use
The advantage of using
structure.sql is that it will copy the structure of the database exactly, including functions, views, constraints, stored procedures, etc., which is not possible with
class Application < Rails::Application
# Add this line:
config.active_record.schema_format = :sql
rails db:migrate and the file
db/structure.sql will be generated. When we run the
rails db:setup command it no longer uses
schema.rb but uses