如何强制清除Rails SQL缓存? [英] How can I force the rails SQL cache to clear?

查看:119
本文介绍了如何强制清除Rails SQL缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试脚本中的两次测试之间运行 rails db:reset db:migrate (该脚本直接导入并与模型交互),但是在第一个测试和第二次测试.更具体地说,由第一次测试引起的更改不会像应该的那样逆转.

I am running rails db:reset db:migrate in between tests from within my testing script (which imports and interfaces with the model directly), but the changes are not reflected between the first test and the second test. More specifically, the changes caused by the first test are not reversed as they should be.

当我从外壳外部(从外壳)连接到数据库时,我发现该命令已生效.

When I connect to the database externally (from the shell), I observe that the command has taken effect.

我已经看过这个问题,但是该解决方案没有任何效果(从字面上看,没有错误,但也没有明显的效果).

I have already looked at this question but the solution had no effect (quite literally, there was no error but also no discernible effect).

如何强制我的测试脚本清除其sqlite状态的内存缓存?

How can I force my test script to clear its in-memory cache of the sqlite state?

  1. 创建一个新的Rails应用程序.

  1. Create a new rails app.

rails new MWE

  • 将以下内容放入 db/schema.rb

    ActiveRecord::Schema.define(version: 20140408213603) do
        create_table "users", force: true do |t|
        t.string   "username"
      end
    end
    

  • 将以下内容放入 db/seed.rb .

    User.create(username: 'user1')
    User.create(username: 'user2')
    User.create(username: 'user3')
    

  • 将以下内容放入 Gemfile 中.

    source 'https://rubygems.org' 
    gem 'rails', '4.0.0' 
    gem 'sqlite3' 
    gem 'protected_attributes' 
    

  • 将以下内容放入名为 app/models/user.rb 的文件中.

    class User < ActiveRecord::Base
      attr_accessible :username
    end
    

  • 运行以下命令.

  • Run the following commands.

    bundle install
    rake db:reset
    

  • 将以下内容放入名为 MWE.rb

    load 'config/application.rb'
    load 'config/environment.rb'
    load 'app/models/user.rb'
    
    # Mimic the unsafe call in the source code
    system("bundle exec rake db:reset")
    puts User.count
    User.destroy_all("username = 'user3'")
    puts User.count
    system("bundle exec rake db:reset")
    puts User.count
    User.destroy_all("username = 'user3'")
    puts User.count
    

  • 运行文件并观察实际输出.

    $ ruby MWE.rb
    -- create_table("users", {:force=>true})
       -> 0.0362s
    -- initialize_schema_migrations_table()
       -> 0.0248s
    3
    2
    -- create_table("users", {:force=>true})
       -> 0.0809s
    -- initialize_schema_migrations_table()
       -> 0.0490s
    2
    2
    

  • 所需的输出

        -- create_table("users", {:force=>true})
           -> 0.0362s
        -- initialize_schema_migrations_table()
           -> 0.0248s
        3
        2
        -- create_table("users", {:force=>true})
           -> 0.0809s
        -- initialize_schema_migrations_table()
           -> 0.0490s
        3
        2
    

    如何强制将数据库重置反映在模型中?

    推荐答案

    您需要做的就是重新建立数据库连接.试试:

    All you need to do is to reestablish database connection. Try:

    system("bundle exec rake db:reset")
    puts User.count
    User.destroy_all("username = 'user3'")
    puts User.count
    
    system("bundle exec rake db:reset")
    ActiveRecord::Base.clear_all_connections!
    puts User.count
    User.destroy_all("username = 'user3'")
    puts User.count
    

    主要问题是:为什么需要这样做?我敢肯定,有一种更好的方法可以实现您想要的目标.

    The main question is: why do you need to do this? I am pretty certain there is a better way to achieve what you want.

    这篇关于如何强制清除Rails SQL缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