database_cleaner 正在擦除我的开发数据库

2024-03-30

I have database-cleaner为我的 Rails 4 应用程序配置, 每次运行测试时,我发现我的数据库在两个版本中都被清除了test and development环境。

我的配置在rails_helper如下:

ENV["RAILS_ENV"] ||= 'test'
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'database_cleaner'
Rails.env = "test"
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = false

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end


  config.mock_with :rspec

  config.before(:all) do
    ActiveRecord::Base.skip_callbacks = true
  end

  config.after(:all) do
    ActiveRecord::Base.skip_callbacks = false
  end

end

我怎样才能确保清洁器只擦除数据库test environment不碰我的development?

My database.yml如下:

# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

development:
  <<: *default
  database: directory-service_development

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user that initialized the database.
  #username: directory-service

  # The password associated with the postgres role (username).
  #password:

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: directory-service_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: directory-service_production
  username: directory-service
  password: <%= ENV['DIRECTORY-SERVICE_DATABASE_PASSWORD'] %>

我建议改变

ENV["RAILS_ENV"] ||= 'test'

to

ENV["RAILS_ENV"] = 'test'

并删除

Rails.env = 'test'

因为 RAILS_ENV 环境变量应该足以进行配置

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

database_cleaner 正在擦除我的开发数据库 的相关文章

随机推荐

  • 下载 ASP.NET MVC C# 中字节数组列表中包含的多个文件

    我正在开发一个 ASP NET MVC 5 应用程序 我编写了一个代码 允许我下载存储在 SQL Server 数据库中的文件 varbinary 我可以使用以下命令下载单个文件 public JsonResult PrepareSingl
  • 如何将公历转换为中国农历?

    我想使用公历到中国农历构建一个Android应用程序 我不知道如何从公历转换为农历 我怎样才能做到这一点 从公历到中文的转换 我刚刚发布了Time4J的新版本 v4 35 但是使用Time4A https github com MenoDa
  • 在caffe中定义新层时如何获取学习率或迭代次数

    我想当迭代次数达到一定次数时改变损失层中的损失计算方法 为了实现它 我认为我需要获取当前的学习率或迭代次数 然后我使用if短语选择是否改变损失计算方法 您可以添加一个成员变量咖啡类保存当前的学习率或迭代次数并在您想要的层中访问它 例如 要获
  • 如何从命令行删除钥匙串引用

    From the Keychain app I have the option to delete just the reference to a listed Keychain that was unlocked and listed v
  • 使用元类覆盖复杂内置方法

    作为学习练习 我正在尝试实现一个类来模拟 python 的行为complex内置的 但具有不同的行为 str and repr 方法 我希望它们以以下格式打印 1 0 2 0 代替 1 2j 我首先尝试简单地从complex并重新定义 st
  • 在 Fortran 语言中 advance='no' 是什么意思?

    我是 Fortran 新手 我不明白这一行 write a35 advance no in program democonvertion implicit none real tc tf tr tk write a35 advance no
  • Windows RT .NET 兼容性

    我只需要知道我是否需要相同的框架来开发适用于平板电脑 Windows RT 和 PC 的 Windows 8 应用程序 或者平板电脑仍然使用 NET 紧凑框架 在 Windows RT 即 Windows 8 的平板电脑 ARM 版本 上运
  • C# 中的字节数组加密

    我想使用按位运算符创建一个很好的密码学 然而我没有这样做 我希望它具有使用字节数组的按位运算符来加密和解密我的字节数组 public class Cryptographer private byte Keys get set public
  • 从 F# 访问名称中包含 .base 的命名空间

    正如标题所示 我试图使用在名称中包含 base 的名称空间中声明的类 考虑如下情况 open Foo base Bar 在 C 中 我只是在基数之前使用 但 F 似乎忽略了这一点 并认为 是用于列表连接的中缀运算符 由于命名空间属于我无法修
  • 创建每个键包含多个值的字典

    如何创建一个字典 其中每个键包含 2 个列表中的多个值 例如 我有 gt gt gt list1 fruit fruit vegetable gt gt gt list2 apple banana carrot 而且 我想要一些效果 gt
  • 将 jquery masonry 集成到 zurb 基础网格中

    我在将砌体与基础网格布局集成时遇到问题 本质上 对于具有不同高度的相同宽度图像 砌体按预期工作 但在某些断点处 网格将仅具有两列布局 而不是通常的四列布局 但是 如果您继续最小化浏览器宽度 则会返回四列 因此不可能没有空间来显示它们 var
  • 通过父id和mysql中的where子句获取所有子项

    我有一个表 将 id 和parent id 存储在同一个表中 我想要一个接受parent id 作为参数并返回第n 级的所有子节点的递归查询 为此 我正在使用此代码并为我正常工作 select id name parent from sel
  • 如何使用 PowerShell 发送电子邮件

    我想从 PowerShell 发送电子邮件 所以我使用这个命令 EmailFrom email protected cdn cgi l email protection EmailTo email protected cdn cgi l e
  • 使用 eval 和 eval-source-map 有什么区别?

    我正在使用 webpack 来配置源映射 我想知道有人可以澄清 eval 和 eval source map 之间的区别吗 我个人看不出有什么区别 来自文档 https webpack js org configuration devtoo
  • .NET Core 的 IWebProxy 实现的位置

    System Net IWebProxy 的可用实现是什么 来自 System Net Primitives DNX Core 根据应用程序要求 dnxcore50 中可以使用唯一的框架 那么包含代理实现的正确 NuGet 包是什么 解决此
  • 在运行时测试 iOS 版本特定功能的推荐方法

    我的目标是 IOS 4 3 和 5 0 其中一个应用程序是针对 5 0 SDK 构建的 并且希望仅当该应用程序在 iOS5 设备上运行时才添加对 iOS5 中引入的 Twitter 功能的支持 在运行时可靠地测试这些操作系统功能的可用性而不
  • Apollo GraphQL 请求被取消

    我正在使用 React 类型前置文本字段组件Downshift and react apollo 当用户输入时 我正在查询输入建议并将其显示在文本字段下方 不幸的是 这段经历并不顺利 由于某种原因 Apollo 时不时会取消 50 以上的请
  • 使用 facebook graph api 搜索好友

    Facebook 是否有用于搜索用户好友的 API Tim 您可以这样搜索朋友的名字 select uid name sex from user where uid in SELECT uid2 FROM friend WHERE uid1
  • 对于阿拉伯语应用程序,英文字母的顺序相反(在 wkwebview 内)

    您好 当我将操作系统更新到版本 14 时 我在 webview 中看到了一个问题 即我的阿拉伯应用程序上的英文字母顺序相反 我的应用程序本地化设置为 Localization native development region ar 并使用
  • database_cleaner 正在擦除我的开发数据库

    I have database cleaner为我的 Rails 4 应用程序配置 每次运行测试时 我发现我的数据库在两个版本中都被清除了test and development环境 我的配置在rails helper如下 ENV RAIL