From 1cdc2d1771d3cd551e932bae9556b69613f96e6f Mon Sep 17 00:00:00 2001 From: Tom Tuddenham Date: Fri, 28 Jul 2023 20:36:58 +0930 Subject: [PATCH] 1122 support for Postgres prepare statement --- ...0230526212613_convert_to_active_storage.rb | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/db/migrate/20230526212613_convert_to_active_storage.rb b/db/migrate/20230526212613_convert_to_active_storage.rb index b51d269b4..b2fa8d38c 100644 --- a/db/migrate/20230526212613_convert_to_active_storage.rb +++ b/db/migrate/20230526212613_convert_to_active_storage.rb @@ -15,17 +15,33 @@ def up 'LASTVAL()' end - ActiveRecord::Base.connection.raw_connection.prepare(<<-SQL) - INSERT INTO active_storage_blobs ( - `key`, filename, content_type, metadata, byte_size, checksum, created_at - ) VALUES (?, ?, ?, '{}', ?, ?, ?) - SQL - - ActiveRecord::Base.connection.raw_connection.prepare(<<-SQL) - INSERT INTO active_storage_attachments ( - name, record_type, record_id, blob_id, created_at - ) VALUES (?, ?, ?, #{get_blob_id}, ?) - SQL + ActiveRecord::Base.connection.raw_connection.then do |conn| + if conn.is_a?(PG::Connection) + conn.prepare('active_storage_blobs', <<-SQL) + INSERT INTO active_storage_blobs ( + key, filename, content_type, metadata, byte_size, checksum, created_at + ) VALUES ($1, $2, $3, '{}', $4, $5, $6) + SQL + + conn.prepare('active_storage_attachments', <<-SQL) + INSERT INTO active_storage_attachments ( + name, record_type, record_id, blob_id, created_at + ) VALUES ($1, $2, $3, #{get_blob_id}, $4) + SQL + else + conn.raw_connection.prepare(<<-SQL) + INSERT INTO active_storage_blobs ( + `key`, filename, content_type, metadata, byte_size, checksum, created_at + ) VALUES (?, ?, ?, '{}', ?, ?, ?) + SQL + + conn.raw_connection.prepare(<<-SQL) + INSERT INTO active_storage_attachments ( + name, record_type, record_id, blob_id, created_at + ) VALUES (?, ?, ?, #{get_blob_id}, ?) + SQL + end + end Rails.application.eager_load! models = ActiveRecord::Base.descendants.reject { |model| model.abstract_class? || model == ActionMailbox::InboundEmail || model == ActionText::RichText }