Class: ActiveRecord::ConnectionAdapters::RedshiftAdapter
- Inherits:
 - 
      AbstractAdapter
      
        
- Object
 - AbstractAdapter
 - ActiveRecord::ConnectionAdapters::RedshiftAdapter
 
 
- Includes:
 - ActiveRecord::ConnectionAdapters::Redshift::DatabaseStatements, ActiveRecord::ConnectionAdapters::Redshift::Quoting, ActiveRecord::ConnectionAdapters::Redshift::ReferentialIntegrity, ActiveRecord::ConnectionAdapters::Redshift::SchemaStatements
 
- Defined in:
 - lib/active_record/connection_adapters/redshift_adapter.rb
 
Overview
The PostgreSQL adapter works with the native C (bitbucket.org/ged/ruby-pg) driver.
Options:
- 
:host- Defaults to a Unix-domain socket in /tmp. On machines without Unix-domain sockets, the default is to connect to localhost. - 
:port- Defaults to 5432. - 
:username- Defaults to be the same as the operating system name of the user running the application. - 
:password- Password to be used if the server demands password authentication. - 
:database- Defaults to be the same as the user name. - 
:schema_search_path- An optional schema search path for the connection given as a string of comma-separated schema names. This is backward-compatible with the:schema_orderoption. - 
:encoding- An optional client encoding that is used in aSET client_encoding TO <encoding>call on the connection. - 
:min_messages- An optional client min messages that is used in aSET client_min_messages TO <min_messages>call on the connection. - 
:variables- An optional hash of additional parameters that will be used inSET SESSION key = valcalls on the connection. - 
:insert_returning- Does nothing for Redshift. 
Any further options are used as connection parameters to libpq. See www.postgresql.org/docs/9.1/static/libpq-connect.html for the list of parameters.
In addition, default connection parameters of libpq can be set per environment variables. See www.postgresql.org/docs/9.1/static/libpq-envars.html .
Defined Under Namespace
Classes: StatementPool
Constant Summary collapse
- ADAPTER_NAME =
 'Redshift'.freeze
- NATIVE_DATABASE_TYPES =
 { primary_key: "integer identity primary key", string: { name: "varchar" }, text: { name: "varchar" }, integer: { name: "integer" }, float: { name: "decimal" }, decimal: { name: "decimal" }, datetime: { name: "timestamp" }, time: { name: "timestamp" }, date: { name: "date" }, bigint: { name: "bigint" }, boolean: { name: "boolean" }, }
- OID =
          
:nodoc:
 Redshift::OID
- OPERATION_ALIASES =
          
