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

相关推荐
睡觉的时候不会困1 小时前
Redis 主从复制详解:原理、配置与主从切换实战
数据库·redis·bootstrap
程序员的世界你不懂3 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
自学也学好编程3 小时前
【数据库】Redis详解:内存数据库与缓存之王
数据库·redis
JAVA不会写3 小时前
在Mybatis plus中如何使用自定义Sql
数据库·sql
IT 小阿姨(数据库)4 小时前
PgSQL监控死元组和自动清理状态的SQL语句执行报错ERROR: division by zero原因分析和解决方法
linux·运维·数据库·sql·postgresql·centos
ChinaRainbowSea4 小时前
7. LangChain4j + 记忆缓存详细说明
java·数据库·redis·后端·缓存·langchain·ai编程
小马学嵌入式~5 小时前
嵌入式 SQLite 数据库开发笔记
linux·c语言·数据库·笔记·sql·学习·sqlite
Java小白程序员5 小时前
MyBatis基础到高级实践:全方位指南(中)
数据库·mybatis
Monly215 小时前
人大金仓:merge sql error, dbType null, druid-1.2.20
数据库·sql
不宕机的小马达6 小时前
【Mysql|第一篇】Mysql的安装与卸载、Navicat工具的使用
数据库·mysql