Module: PostgresHelper
- Defined in:
- lib/db/postgres.rb
Class Method Summary collapse
- .connect_to_postgres(config) ⇒ Object
- .disconnect_from_postgres(pool) ⇒ Object
- .format_postgres_config(connection_string) ⇒ Object
- .get_columns_by_table_postgres(client, schema_name, table_name) ⇒ Object
- .get_foreign_keys_postgres(client, schema_name, table_name, primary_key) ⇒ Object
- .get_schema_column_info_postgres(client, schema_name, table_names) ⇒ Object
- .get_schemas_postgres(client) ⇒ Object
- .get_tables_by_schema_postgres(client, schema_names) ⇒ Object
- .run_query_postgres(sql, pool) ⇒ Object
Class Method Details
.connect_to_postgres(config) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/db/postgres.rb', line 9 def connect_to_postgres(config) # Create a connection pool that matches Puma's thread count ConnectionPool.new(size: 5, timeout: 5) do PG.connect( host: config[:host], port: config[:port], dbname: config[:database], user: config[:username], password: config[:password], sslmode: config[:sslmode] || 'prefer', gssencmode: 'disable', # Disable GSSAPI encryption krbsrvname: nil, # Disable Kerberos service name target_session_attrs: 'read-write' ) end end |
.disconnect_from_postgres(pool) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/db/postgres.rb', line 26 def disconnect_from_postgres(pool) return unless pool.respond_to?(:shutdown) pool.shutdown { |conn| conn.close if conn.respond_to?(:close) } rescue PG::Error => e puts "Error closing connection pool: #{e.message}" end |
.format_postgres_config(connection_string) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/db/postgres.rb', line 192 def format_postgres_config(connection_string) parsed = URI.parse(connection_string) { host: parsed.host, port: parsed.port || 5432, database: parsed.path[1..-1], username: parsed.user || 'postgres', password: parsed.password || '', sslmode: parsed.query&.split('&')&.find { |param| param.start_with?('sslmode=') }&.split('=')&.last || 'prefer' } end |
.get_columns_by_table_postgres(client, schema_name, table_name) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/db/postgres.rb', line 111 def get_columns_by_table_postgres(client, schema_name, table_name) sql = " SELECT column_name \n FROM information_schema.columns \n WHERE table_schema = '\#{schema_name}' \n AND table_name = '\#{table_name}'\n ORDER BY ordinal_position\n SQL\n \n results = run_query_postgres(sql, client)\n results[:rows].map { |row| row['column_name'] }\nend\n" |
.get_foreign_keys_postgres(client, schema_name, table_name, primary_key) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/db/postgres.rb', line 124 def get_foreign_keys_postgres(client, schema_name, table_name, primary_key) depluralized_table_name = table_name.singularize sql = " SELECT column_name \n FROM information_schema.columns \n WHERE table_schema = '\#{schema_name}' \n AND table_name != '\#{table_name}' \n AND (column_name = '\#{primary_key}' \n OR column_name = '\#{depluralized_table_name}_\#{primary_key}' \n OR column_name = '\#{depluralized_table_name}\#{primary_key.capitalize}')\n SQL\n\n results = run_query_postgres(sql, client)\n foreign_keys = results[:rows].map { |key| key['column_name'] }\n \n foreign_keys = foreign_keys.reject { |key| ['id', '_id_'].include?(key) }\n foreign_keys = foreign_keys.uniq\n\n if foreign_keys.empty?\n sql = <<~SQL\n SELECT column_name \n FROM information_schema.columns \n WHERE table_schema = '\#{schema_name}' \n AND table_name != '\#{table_name}' \n AND (column_name LIKE '\#{table_name}%' \n OR column_name LIKE '%\\\\_id' \n OR column_name LIKE '%Id' \n OR column_name LIKE '%\\\\_\#{primary_key}' \n OR column_name LIKE '%\#{primary_key.capitalize}')\n SQL\n\n results = run_query_postgres(sql, client)\n foreign_keys = results[:rows].map { |key| key['column_name'] }.uniq\n end\n\n foreign_keys\nend\n" |
.get_schema_column_info_postgres(client, schema_name, table_names) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/db/postgres.rb', line 163 def get_schema_column_info_postgres(client, schema_name, table_names) table_names.map do |table_name| query = " SELECT \n column_name,\n udt_name as field_type,\n ordinal_position as sort_number\n FROM information_schema.columns\n WHERE table_schema = '\#{table_name[:schemaName]}'\n AND table_name = '\#{table_name[:tableName]}'\n ORDER BY sort_number\n SQL\n\n results = run_query_postgres(query, client)\n {\n tableName: \"\#{table_name[:schemaName]}.\#{table_name[:tableName]}\",\n displayName: \"\#{table_name[:schemaName]}.\#{table_name[:tableName]}\",\n columns: results[:rows].map do |row|\n {\n columnName: row['column_name'],\n displayName: row['column_name'],\n dataTypeID: get_pg_type_oid(row['field_type']),\n fieldType: row['field_type']\n }\n end\n }\n end\nend\n" |
.get_schemas_postgres(client) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/db/postgres.rb', line 78 def get_schemas_postgres(client) sql = " SELECT schema_name \n FROM information_schema.schemata \n WHERE schema_name NOT LIKE 'pg_%' \n AND schema_name != 'information_schema'\n SQL\n \n results = run_query_postgres(sql, client)\n results[:rows].map { |row| row['schema_name'] }\nend\n" |
.get_tables_by_schema_postgres(client, schema_names) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/db/postgres.rb', line 90 def get_tables_by_schema_postgres(client, schema_names) all_tables = schema_names.flat_map do |schema| sql = " SELECT table_name, table_schema \n FROM information_schema.tables \n WHERE table_schema = '\#{schema}'\n AND table_type = 'BASE TABLE'\n SQL\n \n results = run_query_postgres(sql, client)\n results[:rows].map do |row|\n {\n tableName: row['table_name'],\n schemaName: row['table_schema']\n }\n end\n end\n\n all_tables\nend\n" |
.run_query_postgres(sql, pool) ⇒ Object
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/db/postgres.rb', line 33 def run_query_postgres(sql, pool) pool.with do |client| result = client.exec(sql) fields = result.fields.map do |field| { name: field, dataTypeID: result.ftype(result.fields.index(field)) } end # Create a map of field names to their types field_types = result.fields.map.with_index { |field, i| [field, result.ftype(i)] }.to_h json_types = [114, 3802] # JSON and JSONB OIDs rows = result.values.map do |row| result.fields.zip(row).map do |field_name, value| # only parse JSON if the field is a JSON type parsed_value = if json_types.include?(field_types[field_name]) begin json_value = JSON.parse(value.to_s) if json_value.is_a?(Array) json_value.map(&:to_s) else json_value end rescue JSON::ParserError value end else value end [field_name, parsed_value] end.to_h end { fields: fields, rows: rows } ensure result&.clear if result.respond_to?(:clear) end end |