Skip to content

Commit

Permalink
1122 support for Postgres prepare statement
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrisoxide committed Jul 28, 2023
1 parent 6eb1726 commit 1cdc2d1
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions db/migrate/20230526212613_convert_to_active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 1cdc2d1

Please sign in to comment.