:nodoc:
 { # :nodoc: "maximum" => "max", "minimum" => "min", "average" => "avg", }
Constants included from ActiveRecord::ConnectionAdapters::Redshift::DatabaseStatements
ActiveRecord::ConnectionAdapters::Redshift::DatabaseStatements::BYTEA_COLUMN_TYPE_OID, ActiveRecord::ConnectionAdapters::Redshift::DatabaseStatements::MONEY_COLUMN_TYPE_OID
Instance Method Summary collapse
- 
  
    
      #active?  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    
Is this connection alive and ready for queries?.
 - 
  
    
      #clear_cache!  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Clears the prepared statements cache.
 - 
  
    
      #column_name_for_operation(operation, node)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
:nodoc:.
 - #disable_extension(name) ⇒ Object
 - 
  
    
      #disconnect!  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Disconnects from the database if already connected.
 - #enable_extension(name) ⇒ Object
 - #extension_enabled?(name) ⇒ Boolean
 - #index_algorithms ⇒ Object
 - 
  
    
      #initialize(connection, logger, connection_parameters, config)  ⇒ RedshiftAdapter 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
Initializes and connects a PostgreSQL adapter.
 - 
  
    
      #lookup_cast_type(sql_type)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
:nodoc:.
 - 
  
    
      #native_database_types  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
:nodoc:.
 - 
  
    
      #reconnect!  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Close then reopen the connection.
 - #reset! ⇒ Object
 - 
  
    
      #schema_creation  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
:nodoc:.
 - 
  
    
      #session_auth=(user)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Set the authorized user for this session.
 - #supports_ddl_transactions? ⇒ Boolean
 - #supports_explain? ⇒ Boolean
 - #supports_extensions? ⇒ Boolean
 - #supports_foreign_keys? ⇒ Boolean
 - #supports_import? ⇒ Boolean
 - #supports_index_sort_order? ⇒ Boolean
 - #supports_materialized_views? ⇒ Boolean
 - 
  
    
      #supports_migrations?  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    
Returns true, since this connection adapter supports migrations.
 - #supports_partial_index? ⇒ Boolean
 - 
  
    
      #supports_primary_key?  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    
Does PostgreSQL support finding primary key on non-Active Record tables?.
 - #supports_ranges? ⇒ Boolean
 - #supports_transaction_isolation? ⇒ Boolean
 - #supports_views? ⇒ Boolean
 - 
  
    
      #table_alias_length  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Returns the configured supported identifier length supported by PostgreSQL.
 - #truncate(table_name, name = nil) ⇒ Object
 - 
  
    
      #update_table_definition(table_name, base)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
:nodoc:.
 - #use_insert_returning? ⇒ Boolean
 - #valid_type?(type) ⇒ Boolean
 
Methods included from ActiveRecord::ConnectionAdapters::Redshift::DatabaseStatements
#begin_db_transaction, #begin_isolated_db_transaction, #commit_db_transaction, #exec_delete, #exec_insert, #exec_query, #exec_rollback_db_transaction, #execute, #explain, #query, #result_as_array, #select_rows, #select_value, #select_values, #sql_for_insert
Methods included from ActiveRecord::ConnectionAdapters::Redshift::SchemaStatements
#add_column, #add_index, #change_column, #change_column_default, #change_column_null, #collation, #columns, #columns_for_distinct, #create_database, #create_schema, #ctype, #current_database, #current_schema, #data_source_exists?, #data_sources, #default_sequence_name, #drop_database, #drop_schema, #drop_table, #encoding, #extract_foreign_key_action, #fetch_type_metadata, #foreign_keys, #index_name_exists?, #index_name_length, #indexes, #new_column, #pk_and_sequence_for, #primary_keys, #recreate_database, #remove_index!, #rename_column, #rename_index, #rename_table, #reset_pk_sequence!, #schema_exists?, #schema_names, #schema_search_path, #schema_search_path=, #serial_sequence, #set_pk_sequence!, #table_exists?, #tables, #type_to_sql, #view_exists?, #views
Methods included from ActiveRecord::ConnectionAdapters::Redshift::ReferentialIntegrity
#disable_referential_integrity, #supports_disable_referential_integrity?
Methods included from ActiveRecord::ConnectionAdapters::Redshift::Quoting
#escape_bytea, #quote_column_name, #quote_default_value, #quote_schema_name, #quote_string, #quote_table_name, #quote_table_name_for_assignment, #quoted_date, #unescape_bytea
Constructor Details
#initialize(connection, logger, connection_parameters, config) ⇒ RedshiftAdapter
Initializes and connects a PostgreSQL adapter.
      158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 158 def initialize(connection, logger, connection_parameters, config) super(connection, logger, config) @visitor = Arel::Visitors::PostgreSQL.new self @visitor.extend(ConnectionAdapters::DetermineIfPreparableVisitor) if defined?(ConnectionAdapters::DetermineIfPreparableVisitor) @prepared_statements = false @connection_parameters = connection_parameters # @local_tz is initialized as nil to avoid warnings when connect tries to use it @local_tz = nil @table_alias_length = nil connect @statements = StatementPool.new @connection, self.class.type_cast_config_to_integer(config[:statement_limit]) @type_map = Type::HashLookupTypeMap.new initialize_type_map(type_map) @local_tz = execute('SHOW TIME ZONE', 'SCHEMA').first["TimeZone"] @use_insert_returning = @config.key?(:insert_returning) ? self.class.type_cast_config_to_boolean(@config[:insert_returning]) : false end  | 
  
Instance Method Details
#active? ⇒ Boolean
Is this connection alive and ready for queries?
      191 192 193 194 195 196  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 191 def active? @connection.query 'SELECT 1' true rescue PG::Error false end  | 
  
#clear_cache! ⇒ Object
Clears the prepared statements cache.
      182 183 184  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 182 def clear_cache! @statements.clear end  | 
  
#column_name_for_operation(operation, node) ⇒ Object
:nodoc:
      298 299 300  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 298 def column_name_for_operation(operation, node) # :nodoc: OPERATION_ALIASES.fetch(operation) { operation.downcase } end  | 
  
#disable_extension(name) ⇒ Object
      263 264  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 263 def disable_extension(name) end  | 
  
#disconnect! ⇒ Object
Disconnects from the database if already connected. Otherwise, this method does nothing.
      217 218 219 220  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 217 def disconnect! super @connection.close rescue nil end  | 
  
#enable_extension(name) ⇒ Object
      260 261  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 260 def enable_extension(name) end  | 
  
#extension_enabled?(name) ⇒ Boolean
      266 267 268  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 266 def extension_enabled?(name) false end  | 
  
#index_algorithms ⇒ Object
      125 126 127  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 125 def index_algorithms { concurrently: 'CONCURRENTLY' } end  | 
  
#lookup_cast_type(sql_type) ⇒ Object
:nodoc:
      293 294 295 296  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 293 def lookup_cast_type(sql_type) # :nodoc: oid = execute("SELECT #{quote(sql_type)}::regtype::oid", "SCHEMA").first['oid'].to_i super(oid) end  | 
  
#native_database_types ⇒ Object
:nodoc:
      222 223 224  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 222 def native_database_types #:nodoc: NATIVE_DATABASE_TYPES end  | 
  
#reconnect! ⇒ Object
Close then reopen the connection.
      199 200 201 202 203  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 199 def reconnect! super @connection.reset configure_connection end  | 
  
#reset! ⇒ Object
      205 206 207 208 209 210 211 212 213  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 205 def reset! clear_cache! reset_transaction unless @connection.transaction_status == ::PG::PQTRANS_IDLE @connection.query 'ROLLBACK' end @connection.query 'DISCARD ALL' configure_connection end  | 
  
#schema_creation ⇒ Object
:nodoc:
      101 102 103  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 101 def schema_creation # :nodoc: Redshift::SchemaCreation.new self end  | 
  
#session_auth=(user) ⇒ Object
Set the authorized user for this session
      276 277 278 279  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 276 def session_auth=(user) clear_cache! exec_query "SET SESSION AUTHORIZATION #{user}" end  | 
  
#supports_ddl_transactions? ⇒ Boolean
      236 237 238  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 236 def supports_ddl_transactions? true end  | 
  
#supports_explain? ⇒ Boolean
      240 241 242  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 240 def supports_explain? true end  | 
  
#supports_extensions? ⇒ Boolean
      244 245 246  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 244 def supports_extensions? false end  | 
  
#supports_foreign_keys? ⇒ Boolean
      117 118 119  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 117 def supports_foreign_keys? true end  | 
  
#supports_import? ⇒ Boolean
      256 257 258  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 256 def supports_import? true end  | 
  
#supports_index_sort_order? ⇒ Boolean
      105 106 107  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 105 def supports_index_sort_order? false end  | 
  
#supports_materialized_views? ⇒ Boolean
      252 253 254  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 252 def supports_materialized_views? false end  | 
  
#supports_migrations? ⇒ Boolean
Returns true, since this connection adapter supports migrations.
      227 228 229  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 227 def supports_migrations? true end  | 
  
#supports_partial_index? ⇒ Boolean
      109 110 111  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 109 def supports_partial_index? false end  | 
  
#supports_primary_key? ⇒ Boolean
Does PostgreSQL support finding primary key on non-Active Record tables?
      232 233 234  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 232 def supports_primary_key? #:nodoc: true end  | 
  
#supports_ranges? ⇒ Boolean
      248 249 250  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 248 def supports_ranges? false end  | 
  
#supports_transaction_isolation? ⇒ Boolean
      113 114 115  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 113 def supports_transaction_isolation? false end  | 
  
#supports_views? ⇒ Boolean
      121 122 123  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 121 def supports_views? true end  | 
  
#table_alias_length ⇒ Object
Returns the configured supported identifier length supported by PostgreSQL
      271 272 273  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 271 def table_alias_length @table_alias_length ||= query('SHOW max_identifier_length', 'SCHEMA')[0][0].to_i end  | 
  
#truncate(table_name, name = nil) ⇒ Object
      186 187 188  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 186 def truncate(table_name, name = nil) exec_query "TRUNCATE TABLE #{quote_table_name(table_name)}", name, [] end  | 
  
#update_table_definition(table_name, base) ⇒ Object
:nodoc:
      289 290 291  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 289 def update_table_definition(table_name, base) #:nodoc: Redshift::Table.new(table_name, base) end  | 
  
#use_insert_returning? ⇒ Boolean
      281 282 283  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 281 def use_insert_returning? false end  | 
  
#valid_type?(type) ⇒ Boolean
      285 286 287  | 
    
      # File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 285 def valid_type?(type) !native_database_types[type].nil? end  |