Class: ActiveRecord::ConnectionAdapters::RedshiftAdapter

Inherits:
PostgreSQLAdapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/redshift_adapter.rb

Constant Summary collapse

ADAPTER_NAME =
'Redshift'.freeze
NATIVE_DATABASE_TYPES =
PostgreSQLAdapter::NATIVE_DATABASE_TYPES.merge({
  primary_key: "bigint primary key identity(1,1)",
})

Instance Method Summary collapse

Constructor Details

#initialize(connection, logger, connection_parameters, config) ⇒ RedshiftAdapter

Returns a new instance of RedshiftAdapter.



37
38
39
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 37

def initialize(connection, logger, connection_parameters, config)
  super
end

Instance Method Details

#column_definitions(table_name) ⇒ Object

Returns the list of a table’s column names, data types, and default values.

The underlying query is roughly:

SELECT column.name, column.type, default.value
  FROM column LEFT JOIN default
    ON column.table_id = default.table_id
   AND column.num = default.column_num
 WHERE column.table_id = get_table_id('table_name')
   AND column.num > 0
   AND NOT column.is_dropped
 ORDER BY column.num

If the table name is not prefixed with a schema, the database will take the first match from the schema search path.

Query implementation notes:

- format_type includes the column size constraint, e.g. varchar(50)
- ::regclass is a function that gives the id for a table name


142
143
144
145
146
147
148
149
150
151
152
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 142

def column_definitions(table_name) #:nodoc:
  exec_query(<<-end_sql, 'SCHEMA').rows
    SELECT a.attname, format_type(a.atttypid, a.atttypmod),
             pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
      FROM pg_attribute a LEFT JOIN pg_attrdef d
        ON a.attrelid = d.adrelid AND a.attnum = d.adnum
     WHERE a.attrelid = '#{quote_table_name(table_name)}'::regclass
       AND a.attnum > 0 AND NOT a.attisdropped
     ORDER BY a.attnum
  end_sql
end

#configure_connectionObject

Configures the encoding, verbosity, schema search path, and time zone of the connection. This is called by #connect and should not be called manually.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 43

def configure_connection
  if @config[:encoding]
    @connection.set_client_encoding(@config[:encoding])
  end
  # self.client_min_messages = @config[:min_messages] || 'warning'
  self.schema_search_path = @config[:schema_search_path] || @config[:schema_order]

  # Use standard-conforming strings so we don't have to do the E'...' dance.
  # set_standard_conforming_strings

  # If using Active Record's time zone support configure the connection to return
  # TIMESTAMP WITH ZONE types in UTC.
  # (SET TIME ZONE does not use an equals sign like other SET variables)
  # if ActiveRecord::Base.default_timezone == :utc
  #   execute("SET time zone 'UTC'", 'SCHEMA')
  # elsif @local_tz
  #   execute("SET time zone '#{@local_tz}'", 'SCHEMA')
  # end

  # SET statements from :variables config hash
  # http://www.postgresql.org/docs/8.3/static/sql-set.html
  variables = @config[:variables] || {}
  variables.map do |k, v|
    if v == ':default' || v == :default
      # Sets the value to the global or compile default
      execute("SET SESSION #{k} TO DEFAULT", 'SCHEMA')
    elsif !v.nil?
      execute("SET SESSION #{k} TO #{quote(v)}", 'SCHEMA')
    end
  end
end

#create_table_definition(*args) ⇒ Object

Schema Statements



180
181
182
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 180

def create_table_definition(*args) # :nodoc:
  Redshift::TableDefinition.new(*args)
end

#get_advisory_lock(lock_id) ⇒ Object



172
173
174
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 172

def get_advisory_lock(lock_id)
  lock_id
end

#indexes(table_name, name = nil) ⇒ Object



168
169
170
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 168

def indexes(table_name, name = nil)
  []
end

#native_database_typesObject

:nodoc:



80
81
82
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 80

def native_database_types #:nodoc:
  NATIVE_DATABASE_TYPES
end

#postgresql_versionObject



75
76
77
78
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 75

def postgresql_version
  # Hack to avoid native PostgreQLAdapter's version check
  return 100000 + 80002
end

#primary_keys(table) ⇒ Object

Returns just a table’s primary key



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 155

def primary_keys(table)
  row = exec_query(<<-end_sql, 'SCHEMA').rows.map do |row|
    SELECT DISTINCT(attr.attname)
    FROM pg_attribute attr
    INNER JOIN pg_depend dep ON attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid
    INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
    WHERE cons.contype = 'p'
      AND dep.refobjid = '#{quote_table_name(table)}'::regclass
  end_sql
    row && row.first
  end
end

#release_advisory_lock(lock_id) ⇒ Object



176
177
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 176

def release_advisory_lock(lock_id)
end

#schema_creationObject

:nodoc:



188
189
190
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 188

def schema_creation # :nodoc:
  Redshift::SchemaCreation.new self
end

#supports_extensions?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 108

def supports_extensions?
  false
end

#supports_foreign_keys?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 100

def supports_foreign_keys?
  false
end

#supports_index_sort_order?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 88

def supports_index_sort_order?
  false
end

#supports_materialized_views?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 116

def supports_materialized_views?
  false
end

#supports_partial_index?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 92

def supports_partial_index?
  false
end

#supports_ranges?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 112

def supports_ranges?
  false
end

#supports_statement_cache?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 84

def supports_statement_cache?
  false
end

#supports_transaction_isolation?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 96

def supports_transaction_isolation?
  false
end

#supports_views?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 104

def supports_views?
  false
end

#update_table_definition(table_name, base) ⇒ Object

:nodoc:



184
185
186
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 184

def update_table_definition(table_name, base) #:nodoc:
  Redshift::Table.new(table_name, base)
end

#use_insert_returning?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/active_record/connection_adapters/redshift_adapter.rb', line 120

def use_insert_returning?
  false
end