Search code examples
ruby-on-railsruby-on-rails-4cql3cequel

How to generate a delete statement with Cequel (Rails)


I want to generate a DELETE statement with Cequel:

DELETE FROM users where pk = 'jsmith' and cc < 100;

Let's say my user model looks like this

class Users
    include Cequel::Record

    key :pk, :bigint, { partition: true } # partition key
    key :cc, :timestamp, { order: :desc } # clustering column

end

Right now, I am iterating through the rows using a simple where clause and destroy them one by one, I know this is not the right way to do it, but I can't find a way to generate the correct statement to delete them all at once.

How can I use my Users model to generate the above CQL statement.

EDIT: also posted here


Solution

  • About the smaller-than part, it is not allowed to use it on a primary key part in a DELETE statement.

     ~ cqlsh
    Connected to Test Cluster at 127.0.0.1:9042.
    [cqlsh 5.0.1 | Cassandra 2.1.2 | CQL spec 3.2.0 | Native protocol v3]
    Use HELP for help.
    cqlsh:db_development> DELETE FROM users WHERE pk = 'jsmith' AND cc < 100 ;
    code=2200 [Invalid query] message="Invalid operator < for PRIMARY KEY part cc"
    cqlsh:db_development> DELETE FROM users WHERE pk = 'jsmith' AND cc = 100 ;
    cqlsh:db_development>
    

    The delete_all method does create a DELETE statement.
    here is how to use it:

    irb(main):036:0> Users.where(pk: 'jsmith').where(cc: 100).delete_all
    CQL (2ms) DELETE FROM users WHERE pk = 'jsmith' AND cc = 100
    => #<Cassandra::Result:0x3fdb891da8cc @rows=[] @last_page=true>
    irb(main):037:0>
    

    As I have more clustering columns after the cc one, iterating through the ccs gives me a more effective solution than iterating through each of the other clustering columns under cc.

    Some more explanations on github: https://github.com/cequel/cequel/issues/237