Skip to content

Commit f59b081

Browse files
committed
Change SQLite3Adapter to always represent boolean values as integers
1 parent 0bef23e commit f59b081

File tree

8 files changed

+44
-109
lines changed

8 files changed

+44
-109
lines changed

activerecord/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Deprecate `config.activerecord.sqlite3.represent_boolean_as_integer`.
2+
3+
*Rafael Mendonça França*
4+
5+
* Change `SQLite3Adapter` to always represent boolean values as integers.
6+
7+
*Rafael Mendonça França*
8+
19
* Remove ability to specify a timestamp name for `#cache_key`.
210

311
*Rafael Mendonça França*

activerecord/lib/active_record/connection_adapters/sqlite3/quoting.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ def quoted_binary(value)
3030
end
3131

3232
def quoted_true
33-
ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? "1" : "'t'"
33+
"1"
3434
end
3535

3636
def unquoted_true
37-
ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? 1 : "t"
37+
1
3838
end
3939

4040
def quoted_false
41-
ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? "0" : "'f'"
41+
"0"
4242
end
4343

4444
def unquoted_false
45-
ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? 0 : "f"
45+
0
4646
end
4747

4848
private

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,15 @@ class SQLite3Adapter < AbstractAdapter
7676
json: { name: "json" },
7777
}
7878

79-
##
80-
# :singleton-method:
81-
# Indicates whether boolean values are stored in sqlite3 databases as 1
82-
# and 0 or 't' and 'f'. Leaving <tt>ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer</tt>
83-
# set to false is deprecated. SQLite databases have used 't' and 'f' to
84-
# serialize boolean values and must have old data converted to 1 and 0
85-
# (its native boolean serialization) before setting this flag to true.
86-
# Conversion can be accomplished by setting up a rake task which runs
87-
#
88-
# ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 1)
89-
# ExampleModel.where("boolean_column = 'f'").update_all(boolean_column: 0)
90-
# for all models and all boolean columns, after which the flag must be set
91-
# to true by adding the following to your <tt>application.rb</tt> file:
92-
#
93-
# Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
94-
class_attribute :represent_boolean_as_integer, default: false
79+
def self.represent_boolean_as_integer=(value) # :nodoc:
80+
if value == false
81+
raise "`.represent_boolean_as_integer=` is now always true, so make sure your application can work with it and remove this settings."
82+
end
83+
84+
ActiveSupport::Deprecation.warn(
85+
"`.represent_boolean_as_integer=` is now always true, so setting this is deprecated and will be removed in Rails 6.1."
86+
)
87+
end
9588

9689
class StatementPool < ConnectionAdapters::StatementPool # :nodoc:
9790
private

activerecord/lib/active_record/railtie.rb

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,18 @@ class Railtie < Rails::Railtie # :nodoc:
167167

168168
initializer "active_record.set_configs" do |app|
169169
ActiveSupport.on_load(:active_record) do
170-
configs = app.config.active_record.dup
170+
configs = app.config.active_record
171+
172+
represent_boolean_as_integer = configs.sqlite3.delete(:represent_boolean_as_integer)
173+
174+
unless represent_boolean_as_integer.nil?
175+
ActiveSupport.on_load(:active_record_sqlite3adapter) do
176+
ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = represent_boolean_as_integer
177+
end
178+
end
179+
171180
configs.delete(:sqlite3)
181+
172182
configs.each do |k, v|
173183
send "#{k}=", v
174184
end
@@ -236,35 +246,6 @@ class Railtie < Rails::Railtie # :nodoc:
236246
end
237247
end
238248

239-
initializer "active_record.check_represent_sqlite3_boolean_as_integer" do
240-
config.after_initialize do
241-
ActiveSupport.on_load(:active_record_sqlite3adapter) do
242-
represent_boolean_as_integer = Rails.application.config.active_record.sqlite3.delete(:represent_boolean_as_integer)
243-
unless represent_boolean_as_integer.nil?
244-
ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = represent_boolean_as_integer
245-
end
246-
247-
unless ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer
248-
ActiveSupport::Deprecation.warn <<-MSG
249-
Leaving `ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer`
250-
set to false is deprecated. SQLite databases have used 't' and 'f' to serialize
251-
boolean values and must have old data converted to 1 and 0 (its native boolean
252-
serialization) before setting this flag to true. Conversion can be accomplished
253-
by setting up a rake task which runs
254-
255-
ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 1)
256-
ExampleModel.where("boolean_column = 'f'").update_all(boolean_column: 0)
257-
258-
for all models and all boolean columns, after which the flag must be set to
259-
true by adding the following to your application.rb file:
260-
261-
Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
262-
MSG
263-
end
264-
end
265-
end
266-
end
267-
268249
initializer "active_record.set_filter_attributes" do
269250
ActiveSupport.on_load(:active_record) do
270251
self.filter_attributes += Rails.application.config.filter_parameters

activerecord/test/cases/adapters/sqlite3/quoting_test.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66

77
class SQLite3QuotingTest < ActiveRecord::SQLite3TestCase
88
def setup
9+
super
910
@conn = ActiveRecord::Base.connection
10-
@initial_represent_boolean_as_integer = ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer
11-
end
12-
13-
def teardown
14-
ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = @initial_represent_boolean_as_integer
1511
end
1612

