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

相关推荐
micromicrofat10 分钟前
【MongoDB】WSL2访问宿主机的MongoDB
数据库·mongodb
两拆15 分钟前
Redhat7.9安装部署Oracle 19C
数据库·oracle
TDengine (老段)35 分钟前
TDengine 企业用户建表规模有多大?
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
byzh_rc1 小时前
[算法设计与分析-从入门到入土] 复杂算法
数据库·人工智能·算法·机器学习·支持向量机
白露与泡影1 小时前
详细描述一条 SQL 语句在 MySQL 中的执行过程。
数据库·sql·mysql
qq_316837751 小时前
mysql mybatisPlus 存储经纬度
数据库·mysql
杀死那个蝈坦2 小时前
短链接生成-基于布隆过滤器和唯一索引
java·数据库·微服务·oracle·rocketmq
3824278272 小时前
使用 webdriver-manager配置geckodriver
java·开发语言·数据库·爬虫·python
惜分飞2 小时前
Oracle Recovery Tools 使用说明
数据库·oracle·oracle恢复·替代bbed·oracle恢复工具
如旧呀2 小时前
爬虫小知识
数据库·爬虫·mysql