未验证 提交 84718df8 编写于 作者: D Derek Prior

Update schema.rb documentation [CI SKIP]

The documentation previously claimed that `db/schema.rb` was "the
authoritative source for your database schema" while simultaneously
also acknowledging that the file is generated. These two statements are
incongruous and the guides accurately call out that many database
constructs are unsupported by `schema.rb`. This change updates the
comment at the top of `schema.rb` to remove the assertion that the file
is authoritative.

The documentation also previously referred vaguely to "issues" when
re-running old migrations. This has been updated slightly to hint at the
types of problems that one can encounter with old migrations.

In sum, this change attempts to more accurately capture the pros, cons,
and shortcomings of the two schema formats in the guides and in the
comment at the top of `schema.rb`.

[Derek Prior & Sean Griffin]
Co-authored-by: NSean Griffin <sean@seantheprogrammer.com>
上级 fb9ed7b0
...@@ -71,11 +71,11 @@ def header(stream) ...@@ -71,11 +71,11 @@ def header(stream)
# of editing this file, please use the migrations feature of Active Record to # of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition. # incrementally modify your database, and then regenerate this schema definition.
# #
# Note that this schema.rb definition is the authoritative source for your # This file is the source Rails uses to define your schema when running `rails
# database schema. If you need to create the application database on another # db:schema:load`. When creating a new database, `rails db:schema:load` tends to
# system, you should be using db:schema:load, not running all the migrations # be faster and is potentially less error prone than running all of your
# from scratch. The latter is a flawed and unsustainable approach (the more migrations # migrations from scratch. Old migrations may fail to apply correctly if those
# you'll amass, the slower it'll run and the greater likelihood for issues). # migrations use external dependencies or application code.
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
......
...@@ -917,35 +917,29 @@ Schema Dumping and You ...@@ -917,35 +917,29 @@ Schema Dumping and You
### What are Schema Files for? ### What are Schema Files for?
Migrations, mighty as they may be, are not the authoritative source for your 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 database schema. Your database remains the authoritative source. By default,
Active Record generates by examining the database. They are not designed to be Rails generates `db/schema.rb` which attempts to capture the current state of
edited, they just represent the current state of the database. your database schema.
There is no need (and it is error prone) to deploy a new instance of an app by It tends to be faster and less error prone to create a new instance of your
replaying the entire migration history. It is much simpler and faster to just application's database by loading the schema file via `rails db:schema:load`
load into the database a description of the current schema. than it is to replay the entire migration history. Old migrations may fail to
apply correctly if those migrations use changing external dependencies or rely
For example, this is how the test database is created: the current development on application code which evolves separately from your migrations.
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 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 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 frequently spread across several migrations, but the information is nicely
summed up in the schema file. The summed up in the schema file.
[annotate_models](https://github.com/ctran/annotate_models) gem automatically
adds and updates comments at the top of each model summarizing the schema if
you desire that functionality.
### Types of Schema Dumps ### Types of Schema Dumps
There are two ways to dump the schema. This is set in `config/application.rb` The format of the schema dump generated by Rails is controlled by the
by the `config.active_record.schema_format` setting, which may be either `:sql` `config.active_record.schema_format` setting in `config/application.rb`. By
or `:ruby`. default, the format is `:ruby`, but can also be set to `:sql`.
If `:ruby` is selected, then the schema is stored in `db/schema.rb`. If you look If `:ruby` is selected, then the schema is stored in `db/schema.rb`. If you look
at this file you'll find that it looks an awful lot like one very big at this file you'll find that it looks an awful lot like one very big migration:
migration:
```ruby ```ruby
ActiveRecord::Schema.define(version: 20080906171750) do ActiveRecord::Schema.define(version: 20080906171750) do
...@@ -967,36 +961,32 @@ end ...@@ -967,36 +961,32 @@ end
In many ways this is exactly what it is. This file is created by inspecting the In many ways this is exactly what it is. This file is created by inspecting the
database and expressing its structure using `create_table`, `add_index`, and so database and expressing its structure using `create_table`, `add_index`, and so
on. Because this is database-independent, it could be loaded into any database on.
that Active Record supports. This could be very useful if you were to
distribute an application that is able to run against multiple databases. `db/schema.rb` cannot express everything your database may support such as
triggers, sequences, stored procedures, check constraints, etc. While migrations
NOTE: `db/schema.rb` cannot express database specific items such as triggers, may use `execute` to create database constructs that are not supported by the
sequences, stored procedures or check constraints, etc. Please note that while Ruby migration DSL, these constructs may not be able to be reconstituted by the
custom SQL statements can be run in migrations, these statements cannot be reconstituted schema dumper. If you are using features like these, you should set the schema
by the schema dumper. If you are using features like this, then you format to `:sql` in order to get an accurate schema file that is useful to
should set the schema format to `:sql`. create new database instances.
Instead of using Active Record's schema dumper, the database's structure will When the schema format is set to `:sql`, the database structure will be dumped
be dumped using a tool specific to the database (via the `db:structure:dump` using a tool specific to the database into `db/structure.sql`. For example, for
rails task) into `db/structure.sql`. For example, for PostgreSQL, the `pg_dump` PostgreSQL, the `pg_dump` utility is used. For MySQL and MariaDB, this file will
utility is used. For MySQL and MariaDB, this file will contain the output of contain the output of `SHOW CREATE TABLE` for the various tables.
`SHOW CREATE TABLE` for the various tables.
To load the schema from `db/strcuture.sql`, run `rails db:structure:load`.
Loading these schemas is simply a question of executing the SQL statements they Loading this file is done by executing the SQL statements it contains. By
contain. By definition, this will create a perfect copy of the database's definition, this will create a perfect copy of the database's structure.
structure. Using the `:sql` schema format will, however, prevent loading the
schema into a RDBMS other than the one used to create it.
### Schema Dumps and Source Control ### Schema Dumps and Source Control
Because schema dumps are the authoritative source for your database schema, it Because schema files are commonly used to create new databases, it is strongly
is strongly recommended that you check them into source control. recommended that you check your schema file into source control.
`db/schema.rb` contains the current version number of the database. This Merge conflicts can occur in your schema file when two branches modify schema.
ensures conflicts are going to happen in the case of a merge where both To resolve these conflicts run `rails db:migrate` to regenerate the schema file.
branches touched the schema. When that happens, solve conflicts manually,
keeping the highest version number of the two.
Active Record and Referential Integrity Active Record and Referential Integrity
--------------------------------------- ---------------------------------------
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册