<commit_before>module Aether
  module RedshiftHelper
    def self.recreate_schema(connection:, schema_name:)
      query = [
        "DROP SCHEMA IF EXISTS #{schema_name} CASCADE",
        "CREATE SCHEMA IF NOT EXISTS #{schema_name}"
      ].join('; ')

      connection.exec(query)
    end

    def self.swap_schemas(
      connection:,
      staging_schema:,
      target_schema:,
      swap_schema:)

      target_schema_exists = RedshiftHelper::schema_exists?(
        connection: connection,
        schema_name: target_schema
      )

      if target_schema_exists
        connection.exec("DROP SCHEMA IF EXISTS #{swap_schema} CASCADE")

        swap_query = [
          "ALTER SCHEMA #{target_schema} RENAME TO #{swap_schema}",
          "ALTER SCHEMA #{staging_schema} RENAME TO #{target_schema}"
        ].join('; ')

        connection.exec(swap_query)

        connection.exec("DROP SCHEMA #{swap_schema} CASCADE")
      else
        connection.exec(
          "ALTER SCHEMA #{staging_schema} RENAME TO #{target_schema}"
        )
      end
    end

    private
<commit_msg>swap schemas in a single transaction<commit_after>module Aether
  module RedshiftHelper
    def self.recreate_schema(connection:, schema_name:)
      query = [
        "DROP SCHEMA IF EXISTS #{schema_name} CASCADE",
        "CREATE SCHEMA IF NOT EXISTS #{schema_name}"
      ].join('; ')

      connection.exec(query)
    end

    def self.swap_schemas(
      connection:,
      staging_schema:,
      target_schema:,
      swap_schema:)

      target_schema_exists = RedshiftHelper::schema_exists?(
        connection: connection,
        schema_name: target_schema
      )

      if target_schema_exists
        connection.exec("DROP SCHEMA IF EXISTS #{swap_schema} CASCADE")

        swap_query = [
          'BEGIN READ WRITE',
          "ALTER SCHEMA #{target_schema} RENAME TO #{swap_schema}",
          "ALTER SCHEMA #{staging_schema} RENAME TO #{target_schema}",
          'COMMIT'
        ].join('; ')

        connection.exec(swap_query)

        connection.exec("DROP SCHEMA #{swap_schema} CASCADE")
      else
        connection.exec(
          "ALTER SCHEMA #{staging_schema} RENAME TO #{target_schema}"
        )
      end
    end

    private

{'fraction_non_alphanumeric': 0.09808292465448061, 'fraction_numerical': 0.0, 'mean_word_length': 2.441717791411043, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3283440', 'n_tokens_mistral': 710, 'n_tokens_neox': 670, 'n_words': 155}