Class: ActiveRecord::ConnectionAdapters::RedshiftAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::RedshiftAdapter
- Includes:
- DatabaseStatements, Quoting, ReferentialIntegrity, SchemaStatements
- Defined in:
- lib/active_record/connection_adapters/redshift_adapter.rb,
lib/active_record/connection_adapters/redshift/oid.rb,
lib/active_record/connection_adapters/redshift/quoting.rb,
lib/active_record/connection_adapters/redshift/schema_statements.rb,
lib/active_record/connection_adapters/redshift/database_statements.rb,
lib/active_record/connection_adapters/redshift/referential_integrity.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_order
option. -
: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 = val
calls 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
Modules: ColumnMethods, DatabaseStatements, OID, Quoting, ReferentialIntegrity, SchemaStatements, Utils Classes: BindSubstitution, ColumnDefinition, SchemaCreation, StatementPool, Table, TableDefinition
Constant Summary collapse
- ADAPTER_NAME =
'Redshift'
- NATIVE_DATABASE_TYPES =
{ primary_key: "serial primary key", string: { name: "character varying", limit: 255 }, text: { name: "text" }, integer: { name: "integer" }, float: { name: "float" }, decimal: { name: "decimal" }, datetime: { name: "timestamp" }, timestamp: { name: "timestamp" }, time: { name: "time" }, date: { name: "date" }, daterange: { name: "daterange" }, numrange: { name: "numrange" }, tsrange: { name: "tsrange" }, tstzrange: { name: "tstzrange" }, int4range: { name: "int4range" }, int8range: { name: "int8range" }, binary: { name: "bytea" }, boolean: { name: "boolean" }, xml: { name: "xml" }, tsvector: { name: "tsvector" }, hstore: { name: "hstore" }, inet: { name: "inet" }, cidr: { name: "cidr" }, macaddr: { name: "macaddr" }, uuid: { name: "uuid" }, json: { name: "json" }, ltree: { name: "ltree" } }
Instance Method Summary collapse
-
#active? ⇒ Boolean
Is this connection alive and ready for queries?.
-
#adapter_name ⇒ Object
Returns ‘PostgreSQL’ as adapter name for identification purposes.
-
#clear_cache! ⇒ Object
Clears the prepared statements cache.
- #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.
-
#migration_keys ⇒ Object
Adds ‘:array` as a valid migration key.
-
#native_database_types ⇒ Object
:nodoc:.
-
#prepare_column_options(column, types) ⇒ Object
Adds ‘:array` option to the default set provided by the AbstractAdapter.
-
#reconnect! ⇒ Object
Close then reopen the connection.
- #reset! ⇒ Object
- #schema_creation ⇒ Object
-
#session_auth=(user) ⇒ Object
Set the authorized user for this session.
- #supports_ddl_transactions? ⇒ Boolean
-
#supports_explain? ⇒ Boolean
Returns true.
-
#supports_extensions? ⇒ Boolean
Returns true if pg > 9.2.
- #supports_import? ⇒ Boolean
- #supports_index_sort_order? ⇒ Boolean
- #supports_insert_with_returning? ⇒ 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
Range datatypes weren’t introduced until PostgreSQL 9.2.
-
#supports_savepoints? ⇒ Boolean
Returns true, since this connection adapter supports savepoints.
-
#supports_statement_cache? ⇒ Boolean
Returns
true
, since this connection adapter supports prepared statement caching. - #supports_transaction_isolation? ⇒ Boolean
-
#table_alias_length ⇒ Object
Returns the configured supported identifier length supported by PostgreSQL.
- #use_insert_returning? ⇒ Boolean
- #valid_type?(type) ⇒ Boolean
Methods included from DatabaseStatements
#begin_db_transaction, #begin_isolated_db_transaction, #commit_db_transaction, #create, #create_savepoint, #exec_delete, #exec_insert, #exec_query, #execute, #explain, #insert_sql, #outside_transaction?, #query, #release_savepoint, #result_as_array, #rollback_db_transaction, #rollback_to_savepoint, #select_rows, #sql_for_insert, #substitute_at, #update_sql
Methods included from SchemaStatements
#add_column, #add_index, #change_column, #change_column_default, #change_column_null, #collation, #columns, #create_database, #create_schema, #ctype, #current_database, #current_schema, #default_sequence_name, #distinct, #drop_database, #drop_schema, #encoding, #index_name_length, #indexes, #pk_and_sequence_for, #primary_key, #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, #table_exists?, #tables, #type_to_sql
Methods included from ReferentialIntegrity
#disable_referential_integrity, #supports_disable_referential_integrity?
Methods included from Quoting
#escape_bytea, #quote, #quote_column_name, #quote_string, #quote_table_name, #quote_table_name_for_assignment, #quoted_date, #type_cast, #unescape_bytea
Constructor Details
#initialize(connection, logger, connection_parameters, config) ⇒ RedshiftAdapter
Initializes and connects a PostgreSQL adapter.
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 526 def initialize(connection, logger, connection_parameters, config) super(connection, logger) if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true }) @visitor = Arel::Visitors::PostgreSQL.new self else @visitor = unprepared_visitor end connection_parameters.delete :prepared_statements @connection_parameters, @config = connection_parameters, config # @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.fetch(:statement_limit) { 1000 }) initialize_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?
557 558 559 560 561 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 557 def active? @connection.connect_poll != PG::PGRES_POLLING_FAILED rescue PGError false end |
#adapter_name ⇒ Object
Returns ‘PostgreSQL’ as adapter name for identification purposes.
429 430 431 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 429 def adapter_name ADAPTER_NAME end |
#clear_cache! ⇒ Object
Clears the prepared statements cache.
552 553 554 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 552 def clear_cache! @statements.clear end |
#disable_extension(name) ⇒ Object
631 632 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 631 def disable_extension(name) end |
#disconnect! ⇒ Object
Disconnects from the database if already connected. Otherwise, this method does nothing.
577 578 579 580 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 577 def disconnect! super @connection.close rescue nil end |
#enable_extension(name) ⇒ Object
628 629 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 628 def enable_extension(name) end |
#extension_enabled?(name) ⇒ Boolean
634 635 636 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 634 def extension_enabled?(name) false end |
#index_algorithms ⇒ Object
464 465 466 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 464 def index_algorithms { concurrently: 'CONCURRENTLY' } end |
#migration_keys ⇒ Object
Adds ‘:array` as a valid migration key
442 443 444 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 442 def migration_keys super + [:array] end |
#native_database_types ⇒ Object
:nodoc:
582 583 584 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 582 def native_database_types #:nodoc: NATIVE_DATABASE_TYPES end |
#prepare_column_options(column, types) ⇒ Object
Adds ‘:array` option to the default set provided by the AbstractAdapter
435 436 437 438 439 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 435 def (column, types) spec = super spec[:array] = 'true' if column.respond_to?(:array) && column.array spec end |
#reconnect! ⇒ Object
Close then reopen the connection.
564 565 566 567 568 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 564 def reconnect! super @connection.reset configure_connection end |
#reset! ⇒ Object
570 571 572 573 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 570 def reset! clear_cache! super end |
#schema_creation ⇒ Object
36 37 38 |
# File 'lib/active_record/connection_adapters/redshift/schema_statements.rb', line 36 def schema_creation SchemaCreation.new self end |
#session_auth=(user) ⇒ Object
Set the authorized user for this session
647 648 649 650 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 647 def session_auth=(user) clear_cache! exec_query "SET SESSION AUTHORIZATION #{user}" end |
#supports_ddl_transactions? ⇒ Boolean
600 601 602 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 600 def supports_ddl_transactions? true end |
#supports_explain? ⇒ Boolean
Returns true.
614 615 616 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 614 def supports_explain? true end |
#supports_extensions? ⇒ Boolean
Returns true if pg > 9.2
619 620 621 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 619 def supports_extensions? false end |
#supports_import? ⇒ Boolean
609 610 611 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 609 def supports_import? true end |
#supports_index_sort_order? ⇒ Boolean
452 453 454 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 452 def supports_index_sort_order? true end |
#supports_insert_with_returning? ⇒ Boolean
596 597 598 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 596 def supports_insert_with_returning? false end |
#supports_migrations? ⇒ Boolean
Returns true, since this connection adapter supports migrations.
587 588 589 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 587 def supports_migrations? true end |
#supports_partial_index? ⇒ Boolean
456 457 458 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 456 def supports_partial_index? true end |
#supports_primary_key? ⇒ Boolean
Does PostgreSQL support finding primary key on non-Active Record tables?
592 593 594 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 592 def supports_primary_key? #:nodoc: true end |
#supports_ranges? ⇒ Boolean
Range datatypes weren’t introduced until PostgreSQL 9.2
624 625 626 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 624 def supports_ranges? false end |
#supports_savepoints? ⇒ Boolean
Returns true, since this connection adapter supports savepoints.
605 606 607 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 605 def supports_savepoints? false end |
#supports_statement_cache? ⇒ Boolean
Returns true
, since this connection adapter supports prepared statement caching.
448 449 450 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 448 def supports_statement_cache? true end |
#supports_transaction_isolation? ⇒ Boolean
460 461 462 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 460 def supports_transaction_isolation? true end |
#table_alias_length ⇒ Object
Returns the configured supported identifier length supported by PostgreSQL
642 643 644 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 642 def table_alias_length @table_alias_length ||= query('SHOW max_identifier_length', 'SCHEMA')[0][0].to_i end |
#use_insert_returning? ⇒ Boolean
671 672 673 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 671 def use_insert_returning? false end |
#valid_type?(type) ⇒ Boolean
675 676 677 |
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 675 def valid_type?(type) !native_database_types[type].nil? end |