Rails: what does schema.rb do?
Asked Answered
T

2

85

I used to think the db/schema.rb in a Rails project stored the database schema, so that ActiveRecord can know what table/column it has.

But earlier I surprisingly noticed that my project runs normally after I delete db/schema.rb!

So, since the Rails can work without it, what does schema.rb really do?

Tuesday answered 27/3, 2012 at 6:32 Comment(1)
On boot Rails queries the database to get the real schema the DB has and this is what active record uses and how the schema.rb is generated. Some more info kirshatrov.com/posts/schema-cacheHeyde
T
126

The schema.rb serves mainly two purposes:

  1. It documents the final current state of the database schema. Often, especially when you have more than a couple of migrations, it's hard to deduce the schema just from the migrations alone. With a present schema.rb, you can just have a look there. ActiveRecord itself will indeed not use it. It will introspect the database during runtime as this is much safer than to expect users to keep the schema.rb up-to-date. However to avoid confusion of your developers, you should always maintain a file that is up-to-date with your migrations.

  2. It is used by the tests to populate the database schema. As such a rake db:schema:dump is often run as part of the rake test:prepare run. The purpose is that the schema of the test database exactly matches the current development database.

In addition to that, some people use the schema.rb to load the final schema into a database to avoid having to run a long list of migrations to setup a fresh database. This is sometimes necessary if you have an application where the previous migrations can not run cleanly with the rest of the application (e.g. because model definitions have changed between the time the migrations were added compared to the current state).

Generally however, I would recommend to keep the migrations up-to-date with the rest of the application so that you can always start with just the migrations rather than replying on schema.rb to avoid multiple and potentially diverging "sources of truth".

Tramel answered 27/3, 2012 at 6:38 Comment(3)
I think it should be expected that the users keep the schema.rb up o date, since the comment on the same file explain: Note that this schema.rb definition is the authoritative source for your database schema.Visayan
jgomo3 - The same comment in that file also says "This file is auto-generated...". so there is no need for the users to keep this file up to date. It happens automatically during migrations.Horatio
In some situations, you do need to update the database schema. For example, in our case we have multiple rails apps that share most of the models and those models are put in a separate repo (let's call it shared-models), which every app depend on. We put our migrations in shared-models, but run it in one of the apps, say app1. Then the schema.rb for app1 is updated automatically during the migration, but schema.rb files for other apps are not updated. You still need to copy the updated schema.rb file from app1 to other apps, so that they are all up to dateSpeedometer
B
10

Rails Documentation / 6.1 What are Schema Files for?

Migrations, mighty as they may be, are not the authoritative source for your database schema. That role falls to either db/schema.rb or an SQL file which Active Record generates by examining the database. They are not designed to be edited, they just represent the current state of the database.

There is no need (and it is error prone) to deploy a new instance of an app by replaying the entire migration history. It is much simpler and faster to just load into the database a description of the current schema.

For example, this is how the test database is created: the current development database is dumped (either to db/schema.rb or db/structure.sql) and then loaded into the test database.

Schema files are also useful if you want a quick look at what attributes an Active Record object has. This information is not in the model's code and is frequently spread across several migrations, but the information is nicely summed up in the schema file. The annotate_models gem automatically adds and updates comments at the top of each model summarizing the schema if you desire that functionality.

Rails documents have you covered.

Bennett answered 9/2, 2017 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.