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 迁移相关的信息,而不包括数据库中的自定义函数、存储过程或触发器。

相关推荐
山茶花.13 分钟前
SQL注入总结
数据库·sql·oracle
m0_736919101 小时前
超越Python:下一步该学什么编程语言?
jvm·数据库·python
m0_748229991 小时前
ThinkPHP快速入门:从零到实战
c语言·开发语言·数据库·学习
阿蒙Amon1 小时前
C#每日面试题-Thread.Sleep和Task.Delay的区别
java·数据库·c#
沉舟侧畔千帆过_1 小时前
一个DBA的真心话:搞定Oracle+PG双库,我就靠这招
数据库·oracle·dba
醉风塘1 小时前
【终极解决方案】Oracle ORA-01795错误:IN列表1000条限制的全面突破指南
数据库·oracle
信创天地1 小时前
从 Oracle 到国产数据库:迁移后成本直降 60%、性能反超 30% 的实战秘籍
数据库·oracle
Mikhail_G2 小时前
Mysql数据库操作指南——排序(零基础篇十)
大数据·数据库·sql·mysql·数据分析
沉舟侧畔千帆过_2 小时前
能源核心系统国产化攻坚:智能电网调度系统从 Oracle 到金仓 KES 迁移实录
数据库·oracle·能源·kingbase·金仓数据库
chengrise2 小时前
Oracle EBS 成本异常排查全指南:差异分摊、成本回滚场景与解决方案
运维·数据库·oracle·erp·ebs