diff --git a/activerecord/lib/active_record/schema.rb b/activerecord/lib/active_record/schema.rb index aba25fb3757ca..0528d17ee7d15 100644 --- a/activerecord/lib/active_record/schema.rb +++ b/activerecord/lib/active_record/schema.rb @@ -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 @@ -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 diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index c97d0899c8313..05aa4d43b40ca 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -73,23 +73,23 @@ def define_params @version ? "version: #{formatted_version}" : "" end + # TODO: Fix dumper def header(stream) - stream.puts <
``` +### 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 -------------------------------------