<commit_before># frozen_string_literal: true

class ConvertToActiveStorage < ActiveRecord::Migration[5.2]
  require 'open-uri'

  ID_PARTITION_LIMIT = 1_000_000_000
  DIGEST = OpenSSL::Digest.const_get('SHA1').new

  def up
    ActiveRecord::Base.connection.raw_connection.prepare('active_storage_blob_statement', <<-SQL)
      INSERT INTO active_storage_blobs (
        key, filename, content_type, metadata, byte_size, checksum, created_at
      ) VALUES ($1, $2, $3, '{}', $4, $5, $6)
      RETURNING id;
    SQL

    ActiveRecord::Base.connection.raw_connection.prepare('active_storage_attachment_statement', <<-SQL)
      INSERT INTO active_storage_attachments (
        name, record_type, record_id, blob_id, created_at
      ) VALUES ($1, $2, $3, $4, $5)
    SQL

    Rails.application.eager_load!
    models = ApplicationRecord.descendants.reject(&:abstract_class?)

    transaction do
      models.each do |model|
        attachments = model.column_names.map do |c|
          $1 if c =~ /(.+)_file_name$/
        end.compact

        next if attachments.blank?

        model.find_each.each do |instance|
          attachments.each do |attachment|
            next if instance.__send__("#{attachment}_file_name").blank?

            res = ActiveRecord::Base.connection.raw_connection.exec_prepared(
              'active_storage_blob_statement', [
                key(instance, attachment),
                instance.__send__("#{attachment}_file_name"),
                instance.__send__("#{attachment}_content_type"),
                instance.__send__("#{attachment}_file_size") || 0,
                checksum(attachment),
                instance.updated_at.iso8601
              ]
            )

            ActiveRecord::Base.connection.raw_connection.exec_prepared(
              'active_storage_attachment_statement', [
                attachment,
                model.name,
                instance.id,
                res[0]['id'],
                instance.updated_at.iso8601
              ]<commit_msg>Fix active storage migration script (#2060)<commit_after># frozen_string_literal: true

class ConvertToActiveStorage < ActiveRecord::Migration[5.2]
  require 'open-uri'

  ID_PARTITION_LIMIT = 1_000_000_000
  DIGEST = OpenSSL::Digest.const_get('SHA1').new

  def up
    ActiveRecord::Base.connection.raw_connection.prepare('active_storage_blob_statement', <<-SQL)
      INSERT INTO active_storage_blobs (
        key, filename, content_type, metadata, byte_size, checksum, created_at
      ) VALUES ($1, $2, $3, '{}', $4, $5, $6)
      RETURNING id;
    SQL

    ActiveRecord::Base.connection.raw_connection.prepare('active_storage_attachment_statement', <<-SQL)
      INSERT INTO active_storage_attachments (
        name, record_type, record_id, blob_id, created_at
      ) VALUES ($1, $2, $3, $4, $5)
    SQL

    Rails.application.eager_load!
    models = ApplicationRecord.descendants.reject(&:abstract_class?)

    transaction do
      models.each do |model|
        next unless ActiveRecord::Base.connection.table_exists?(model.table_name)

        attachments = model.column_names.map do |c|
          $1 if c =~ /(.+)_file_name$/
        end.compact

        next if attachments.blank?

        model.find_each.each do |instance|
          attachments.each do |attachment|
            next if instance.__send__("#{attachment}_file_name").blank?

            res = ActiveRecord::Base.connection.raw_connection.exec_prepared(
              'active_storage_blob_statement', [
                key(instance, attachment),
                instance.__send__("#{attachment}_file_name"),
                instance.__send__("#{attachment}_content_type"),
                instance.__send__("#{attachment}_file_size") || 0,
                checksum(attachment),
                instance.updated_at.iso8601
              ]
            )

            ActiveRecord::Base.connection.raw_connection.exec_prepared(
              'active_storage_attachment_statement', [
                attachment,
                model.name,
                instance.id,
                res[0]['id'],
                instance.updated_at.iso8601
              ]
{'fraction_non_alphanumeric': 0.10811461594028413, 'fraction_numerical': 0.017818444497953285, 'mean_word_length': 2.453034081463009, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4149271', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1144, 'n_words': 208}