Skip to content

Commit

Permalink
Change key in postgres to allow multiple databases with same tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Neira committed Aug 21, 2019
1 parent d78283b commit f6e4f5a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/rasti/db/type_converters/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def to_db(db, collection_name, attribute_name, value)
to_db_mapping = to_db_mapping_for db, collection_name

if to_db_mapping.key? attribute_name
to_db_mapping[attribute_name][:converter].to_db value, to_db_mapping[attribute_name][:sub_type]
to_db_mapping[attribute_name][:converter].to_db value, to_db_mapping[attribute_name][:sub_type]
else
value
end
Expand All @@ -30,7 +30,9 @@ def from_db(object)
private

def to_db_mapping_for(db, collection_name)
@to_db_mapping[collection_name] ||= begin
key = [db.opts[:database], collection_name]

@to_db_mapping[key] ||= begin
columns = Hash[db.schema(collection_name)]

columns.each_with_object({}) do |(name, schema), hash|
Expand Down
6 changes: 6 additions & 0 deletions spec/type_converters/postgres_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
let(:pg) do
Object.new.tap do |pg|

def pg.opts
{
database: 'database'
}
end

def pg.schema(table_name, opts={})
[
[:hash, {db_type: 'hstore'}],
Expand Down

0 comments on commit f6e4f5a

Please sign in to comment.