module Switchman::ActiveRecord::PostgreSQLAdapter
Public Instance Methods
add_index_options(_table_name, _column_name, **)
click to toggle source
Calls superclass method
# File lib/switchman/active_record/postgresql_adapter.rb, line 102 def add_index_options(_table_name, _column_name, **) index, algorithm, if_not_exists = super algorithm = nil if DatabaseServer.creating_new_shard && algorithm == 'CONCURRENTLY' [index, algorithm, if_not_exists] end
columns(*)
click to toggle source
Calls superclass method
# File lib/switchman/active_record/postgresql_adapter.rb, line 130 def columns(*) with_local_table_name(false) { super } end
create_database(name, options = {})
click to toggle source
copy/paste; use quote_local_table_name
# File lib/switchman/active_record/postgresql_adapter.rb, line 7 def create_database(name, options = {}) options = { encoding: 'utf8' }.merge!(options.symbolize_keys) option_string = options.sum do |key, value| case key when :owner " OWNER = \"#{value}\"" when :template " TEMPLATE = \"#{value}\"" when :encoding " ENCODING = '#{value}'" when :collation " LC_COLLATE = '#{value}'" when :ctype " LC_CTYPE = '#{value}'" when :tablespace " TABLESPACE = \"#{value}\"" when :connection_limit " CONNECTION LIMIT = #{value}" else '' end end execute "CREATE DATABASE #{quote_local_table_name(name)}#{option_string}" end
current_schemas()
click to toggle source
# File lib/switchman/active_record/postgresql_adapter.rb, line 39 def current_schemas select_values('SELECT * FROM unnest(current_schemas(false))') end
extract_schema_qualified_name(string)
click to toggle source
# File lib/switchman/active_record/postgresql_adapter.rb, line 43 def extract_schema_qualified_name(string) name = ::ActiveRecord::ConnectionAdapters::PostgreSQL::Utils.extract_schema_qualified_name(string.to_s) name.instance_variable_set(:@schema, shard.name) if string && !name.schema [name.schema, name.identifier] end
foreign_keys(table_name)
click to toggle source
Calls superclass method
# File lib/switchman/active_record/postgresql_adapter.rb, line 68 def foreign_keys(table_name) super.each do |fk| to_table_qualified_name = ::ActiveRecord::ConnectionAdapters::PostgreSQL::Utils.extract_schema_qualified_name(fk.to_table) fk.to_table = to_table_qualified_name.identifier if to_table_qualified_name.schema == shard.name end end
quote_local_table_name(name)
click to toggle source
# File lib/switchman/active_record/postgresql_adapter.rb, line 75 def quote_local_table_name(name) # postgres quotes tables and columns the same; just pass through # (differs from quote_table_name_with_shard below by no logic to # explicitly qualify the table) quote_column_name(name) end
quote_table_name(name)
click to toggle source
# File lib/switchman/active_record/postgresql_adapter.rb, line 82 def quote_table_name(name) return quote_local_table_name(name) if @use_local_table_name name = ::ActiveRecord::ConnectionAdapters::PostgreSQL::Utils.extract_schema_qualified_name(name.to_s) name.instance_variable_set(:@schema, shard.name) unless name.schema name.quoted end
quoted_scope(name = nil, type: nil)
click to toggle source
significant change: use the shard name if no explicit schema
# File lib/switchman/active_record/postgresql_adapter.rb, line 50 def quoted_scope(name = nil, type: nil) schema, name = extract_schema_qualified_name(name) type = \ case type # rubocop:disable Style/HashLikeCase when 'BASE TABLE' "'r','p'" when 'VIEW' "'v','m'" when 'FOREIGN TABLE' "'f'" end scope = {} scope[:schema] = quote(schema || shard.name) scope[:name] = quote(name) if name scope[:type] = type if type scope end
rename_index(table_name, old_name, new_name)
click to toggle source
# File lib/switchman/active_record/postgresql_adapter.rb, line 124 def rename_index(table_name, old_name, new_name) validate_index_length!(table_name, new_name) execute "ALTER INDEX #{quote_table_name(old_name)} RENAME TO #{quote_local_table_name(new_name)}" end
rename_table(table_name, new_name)
click to toggle source
# File lib/switchman/active_record/postgresql_adapter.rb, line 108 def rename_table(table_name, new_name) clear_cache! execute "ALTER TABLE #{quote_table_name(table_name)} RENAME TO #{quote_local_table_name(new_name)}" pk, seq = pk_and_sequence_for(new_name) if pk idx = "#{table_name}_pkey" new_idx = "#{new_name}_pkey" execute "ALTER INDEX #{quote_table_name(idx)} RENAME TO #{quote_local_table_name(new_idx)}" if seq && seq.identifier == "#{table_name}_#{pk}_seq" new_seq = "#{new_name}_#{pk}_seq" execute "ALTER TABLE #{seq.quoted} RENAME TO #{quote_local_table_name(new_seq)}" end end rename_table_indexes(table_name, new_name) end
with_global_table_name(&block)
click to toggle source
# File lib/switchman/active_record/postgresql_adapter.rb, line 90 def with_global_table_name(&block) with_local_table_name(false, &block) end
with_local_table_name(enable = true) { || ... }
click to toggle source
# File lib/switchman/active_record/postgresql_adapter.rb, line 94 def with_local_table_name(enable = true) # rubocop:disable Style/OptionalBooleanParameter old_value = @use_local_table_name @use_local_table_name = enable yield ensure @use_local_table_name = old_value end