Module: Switchman::ActiveRecord::PostgreSQLAdapter
- Defined in:
- lib/switchman/active_record/postgresql_adapter.rb
Class Method Summary collapse
Instance Method Summary collapse
- #current_schemas ⇒ Object
- #index_name_exists?(table_name, index_name, default) ⇒ Boolean
- #indexes(table_name) ⇒ Object
- #quote_local_table_name(name) ⇒ Object
- #quote_table_name(name) ⇒ Object
- #table_exists?(name) ⇒ Boolean
- #tables(name = nil) ⇒ Object
- #use_qualified_names? ⇒ Boolean
Class Method Details
.prepended(klass) ⇒ Object
4 5 6 |
# File 'lib/switchman/active_record/postgresql_adapter.rb', line 4 def self.prepended(klass) klass::NATIVE_DATABASE_TYPES[:primary_key] = "bigserial primary key".freeze end |
Instance Method Details
#current_schemas ⇒ Object
8 9 10 |
# File 'lib/switchman/active_record/postgresql_adapter.rb', line 8 def current_schemas select_values("SELECT * FROM unnest(current_schemas(false))") end |
#index_name_exists?(table_name, index_name, default) ⇒ Boolean
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/switchman/active_record/postgresql_adapter.rb', line 105 def index_name_exists?(table_name, index_name, default) schema = shard.name if use_qualified_names? exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0 SELECT COUNT(*) FROM pg_class t INNER JOIN pg_index d ON t.oid = d.indrelid INNER JOIN pg_class i ON d.indexrelid = i.oid WHERE i.relkind = 'i' AND i.relname = '#{index_name}' AND t.relname = '#{table_name}' AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = #{schema ? "'#{schema}'" : 'ANY (current_schemas(false))'} ) SQL end |
#indexes(table_name) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/switchman/active_record/postgresql_adapter.rb', line 61 def indexes(table_name) schema = shard.name if use_qualified_names? result = query(<<-SQL, 'SCHEMA') SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid FROM pg_class t INNER JOIN pg_index d ON t.oid = d.indrelid INNER JOIN pg_class i ON d.indexrelid = i.oid WHERE i.relkind = 'i' AND d.indisprimary = 'f' AND t.relname = '#{table_name}' AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = #{schema ? "'#{schema}'" : 'ANY (current_schemas(false))'} ) ORDER BY i.relname SQL result.map do |row| index_name = row[0] unique = row[1] == 't' indkey = row[2].split(" ") inddef = row[3] oid = row[4] columns = Hash[query(<<-SQL, "SCHEMA")] SELECT a.attnum, a.attname FROM pg_attribute a WHERE a.attrelid = #{oid} AND a.attnum IN (#{indkey.join(",")}) SQL column_names = columns.values_at(*indkey).compact unless column_names.empty? # add info on sort order for columns (only desc order is explicitly specified, asc is the default) desc_order_columns = inddef.scan(/(\w+) DESC/).flatten orders = desc_order_columns.any? ? Hash[desc_order_columns.map {|order_column| [order_column, :desc]}] : {} where = inddef.scan(/WHERE (.+)$/).flatten[0] using = inddef.scan(/USING (.+?) /).flatten[0].to_sym ::ActiveRecord::ConnectionAdapters::IndexDefinition.new(table_name, index_name, unique, column_names, [], orders, where, nil, using) end end.compact end |
#quote_local_table_name(name) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/switchman/active_record/postgresql_adapter.rb', line 120 def quote_local_table_name(name) # postgres quotes tables and columns the same; just pass through # (differs from quote_table_name below by no logic to explicitly # qualify the table) quote_column_name(name) end |
#quote_table_name(name) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/switchman/active_record/postgresql_adapter.rb', line 127 def quote_table_name name if ::Rails.version < '4.2'.freeze schema, name_part = extract_pg_identifier_from_name(name.to_s) if !name_part && use_qualified_names? && shard.name schema, name_part = shard.name, schema end unless name_part quote_column_name(schema) else table_name, name_part = extract_pg_identifier_from_name(name_part) "#{quote_column_name(schema)}.#{quote_column_name(table_name)}" end else name = ::ActiveRecord::ConnectionAdapters::PostgreSQL::Utils.extract_schema_qualified_name(name.to_s) if !name.schema && use_qualified_names? name.instance_variable_set(:@schema, shard.name) end name.quoted end end |
#table_exists?(name) ⇒ Boolean
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/switchman/active_record/postgresql_adapter.rb', line 26 def table_exists?(name) if ::Rails.version < '4.2' schema, table = ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::Utils.extract_schema_and_table(name.to_s) return false unless table schema ||= shard.name if use_qualified_names? binds = [[nil, table]] binds << [nil, schema] if schema exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0 SELECT COUNT(*) FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind in ('v','r') AND c.relname = '#{table.gsub(/(^"|"$)/,'')}' AND n.nspname = #{schema ? "'#{schema}'" : 'ANY (current_schemas(false))'} SQL else name = ::ActiveRecord::ConnectionAdapters::PostgreSQL::Utils.extract_schema_qualified_name(name.to_s) return false unless name.identifier if !name.schema && use_qualified_names? name.instance_variable_set(:@schema, shard.name) end exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0 SELECT COUNT(*) FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view AND c.relname = '#{name.identifier}' AND n.nspname = #{name.schema ? "'#{name.schema}'" : 'ANY (current_schemas(false))'} SQL end end |
#tables(name = nil) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/switchman/active_record/postgresql_adapter.rb', line 16 def tables(name = nil) schema = shard.name if use_qualified_names? query(<<-SQL, 'SCHEMA').map { |row| row[0] } SELECT tablename FROM pg_tables WHERE schemaname = #{schema ? "'#{schema}'" : 'ANY (current_schemas(false))'} SQL end |
#use_qualified_names? ⇒ Boolean
12 13 14 |
# File 'lib/switchman/active_record/postgresql_adapter.rb', line 12 def use_qualified_names? @config[:use_qualified_names] end |