hsgubert / cassandra_migrations

Cassandra Migrations is a Cassandra database schema migration library for Rails applications.
MIT License
45 stars 43 forks source link

Added support for Collections and Secondary Indexes #16

Closed bsbodden closed 10 years ago

bsbodden commented 10 years ago

One of my projects needed support for the new CQL collections so I've done a very simple implementation of it, alongside with a simple way to crate secondary indexes (need to add a way to removed an index also).

Missing items:

I fixed a set of typos when raising exceptions, e.g. raise Foo('bar') versus raise Foo, 'bar'.

bsbodden commented 10 years ago

I've done a bit more work on this area. I'll send another pull request in a few days with more refinements. So you can safely ignore this one if you want.

hsgubert commented 10 years ago

It seems great bsbodden! I'll not do the merge right know but I will take a look further on the code later.

About your comment of removing secondary indexes, do you know if it is possible with Cassandra 1.2? I was baffled the other day when I discovered I could't remove a column. I mean CQL supports it, but Cassandra 1.2 does not accept the operation.

I also never used collections either (I use Cassandra in a fairly straightforward way mainly because of its scaling and safety). I see you included migration support, but what about loading support for these collections? Is it already working?

Thx

bsbodden commented 10 years ago

You're right, I think we need some sort of matrix of features in CQL for C* 1.2 versus 2.0. I do have loading and saving support implemented for collections but since some one decided to use { } and [] for sets and lists respectively you need to have the column type available when assembling the CQL statement. The only way I could figure out how to do it was to do a query with limit=1 for that specific column and get the type from the result set metadata. Also the is the choice of adding to a set/list/map versus replacing the whole map. So I'm thinking of implementing/overloading the << operator to build that CQL correctly.

Examples:

Update sets:

Adding...

UPDATE users SET emails = emails + {'fb@friendsofmordor.org'} WHERE user_id = 'frodo';

Removing...

UPDATE users SET emails = emails - {'fb@friendsofmordor.org'} WHERE user_id = 'frodo';

Update lists:

Replacing...

UPDATE users SET top_places = [ 'rivendell', 'rohan' ] WHERE user_id = 'frodo';

Adding ...

UPDATE users SET top_places = [ 'the shire' ] + top_places WHERE user_id = 'frodo';

Removing...

UPDATE users SET top_places = top_places + [ 'mordor' ] WHERE user_id = 'frodo';

bsbodden commented 10 years ago

I also wonder if cassandra_migrations should check the target version that's running and throw exceptions when a used feature is not supported? I'll take a look at other migration/ORMs in Ruby and see how they deal with different target versions of the driver/db.

bsbodden commented 10 years ago

I've updated the implementation to support inserting/updating the different collection types and removing secondary indexes along with a new battery of tests.

bsbodden commented 10 years ago

I'll test this for a week or so in the app we're building and then I'll ping you with my comments.

hsgubert commented 10 years ago

Great! I'll review the code through the weekend meanwhile.

hsgubert commented 10 years ago

Merged.

bsbodden, I would ask you to briefly review de readme file and see if any addition is necessary (in respect to using collections).

Thanks for your great work!

hsgubert commented 10 years ago

Also released version 0.0.5 with this modifications already.

bsbodden commented 10 years ago

I will. Thanks.