aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2017-07-12 23:21:07 +0930
committerGitHub <noreply@github.com>2017-07-12 23:21:07 +0930
commit58f10a31b37e9bb6e975a71aa63744f318ee043d (patch)
tree0c1fae69495ddfc24e4ad8fede784b4b5f4947fb /activerecord/lib
parent21344061dc17151fe749b9dc76a127d597ec43ef (diff)
parent52e050ed00b023968fecda82f19a858876a7c435 (diff)
downloadrails-58f10a31b37e9bb6e975a71aa63744f318ee043d.tar.gz
rails-58f10a31b37e9bb6e975a71aa63744f318ee043d.tar.bz2
rails-58f10a31b37e9bb6e975a71aa63744f318ee043d.zip
Merge pull request #29699 from lugray/represent_boolean_as_integer
Change sqlite3 boolean serialization to use 1 and 0
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/quoting.rb8
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite3/quoting.rb16
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb18
-rw-r--r--activerecord/lib/active_record/railtie.rb24
4 files changed, 62 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
index 61233dcc51..de78c6db44 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
@@ -106,19 +106,19 @@ module ActiveRecord
end
def quoted_true
- "'t'".freeze
+ "TRUE".freeze
end
def unquoted_true
- "t".freeze
+ true
end
def quoted_false
- "'f'".freeze
+ "FALSE".freeze
end
def unquoted_false
- "f".freeze
+ false
end
# Quote date/time values for use in SQL input. Includes microseconds
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3/quoting.rb b/activerecord/lib/active_record/connection_adapters/sqlite3/quoting.rb
index 7276a65098..04dd7fd357 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite3/quoting.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite3/quoting.rb
@@ -22,6 +22,22 @@ module ActiveRecord
"x'#{value.hex}'"
end
+ def quoted_true
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? "1".freeze : "'t'".freeze
+ end
+
+ def unquoted_true
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? 1 : "t".freeze
+ end
+
+ def quoted_false
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? "0".freeze : "'f'".freeze
+ end
+
+ def unquoted_false
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? 0 : "f".freeze
+ end
+
private
def _type_cast(value)
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
diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb
index 9cca103a18..962ed880b9 100644
--- a/activerecord/lib/active_record/railtie.rb
+++ b/activerecord/lib/active_record/railtie.rb
@@ -174,5 +174,29 @@ end_warning
end
end
end
+
+ initializer "active_record.check_represent_sqlite3_boolean_as_integer" do
+ config.after_initialize do
+ ActiveSupport.on_load(:active_record_sqlite3adapter) do
+ unless ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer
+ ActiveSupport::Deprecation.warn <<-MSG
+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
+MSG
+ end
+ end
+ end
+ end
end
end