1713
def test_type_cast_binary_encoding_without_logger
@@ -22,18 +18,10 @@ def test_type_cast_binary_encoding_without_logger
2218
end
2319

2420
def test_type_cast_true
25-
ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = false
26-
assert_equal "t", @conn.type_cast(true)
27-
28-
ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = true
2921
assert_equal 1, @conn.type_cast(true)
3022
end
3123

3224
def test_type_cast_false
33-
ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = false
34-
assert_equal "f", @conn.type_cast(false)
35-
36-
ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = true
3725
assert_equal 0, @conn.type_cast(false)
3826
end
3927

guides/source/configuring.md

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -387,28 +387,6 @@ The PostgreSQL adapter adds one additional configuration option:
387387
highly recommended that you do not enable this in a production environment.
388388
Defaults to `false` in all environments.
389389

390-
The SQLite3Adapter adapter adds one additional configuration option:
391-
392-
* `ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer`
393-
indicates whether boolean values are stored in sqlite3 databases as 1 and 0 or
394-
't' and 'f'. Leaving `ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer`
395-
set to false is deprecated. SQLite databases have used 't' and 'f' to serialize
396-
boolean values and must have old data converted to 1 and 0 (its native boolean
397-
serialization) before setting this flag to true. Conversion can be accomplished
398-
by setting up a Rake task which runs
399-
400-
```ruby
401-
ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 1)
402-
ExampleModel.where("boolean_column = 'f'").update_all(boolean_column: 0)
403-
```
404-
405-
for all models and all boolean columns, after which the flag must be set to true
406-
by adding the following to your `application.rb` file:
407-
408-
```ruby
409-
Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
410-
```
411-
412390
The schema dumper adds two additional configuration options:
413391

414392
* `ActiveRecord::SchemaDumper.ignore_tables` accepts an array of tables that should _not_ be included in any generated schema file.
@@ -896,7 +874,6 @@ text/javascript image/svg+xml application/postscript application/x-shockwave-fla
896874
#### With '5.2':
897875

898876
- `config.active_record.cache_versioning`: `true`
899-
- `ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer`: `true`
900877
- `action_dispatch.use_authenticated_cookie_encryption`: `true`
901878
- `config.active_support.use_authenticated_message_encryption`: `true`
902879
- `config.active_support.use_sha1_digests`: `true`

railties/lib/rails/application/configuration.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,6 @@ def load_defaults(target_version)
9797

9898
if respond_to?(:active_record)
9999
active_record.cache_versioning = true
100-
# Remove the temporary load hook from SQLite3Adapter when this is removed
101-
ActiveSupport.on_load(:active_record_sqlite3adapter) do
102-
ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = true
103-
end
104100
end
105101

106102
if respond_to?(:action_dispatch)

railties/test/application/configuration_test.rb

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,37 +1941,29 @@ def index
19411941
assert_equal({}, Rails.application.config.my_custom_config)
19421942
end
19431943

1944-
test "default SQLite3Adapter.represent_boolean_as_integer for 5.1 is false" do
1944+
test "represent_boolean_as_integer is deprecated" do
19451945
remove_from_config '.*config\.load_defaults.*\n'
19461946

1947-
app_file "app/models/post.rb", <<-RUBY
1948-
class Post < ActiveRecord::Base
1949-
end
1947+
app_file "config/initializers/new_framework_defaults_6_0.rb", <<-RUBY
1948+
Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
19501949
RUBY
19511950

1952-
app "development"
1953-
force_lazy_load_hooks { Post }
1954-
1955-
assert_not ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer
1956-
end
1957-
1958-
test "default SQLite3Adapter.represent_boolean_as_integer for new installs is true" do
19591951
app_file "app/models/post.rb", <<-RUBY
19601952
class Post < ActiveRecord::Base
19611953
end
19621954
RUBY
19631955

19641956
app "development"
1965-
force_lazy_load_hooks { Post }
1966-
1967-
assert ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer
1957+
assert_deprecated do
1958+
force_lazy_load_hooks { Post }
1959+
end
19681960
end
19691961

1970-
test "represent_boolean_as_integer should be able to set via config.active_record.sqlite3.represent_boolean_as_integer" do
1962+
test "represent_boolean_as_integer raises when the value is false" do
19711963
remove_from_config '.*config\.load_defaults.*\n'
19721964

19731965
app_file "config/initializers/new_framework_defaults_6_0.rb", <<-RUBY
1974-
Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
1966+
Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = false
19751967
RUBY
19761968

19771969
app_file "app/models/post.rb", <<-RUBY
@@ -1980,9 +1972,9 @@ class Post < ActiveRecord::Base
19801972
RUBY
19811973

19821974
app "development"
1983-
force_lazy_load_hooks { Post }
1984-
1985-
assert ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer
1975+
assert_raises(RuntimeError) do
1976+
force_lazy_load_hooks { Post }
1977+
end
19861978
end
19871979

19881980
test "config_for containing ERB tags should evaluate" do

0 commit comments

Comments
 (0)