summaryrefslogtreecommitdiff
path: root/config/initializers/active_record_build_select.rb
diff options
context:
space:
mode:
authorJasper Maes <jaspermaes.jm@gmail.com>2019-01-15 22:05:36 +0100
committerJasper Maes <jaspermaes.jm@gmail.com>2019-03-11 21:44:22 +0100
commit3da8f4b90978a12ddbc282d5ec0be621d203f1b9 (patch)
tree48f49644020c07ce2dd1ffa30f8540db4eef228f /config/initializers/active_record_build_select.rb
parentfcf632ef99fee06de42216e769ddfeb28967f17c (diff)
downloadgitlab-ce-jlemaes/gitlab-ce-rails5.1.tar.gz
Upgrade Rails to 5.1.6.1jlemaes/gitlab-ce-rails5.1
Model.new.attributes now also returns encrypted attributes.
Diffstat (limited to 'config/initializers/active_record_build_select.rb')
-rw-r--r--config/initializers/active_record_build_select.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/config/initializers/active_record_build_select.rb b/config/initializers/active_record_build_select.rb
new file mode 100644
index 00000000000..d072f272e3c
--- /dev/null
+++ b/config/initializers/active_record_build_select.rb
@@ -0,0 +1,22 @@
+# rubocop:disable Gitlab/ModuleWithInstanceVariables
+
+# build_select only selects the required fields if the model has ignored_columns.
+# This is incompatible with some migrations or background migration specs because
+# rails keeps a statement cache in memory. So if a model with ignored_columns in a
+# migration is used, the query with select table.col1, table.col2 is stored in the
+# statement cache. If a different migration is then run and one of these columns is
+# removed in the meantime, the query is invalid.
+
+module ActiveRecord
+ module QueryMethods
+ private
+
+ def build_select(arel)
+ if select_values.any?
+ arel.project(*arel_columns(select_values.uniq))
+ else
+ arel.project(@klass.arel_table[Arel.star])
+ end
+ end
+ end
+end