ActiveRecord::Migration.maintain_test_schema!

测试gem: rspec-rails

问题描述

在使用 rspec-rails 进行测试时,出现了以下错误

ruby 复制代码
ActiveRecord::StatementInvalid: UndefinedFunction: ERROR: function init_id() does not exist

这个错误与数据库架构有关。

schema.rb中

create_table "users", id: :string, default: -> { "init_id()" }, force: :cascade do |t|

解决办法

为了解决这个问题,你需要在 config/environments/test.rb 文件中添加以下配置:

ruby 复制代码
config.active_record.schema_format = :sql

这会将数据库架构格式更改为 SQL 格式,以避免出现错误。

问题原因

这个问题的根本原因在于,在运行RSpec测试时,rspec-rails 试图更新数据库架构,但默认情况下,Rails 使用 schema.rb 文件来表示数据库架构,而不是 SQL 文件。因此,需要将配置更改为使用 SQL 格式的架构。

具体代码分析

rails_helper.rb

ruby 复制代码
# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
ActiveRecord::Migration.maintain_test_schema!

active_record/migration.rb 源码中

ruby 复制代码
 def maintain_test_schema! #:nodoc:
   if ActiveRecord::Base.maintain_test_schema
     suppress_messages { load_schema_if_pending! }
   end
 end

继续追溯代码:

ruby 复制代码
      def load_schema_if_pending!
        # 获取数据库配置信息
        current_db_config = Base.connection_db_config
        # 读取Rails.env 中的配置信息
        all_configs = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env)
        # 检查是否需要更新数据库架构(重点注意)
        needs_update = !all_configs.all? do |db_config|
          Tasks::DatabaseTasks.schema_up_to_date?(db_config, ActiveRecord::Base.schema_format)
        end
        # 如果需要更新的话,清楚所有的连接,并且执行bin/rails db:test:prepare
        if needs_update
          # Roundtrip to Rake to allow plugins to hook into database initialization.
          root = defined?(ENGINE_ROOT) ? ENGINE_ROOT : Rails.root
          FileUtils.cd(root) do
            Base.clear_all_connections!
            system("bin/rails db:test:prepare")
          end
        end

        # Establish a new connection, the old database may be gone (db:test:prepare uses purge)
        Base.establish_connection(current_db_config)

        check_pending!
      end

ActiveRecord::Base.schema_format

这个设置有两个选项:

  • :sql:当设置为 :sql 时,数据库架构将以数据库特定的 SQL 语句的形式导出。这意味着导出的文件将包含数据库特定的 SQL 命令,可以在相同类型的数据库中使用。

  • :ruby:当设置为 :ruby 时,数据库架构将以 ActiveRecord::Schema 的格式导出。这是一个通用的格式,可以加载到支持迁移的任何数据库中。这允许您在不同的数据库适配器之间共享相同的数据库架构定义,例如在开发和测试环境中使用不同的数据库

  • 默认使用ruby。

  • :ruby 格式下导出数据库架构时,通常不会包括数据库中定义的函数(方法)。这是因为 :ruby 格式主要用于导出数据库表格的结构和约束,以及与 ActiveRecord 迁移相关的信息,而不包括数据库中的自定义函数、存储过程或触发器。

相关推荐
Acrelhuang1 分钟前
8.3MW屋顶光伏+光储协同:上海汽车变速器低碳工厂的能源革命-安科瑞黄安南
大数据·数据库·人工智能·物联网·数据库开发
崖山数据库系统YashanDB29 分钟前
YashanDB json语法
数据库
陈三一32 分钟前
关于多数据源下Spring声明式事务管理失效问题的分析与解决
数据库·spring
我有医保我先冲1 小时前
SQL复杂查询与性能优化全攻略
数据库·sql·性能优化
烧瓶里的西瓜皮1 小时前
Go语言从零构建SQL数据库引擎(2)
数据库·sql·golang
SelectDB2 小时前
拉卡拉 x Apache Doris:统一金融场景 OLAP 引擎,查询提速 15 倍,资源直降 52%
大数据·数据库·数据分析
爱的叹息2 小时前
华为高斯(GaussDB) 集中式数据库 的开发技术手册,涵盖核心功能、开发流程、优化技巧及常见问题解决方案
数据库·gaussdb
背太阳的牧羊人2 小时前
使用 PyMuPDF(fitz)库打开 PDF 文件,并且是从内存中的字节流(BytesIO)读取 PDF 内容
数据库·pdf·文件处理·pymupdf·fitz
@淡 定3 小时前
MySQL MVCC 机制解析
数据库·mysql
Chandler243 小时前
Redis:内存淘汰原则,缓存击穿,缓存穿透,缓存雪崩
数据库·redis·缓存