Skip to content

Commit

Permalink
Dump the database schema containing the current Rails version
Browse files Browse the repository at this point in the history
Since #42297, Rails now generates
datetime columns on MySQL with a default precision of 6. This means
that users upgrading to Rails 7.0 from 6.1, when loading the database
schema, would get the new precision value, which would not match the
production schema.

To avoid problems like this in the future,
Rails will now freeze `ActiveRecord::Schema` class to
be the 7.0 implementation, and will allow access to other version
using the same mechanism of `ActiveRecord::Migration`.

The schema dumper will generate the new format which will include the
Rails version and will look like this:

```
ActiveRecord::Schema[7.0].define
```

Related to #43934 and #43909.
  • Loading branch information
rafaelfranca committed Feb 7, 2022
1 parent 20846a2 commit 2d76373
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 41 deletions.
61 changes: 38 additions & 23 deletions activerecord/lib/active_record/schema.rb
Expand Up @@ -10,7 +10,7 @@ module ActiveRecord
#
# Usage:
#
# ActiveRecord::Schema.define do
# ActiveRecord::Schema[7.0].define do
# create_table :authors do |t|
# t.string :name, null: false
# end
Expand All @@ -30,32 +30,47 @@ module ActiveRecord
# ActiveRecord::Schema is only supported by database adapters that also
# support migrations, the two features being very similar.
class Schema < Migration::Current
# Eval the given block. All methods available to the current connection
# adapter are available within the block, so you can easily use the
# database definition DSL to build up your schema (
# {create_table}[rdoc-ref:ConnectionAdapters::SchemaStatements#create_table],
# {add_index}[rdoc-ref:ConnectionAdapters::SchemaStatements#add_index], etc.).
#
# The +info+ hash is optional, and if given is used to define metadata
# about the current schema (currently, only the schema's version):
#
# ActiveRecord::Schema.define(version: 2038_01_19_000001) do
# ...
# end
def self.define(info = {}, &block)
new.define(info, &block)
end
module Definition
extend ActiveSupport::Concern

module ClassMethods
# Eval the given block. All methods available to the current connection
# adapter are available within the block, so you can easily use the
# database definition DSL to build up your schema (
# {create_table}[rdoc-ref:ConnectionAdapters::SchemaStatements#create_table],
# {add_index}[rdoc-ref:ConnectionAdapters::SchemaStatements#add_index], etc.).
#
# The +info+ hash is optional, and if given is used to define metadata
# about the current schema (currently, only the schema's version):
#
# ActiveRecord::Schema[7.0].define(version: 2038_01_19_000001) do
# ...
# end
def define(info = {}, &block)
new.define(info, &block)
end
end

def define(info, &block) # :nodoc:
instance_eval(&block)

def define(info, &block) # :nodoc:
instance_eval(&block)
if info[:version].present?
connection.schema_migration.create_table
connection.assume_migrated_upto_version(info[:version])
end

if info[:version].present?
connection.schema_migration.create_table
connection.assume_migrated_upto_version(info[:version])
ActiveRecord::InternalMetadata.create_table
ActiveRecord::InternalMetadata[:environment] = connection.migration_context.current_environment
end
end

include Definition

ActiveRecord::InternalMetadata.create_table
ActiveRecord::InternalMetadata[:environment] = connection.migration_context.current_environment
def self.[](version)
@class_for_version ||= {}
@class_for_version[version] ||= Class.new(Migration::Compatibility.find(version)) do
include Definition
end
end
end
end
32 changes: 16 additions & 16 deletions activerecord/lib/active_record/schema_dumper.rb
Expand Up @@ -73,23 +73,23 @@ def define_params
@version ? "version: #{formatted_version}" : ""
end

# TODO: Fix dumper
def header(stream)
stream.puts <<HEADER
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(#{define_params}) do
HEADER
stream.puts <<~HEADER
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[#{ActiveRecord::Migration.current_version}].define(#{define_params}) do
HEADER
end

def trailer(stream)
Expand Down
Expand Up @@ -37,6 +37,19 @@ def test_has_primary_key
ActiveRecord::Base.primary_key_prefix_type = old_primary_key_prefix_type
end

def test_schema_without_version_is_the_current_version_schema
schema_class = ActiveRecord::Schema
assert schema_class < ActiveRecord::Migration[ActiveRecord::Migration.current_version]
assert_not schema_class < ActiveRecord::Migration[7.0]
assert schema_class < ActiveRecord::Schema::Definition
end

def test_schema_version_accessor
schema_class = ActiveRecord::Schema[6.1]
assert schema_class < ActiveRecord::Migration[6.1]
assert schema_class < ActiveRecord::Schema::Definition
end

def test_schema_define
ActiveRecord::Schema.define(version: 7) do
create_table :fruits do |t|
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/schema_dumper_test.rb
Expand Up @@ -38,6 +38,11 @@ def test_dump_schema_information_outputs_lexically_ordered_versions
ActiveRecord::SchemaMigration.delete_all
end

def test_schema_dump_include_migration_version
output = standard_dump
assert_match %r{ActiveRecord::Schema\[#{ActiveRecord::Migration.current_version}\]\.define}, output
end

def test_schema_dump
output = standard_dump
assert_match %r{create_table "accounts"}, output
Expand Down
2 changes: 1 addition & 1 deletion guides/source/active_record_migrations.md
Expand Up @@ -1038,7 +1038,7 @@ 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 migration:

```ruby
ActiveRecord::Schema.define(version: 2008_09_06_171750) do
ActiveRecord::Schema[7.1].define(version: 2008_09_06_171750) do
create_table "authors", force: true do |t|
t.string "name"
t.datetime "created_at"
Expand Down
2 changes: 1 addition & 1 deletion guides/source/active_record_postgresql.md
Expand Up @@ -98,7 +98,7 @@ NOTE: You need to enable the `hstore` extension to use hstore.

```ruby
# db/migrate/20131009135255_create_profiles.rb
ActiveRecord::Schema.define do
class CreateProfiles < ActiveRecord::Migration[7.0]
enable_extension 'hstore' unless extension_enabled?('hstore')
create_table :profiles do |t|
t.hstore 'settings'
Expand Down
29 changes: 29 additions & 0 deletions guides/source/upgrading_ruby_on_rails.md
Expand Up @@ -388,6 +388,35 @@ You can invalidate the cache either by touching the product, or changing the cac
<% end %>
```

### Rails version is now included in the Active Record schema dump

Rails 7.0 changed some default values for some column types. To avoid that application upgrading from 6.1 to 7.0
load the current schema using the new 7.0 defaults, Rails now includes the version of the framework in the schema dump.

Before loading the schema for the first time in Rails 7.0, make sure to run `rails app:update` to ensure that the
version of the schema is included in the schema dump.

The schema file will look like this:

```ruby
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[6.1].define(version: 2022_01_28_123512) do
```

NOTE: The first time you dump the schema with Rails 7.0, you will see many changes to that file, including
some column information. Make sure to review the new schema file content and commit it to your repository.

Upgrading from Rails 6.0 to Rails 6.1
-------------------------------------

Expand Down

0 comments on commit 2d76373

Please sign in to comment.