dynamoid icon indicating copy to clipboard operation
dynamoid copied to clipboard

changed namespacing does not work if a global secondary index is defined on a table

Open gsampathkumar opened this issue 8 years ago • 7 comments

I've implemented a sharded scheme for our DynamoDB setup. The logic is as follows:

...figure out what shard our data is in...

Dynamoid.configure do |config| config.namespace = "shard name" end

...query tables...

This works perfectly in the development environment. On the production environment it works for any table that does not have a global secondary index and fails for the others.

gsampathkumar avatar Jan 15 '17 06:01 gsampathkumar

Ouch. I am doing the same thing for sharding (one shard for each environment). However I only use tables with global secondary index, so I suppose I haven't seen this issue. Can you create a failing spec?

pboling avatar Jan 15 '17 23:01 pboling

@gsampathkumar @pboling - I was facing the same problem and tried reproducing the problem by creating a failing spec, but it's not a problem in the specs with/without dummy Index.

Then I looked at my own code closer and found this:

The class method global_secondary_index must be called after namespace has been defined. Otherwise the namespace definition will not be take effect.

For my case, I simply needed to make sure of that by carefully checking and ordering my require statements (I'm creating an rails_event_store repository for dynamoid)

As for you, it is probably working in development environment and not in production because the classes are loaded eagerly in production.

I'd say this is not a bug of dynamoid (although it should be mentioned in the docs as a caveat).

mmhan avatar Sep 26 '18 06:09 mmhan

@andrykonchin Additionally, I am still seeing this in the readme:

find_all_by_secondary_index(
    {
        dynamo_primary_key_column_name => dynamo_primary_key_value
    }, # The signature of find_all_by_secondary_index is ugly, so must be an explicit hash here
    :range => {
        "#{range_column}.#{range_modifier}" => range_value
    },
    # false is the same as DESC in SQL (newest timestamp first)
    # true is the same as ASC in SQL (oldest timestamp first)
    scan_index_forward: false # or true
)

I was under the impression that method had been removed/deprecated in 3.0.0 just released. Have I misunderstood?

pboling avatar Sep 28 '18 03:09 pboling

@pboling Yeah, you are right. Nice catch.

andrykonchin avatar Sep 28 '18 06:09 andrykonchin

@andrykonchin That is literally the only method I use in the gem, so I need to figure out how to migrate my code.

pboling avatar Sep 28 '18 10:09 pboling

@pboling I re-checked specs for find_all_by_secondary_index and can confirm that it could be completely replaced with where and other chained methods.

There is only one subtle difference in selecting of index because where fallbacks to Scan if cannot use Query with or without index but find_all_by_secondary_index raises exception.

You can find examples of replacing find_all_by_secondary_index with where here:

https://gist.github.com/andrykonchin/423ef436b580eede5949bf2f930af788

andrykonchin avatar Mar 02 '19 20:03 andrykonchin

@fleshins You'll need this if/when you upgrade to Dynamoid v3!

pboling avatar Mar 15 '19 23:03 pboling