<commit_before>module SchemaEvolutionManager

  class Db

    attr_reader :url

    def initialize(url, opts={})
      @url = Preconditions.check_not_blank(url, "url cannot be blank")
      password = opts.delete(:password)
      Preconditions.assert_empty_opts(opts)
      connection_data = ConnectionData.parse_url(@url)

      if password
        ENV['PGPASSFILE'] = Db.password_to_tempfile(connection_data.pgpass(password))
      end
    end

    # Installs schema_evolution_manager. Automatically upgrades schema_evolution_manager.
    def bootstrap!
      scripts = Scripts.new(self, Scripts::BOOTSTRAP_SCRIPTS)
      dir = File.join(Library.base_dir, "scripts")
      scripts.each_pending(dir) do |filename, path|
        psql_file(filename, path)
        scripts.record_as_run!(filename)
      end
    end

    # executes a simple sql command.
    def psql_command(sql_command)
      Preconditions.assert_class(sql_command, String)
      command = "psql --no-align --tuples-only --command \"%s\" %s" % [sql_command, @url]
      Library.system_or_error(command)
    end

    def Db.attribute_values(path)
      Preconditions.assert_class(path, String)

      options = []

      ['quiet', 'no-align', 'tuples-only'].each do |v|
        options << "--#{v}"
      end

      SchemaEvolutionManager::MigrationFile.new(path).attribute_values.map do |value|
        if value.attribute.name == "transaction"
          if value.value == "single"
            options << "--single-transaction"
          elsif value.value == "none"
            # No-op
          else
            raise "File[%s] - attribute[%s] unsupported value[%s]" % [path, value.attribute.name, value.value]
          end
        else
          raise "File[%s] - unsupported attribute named[%s]" % [path, value.attribute.name]
        end
      end

      options
    end

    # executes sql commands from a file in a single transaction
    def psql_file(filename, path)
      Preconditions.assert_class(path, String)
      Preconditions.check_state(File.exists?(path), "File[%s] not found" % path)

      options = Db.attribute_values(path).join(" ")

      Library.with_temp_file(:prefix => File.basename(path)) do |tmp|
        File.open(tmp, "w") do |out|
          out << "\\set ON_ERROR_STOP true\n\n"
          out << IO.read(path)
        end

        command = "psql --file \"%s\" #{options} %s" % [tmp, @url]

        Library.with_temp_file do |output|
          result = `#{command} &> #{output}`.strip
          status = $?
          if status.to_i > 0
            errors = File.exists?(output) ? IO.read(output) : result
            raise ScriptError.new(self, filename, path, errors)
          end
        end
      end
    end

    # True if the specific schema exists; false otherwise
    def schema_schema_evolution_manager_exists?
      sql = "select count(*) from pg_namespace where nspname='%s'" % Db.schema_name
      psql_command(sql).to_i > 0
    end

    # Parses command line arguments returning an instance of
    # Db. Exists if invalid config.
    def Db.parse_command_line_config(arg_string)
      Preconditions.assert_class(arg_string, String)
      args = Args.new(arg_string, :optional => ['url', 'host', 'user', 'name', 'port'])
      Db.from_args(args)
    end

    # @param password: Optional password to use when connecting to the database.
    def Db.from_args(args, opts={})
      Preconditions.assert_class(args, Args)
      password = opts.delete(:password)
      Preconditions.assert_empty_opts(opts)

      if args.url<commit_msg>Update stderr redirect for compatibility with Windows (#45)<commit_after>module SchemaEvolutionManager

  class Db

    attr_reader :url

    def initialize(url, opts={})
      @url = Preconditions.check_not_blank(url, "url cannot be blank")
      password = opts.delete(:password)
      Preconditions.assert_empty_opts(opts)
      connection_data = ConnectionData.parse_url(@url)

      if password
        ENV['PGPASSFILE'] = Db.password_to_tempfile(connection_data.pgpass(password))
      end
    end

    # Installs schema_evolution_manager. Automatically upgrades schema_evolution_manager.
    def bootstrap!
      scripts = Scripts.new(self, Scripts::BOOTSTRAP_SCRIPTS)
      dir = File.join(Library.base_dir, "scripts")
      scripts.each_pending(dir) do |filename, path|
        psql_file(filename, path)
        scripts.record_as_run!(filename)
      end
    end

    # executes a simple sql command.
    def psql_command(sql_command)
      Preconditions.assert_class(sql_command, String)
      command = "psql --no-align --tuples-only --command \"%s\" %s" % [sql_command, @url]
      Library.system_or_error(command)
    end

    def Db.attribute_values(path)
      Preconditions.assert_class(path, String)

      options = []

      ['quiet', 'no-align', 'tuples-only'].each do |v|
        options << "--#{v}"
      end

      SchemaEvolutionManager::MigrationFile.new(path).attribute_values.map do |value|
        if value.attribute.name == "transaction"
          if value.value == "single"
            options << "--single-transaction"
          elsif value.value == "none"
            # No-op
          else
            raise "File[%s] - attribute[%s] unsupported value[%s]" % [path, value.attribute.name, value.value]
          end
        else
          raise "File[%s] - unsupported attribute named[%s]" % [path, value.attribute.name]
        end
      end

      options
    end

    # executes sql commands from a file in a single transaction
    def psql_file(filename, path)
      Preconditions.assert_class(path, String)
      Preconditions.check_state(File.exists?(path), "File[%s] not found" % path)

      options = Db.attribute_values(path).join(" ")

      Library.with_temp_file(:prefix => File.basename(path)) do |tmp|
        File.open(tmp, "w") do |out|
          out << "\\set ON_ERROR_STOP true\n\n"
          out << IO.read(path)
        end

        command = "psql --file \"%s\" #{options} %s" % [tmp, @url]

        Library.with_temp_file do |output|
          result = `#{command} > #{output} 2>&1`.strip
          status = $?
          if status.to_i > 0
            errors = File.exists?(output) ? IO.read(output) : result
            raise ScriptError.new(self, filename, path, errors)
          end
        end
      end
    end

    # True if the specific schema exists; false otherwise
    def schema_schema_evolution_manager_exists?
      sql = "select count(*) from pg_namespace where nspname='%s'" % Db.schema_name
      psql_command(sql).to_i > 0
    end

    # Parses command line arguments returning an instance of
    # Db. Exists if invalid config.
    def Db.parse_command_line_config(arg_string)
      Preconditions.assert_class(arg_string, String)
      args = Args.new(arg_string, :optional => ['url', 'host', 'user', 'name', 'port'])
      Db.from_args(args)
    end

    # @param password: Optional password to use when connecting to the database.
    def Db.from_args(args, opts={})
      Preconditions.assert_class(args, Args)
      password = opts.delete(:password)
      Preconditions.assert_empty_opts(opts)

      if args.url
{'fraction_non_alphanumeric': 0.1152875175315568, 'fraction_numerical': 0.0011220196353436186, 'mean_word_length': 2.6401225114854516, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 14, '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': '6520606', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2119, 'n_words': 519}