<commit_before>
module Hooky
  module Postgresql

    DEFAULT_PRIVILEGES = [
      {
        :privilege => "ALL PRIVILEGES",
        :type => "DATABASE",
        :column => nil,
        :on => "gonano",
        :with_grant => true
      }
    ]

    DEFAULT_META = {
      :privileges => DEFAULT_PRIVILEGES,
      :roles => [
        "SUPERUSER"
      ]
    }

    DEFAULT_USERS = [
      {
        :username => "nanobox",
        :meta => DEFAULT_META
      }
    ]

    USER_META_PRVILIGES_DEFAULTS = {
      privilege:    {type: :string, from: ["SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER", "ALL", "ALL PRIVILEGES", "USAGE", "CREATE", "CONNECT", "TEMPORARY", "TEMP", "EXECUTE"], default: "SELECT"},
      type:         {type: :string, from: ["TABLE", "ALL TABLES IN SCHEMA", "SEQUENCE", "ALL SEQUENCES IN SCHEMA", "DATABASE", "FOREIGN DATA WRAPPER", "FOREIGN SERVER", "FUNCTION", "ALL FUNCTIONS IN SCHEMA", "LANGUAGE", "LARGE OBJECT", "SCHEMA", "TABLESPACE"], default: "DATABASE"},
      column:       {type: :string, default: nil},
      on:           {type: :string, default: 'gonano'},
      with_grant:   {type: :on_off, default: false}
    }

    USER_META_DEFAULTS = {
      privileges:    {type: :array, of: :hash, template: USER_META_PRVILIGES_DEFAULTS, default: DEFAULT_PRIVILEGES},
      roles:         {type: :array, of: :string, default: []}
    }

    USER_DEFAULTS = {
      username:      {type: :string, default: 'gonano'},
      meta:          {type: :hash, template: USER_META_DEFAULTS, default: DEFAULT_META}
    }

    CONFIG_DEFAULTS = {
      # global settings
      before_deploy: {type: :array, of: :string, default: []},
      after_deploy:  {type: :array, of: :string, default: []},
      hook_ref:      {type: :string, default: "stable"},
      # postgresql settings
      locale:        {type: :string, default: 'en_US.UTF-8'},
      extensions:    {type: :array, of: :string, default: []},
      users:         {type: :array, of: :hash, template: USER_DEFAULTS, default: DEFAULT_USERS}
    }

    def sanitize_env_vars(payload)
      vars = payload[:environment_variables]

      # now lets enable any backwards compatible vars
      vars.inject({}) do |res, (key, val)|
        if /^DATABASE(\d+)_(.+)$/.match key
          # create the backwards compatible version
          res["DB#{$1}_#{$2}"] = val
        end
        # put the original back in
        res[key] = val
        res
      end
    end

    def check_for_ready(port=5432)
      begin
        Timeout::timeout(90) do
          execute 'try connect' do
            command <<-EOF
              bash -c 'until /data/bin/psql -U gonano -p #{port} postgres -c "SELECT * FROM pg_catalog.pg_tables;"
              do
                sleep 1
              done'
            EOF
          end
        end
      rescue Timeout::Error
          puts 'failed to connect to PostgreSQL'
          exit 1
      end
    end

  end
end<commit_msg>rewrite the check for ready loop in ruby<commit_after>
module Hooky
  module Postgresql

    DEFAULT_PRIVILEGES = [
      {
        :privilege => "ALL PRIVILEGES",
        :type => "DATABASE",
        :column => nil,
        :on => "gonano",
        :with_grant => true
      }
    ]

    DEFAULT_META = {
      :privileges => DEFAULT_PRIVILEGES,
      :roles => [
        "SUPERUSER"
      ]
    }

    DEFAULT_USERS = [
      {
        :username => "nanobox",
        :meta => DEFAULT_META
      }
    ]

    USER_META_PRVILIGES_DEFAULTS = {
      privilege:    {type: :string, from: ["SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER", "ALL", "ALL PRIVILEGES", "USAGE", "CREATE", "CONNECT", "TEMPORARY", "TEMP", "EXECUTE"], default: "SELECT"},
      type:         {type: :string, from: ["TABLE", "ALL TABLES IN SCHEMA", "SEQUENCE", "ALL SEQUENCES IN SCHEMA", "DATABASE", "FOREIGN DATA WRAPPER", "FOREIGN SERVER", "FUNCTION", "ALL FUNCTIONS IN SCHEMA", "LANGUAGE", "LARGE OBJECT", "SCHEMA", "TABLESPACE"], default: "DATABASE"},
      column:       {type: :string, default: nil},
      on:           {type: :string, default: 'gonano'},
      with_grant:   {type: :on_off, default: false}
    }

    USER_META_DEFAULTS = {
      privileges:    {type: :array, of: :hash, template: USER_META_PRVILIGES_DEFAULTS, default: DEFAULT_PRIVILEGES},
      roles:         {type: :array, of: :string, default: []}
    }

    USER_DEFAULTS = {
      username:      {type: :string, default: 'gonano'},
      meta:          {type: :hash, template: USER_META_DEFAULTS, default: DEFAULT_META}
    }

    CONFIG_DEFAULTS = {
      # global settings
      before_deploy: {type: :array, of: :string, default: []},
      after_deploy:  {type: :array, of: :string, default: []},
      hook_ref:      {type: :string, default: "stable"},
      # postgresql settings
      locale:        {type: :string, default: 'en_US.UTF-8'},
      extensions:    {type: :array, of: :string, default: []},
      users:         {type: :array, of: :hash, template: USER_DEFAULTS, default: DEFAULT_USERS}
    }

    def sanitize_env_vars(payload)
      vars = payload[:environment_variables]

      # now lets enable any backwards compatible vars
      vars.inject({}) do |res, (key, val)|
        if /^DATABASE(\d+)_(.+)$/.match key
          # create the backwards compatible version
          res["DB#{$1}_#{$2}"] = val
        end
        # put the original back in
        res[key] = val
        res
      end
    end

    def check_for_ready(port=5432)
      begin
        Timeout::timeout(90) do
          begin
            execute 'try connect' do
              command "/data/bin/psql -U gonano -p #{port} postgres -c \"SELECT * FROM pg_catalog.pg_tables;\""
            end
          rescue Hookit::Error::UnexpectedExit
            sleep 1
            retry
          end
        end
      rescue Timeout::Error
        puts 'failed to connect to PostgreSQL'
        exit 1
      end
    end

  end
end
{'fraction_non_alphanumeric': 0.137653181131442, 'fraction_numerical': 0.0036931341279167365, 'mean_word_length': 2.2240259740259742, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '2059717', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1954, 'n_words': 504}