首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ruby-on-rails中设置Sqlite3的日志模式

如何在ruby-on-rails中设置Sqlite3的日志模式
EN

Stack Overflow用户
提问于 2019-05-24 13:13:40
回答 2查看 572关注 0票数 0

我尝试在我的Ruby-on-Rails项目中设置Sqlite3的日志模式。

ruby-on-rails似乎使用sqlite的默认日志模式,即'delete',因为当我更新数据库时,我在'db‘文件夹中看到了一个日志文件,当更新完成时,它被删除了。我希望将jouranl mode设置为"WAL“或"memory”。我尝试了Sqlite命令行

代码语言:javascript
复制
PRAGMA main.journal_mode=WAL

但它不会影响ruby-on-rails中的应用程序。

最后,我通过修改sqlite3_adapter.rb的源代码实现了这个功能

我更改了文件中的一个函数: activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb

代码语言:javascript
复制
def configure_connection  
    # here are original codes
    execute("PRAGMA journal_mode = WAL", "SCHEMA")  
end

因为SQLite3Adapter的初始化调用了configure_connection

这听起来并不是很好的解决方案,尽管它是有效的。在Ruby-on-Rails(版本是5.1.4)中,有没有更好的方法来设置Sqlite3的日志模式?例如配置选项

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-24 13:46:52

自从我不得不这样做已经有一段时间了,但是你应该能够使用初始化器,这样你就不需要修补源代码了。

将类似以下内容放入config/initializers/configure_sqlite_journal.rb

代码语言:javascript
复制
if c = ::ActiveRecord::Base.connection
    c.execute 'PRAGMA journal_mode = WAL'
end

应该做你想做的事

票数 2
EN

Stack Overflow用户

发布于 2020-08-01 23:37:58

我在这里找到了一个更好的答案:Speed up your Rails sqlite database for large dataset,更高的性能和配置:

代码(我把它放到config/initializers/speedup_sqlite3.rb中):

代码语言:javascript
复制
if ::ActiveRecord::Base.connection_config[:adapter] == 'sqlite3'
    if c = ::ActiveRecord::Base.connection
    # see http://www.sqlite.org/pragma.html for details

    # Page size of the database. The page size must be a power of two between 512 and 65536 inclusive
    c.execute 'PRAGMA main.page_size=4096;'

    # Suggested maximum number of database disk pages that SQLite will hold in memory at once per open database file
    c.execute 'PRAGMA main.cache_size=10000;'

    # Database connection locking-mode. The locking-mode is either NORMAL or EXCLUSIVE
    c.execute 'PRAGMA main.locking_mode=EXCLUSIVE;'

    # Setting of the "synchronous" flag, "NORMAL" means sync less often but still more than none
    c.execute 'PRAGMA main.synchronous=NORMAL;'

    # Journal mode for database, WAL=write-ahead log
    c.execute 'PRAGMA main.journal_mode=WAL;'

    # Storage location for temporary tables, indices, views, triggers
    c.execute 'PRAGMA main.temp_store = MEMORY;'
  end
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56286225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档