@@ -39,11 +39,10 @@ def initialize(...)
3939 # Implementations may assume this method will only be called while
4040 # holding @lock (or from #initialize).
4141 #
42- # extends https://github.com/rails/rails/blob/main/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L691
42+ # overrides https://github.com/rails/rails/blob/main/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L691
4343 def configure_connection
44- super
45-
4644 configure_busy_handler_timeout
45+ check_version
4746 configure_pragmas
4847 configure_extensions
4948
@@ -76,7 +75,36 @@ def configure_busy_handler_timeout
7675 end
7776
7877 def configure_pragmas
79- @config . fetch ( :pragmas , [ ] ) . each do |key , value |
78+ defaults = {
79+ # Enforce foreign key constraints
80+ # https://www.sqlite.org/pragma.html#pragma_foreign_keys
81+ # https://www.sqlite.org/foreignkeys.html
82+ "foreign_keys" => "ON" ,
83+ # Impose a limit on the WAL file to prevent unlimited growth
84+ # https://www.sqlite.org/pragma.html#pragma_journal_size_limit
85+ "journal_size_limit" => 64 . megabytes ,
86+ # Set the local connection cache to 2000 pages
87+ # https://www.sqlite.org/pragma.html#pragma_cache_size
88+ "cache_size" => 2000
89+ }
90+ unless @memory_database
91+ defaults . merge! (
92+ # Journal mode WAL allows for greater concurrency (many readers + one writer)
93+ # https://www.sqlite.org/pragma.html#pragma_journal_mode
94+ "journal_mode" => "WAL" ,
95+ # Set more relaxed level of database durability
96+ # 2 = "FULL" (sync on every write), 1 = "NORMAL" (sync every 1000 written pages) and 0 = "NONE"
97+ # https://www.sqlite.org/pragma.html#pragma_synchronous
98+ "synchronous" => "NORMAL" ,
99+ # Set the global memory map so all processes can share some data
100+ # https://www.sqlite.org/pragma.html#pragma_mmap_size
101+ # https://www.sqlite.org/mmap.html
102+ "mmap_size" => 128 . megabytes
103+ )
104+ end
105+ pragmas = defaults . merge ( @config . fetch ( :pragmas , { } ) )
106+
107+ pragmas . each do |key , value |
80108 execute ( "PRAGMA #{ key } = #{ value } " , "SCHEMA" )
81109 end
82110 end
0 commit comments