diff options
author | Lisa Ugray <lisa.ugray@shopify.com> | 2017-07-06 12:59:33 -0400 |
---|---|---|
committer | Lisa Ugray <lisa.ugray@shopify.com> | 2017-07-11 14:52:46 -0400 |
commit | 52e050ed00b023968fecda82f19a858876a7c435 (patch) | |
tree | 3386fd2fd194e7926076fce9084a9fbc65013c13 /activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb | |
parent | 07ed697f7b0debd8736a188fad67fe5e0c98739e (diff) | |
download | rails-52e050ed00b023968fecda82f19a858876a7c435.tar.gz rails-52e050ed00b023968fecda82f19a858876a7c435.tar.bz2 rails-52e050ed00b023968fecda82f19a858876a7c435.zip |
Change sqlite3 boolean serialization to use 1 and 0
Abstract boolean serialization has been using 't' and 'f', with MySQL
overriding that to use 1 and 0.
This has the advantage that SQLite natively recognizes 1 and 0 as true
and false, but does not natively recognize 't' and 'f'.
This change in serialization requires a migration of stored boolean data
for SQLite databases, so it's implemented behind a configuration flag
whose default false value is deprecated. The flag itself can be
deprecated in a future version of Rails. While loaded models will give
the correct result for boolean columns without migrating old data,
where() clauses will interact incorrectly with old data.
While working in this area, also change the abstract adapter to use
`"TRUE"` and `"FALSE"` as quoted values and `true` and `false` for
unquoted. These are supported by PostreSQL, and MySQL remains
overriden.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 04129841e4..b79dbe0733 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -72,6 +72,23 @@ module ActiveRecord boolean: { name: "boolean" } } + ## + # :singleton-method: + # Indicates whether boolean values are stored in sqlite3 databases as 1 + # and 0 or 't' and 'f'. Leaving `ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer` + # set to false is deprecated. SQLite databases have used 't' and 'f' to + # serialize boolean values and must have old data converted to 1 and 0 + # (its native boolean serialization) before setting this flag to true. + # Conversion can be accomplished by setting up a rake task which runs + # + # ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 1) + # ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 0) + # for all models and all boolean columns, after which the flag must be set + # to true by adding the following to your application.rb file: + # + # ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = true + class_attribute :represent_boolean_as_integer, default: false + class StatementPool < ConnectionAdapters::StatementPool private @@ -512,5 +529,6 @@ module ActiveRecord execute("PRAGMA foreign_keys = ON", "SCHEMA") end end + ActiveSupport.run_load_hooks(:active_record_sqlite3adapter, SQLite3Adapter) end end |