Skip to content

Instantly share code, notes, and snippets.

@coffeemug
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coffeemug/9518214 to your computer and use it in GitHub Desktop.
Save coffeemug/9518214 to your computer and use it in GitHub Desktop.

Migrating to RethinkDB 1.12

There are a number of breaking changes in RethinkDB 1.12 you should be aware of before migrating.

Data migration

First, make sure to go through the regular data migration process, since the 1.12 file format isn't compatible with file formats generated by previous versions of RethinkDB.

Then, replace group_by and grouped_map_reduce commands in your applications with the new group command.

Migrating the group_by command

Here is an example of an old-style group_by query:

r.table('users').group_by('location', r.count('age'))

And here is how to write the same query in RethinkDB as of 1.12:

r.table('users').group('location').count('age')

Note that all commands chained after group will operate on each group, not on the full resultset. To operate on the full resultset, first call ungroup. For example, suppose you'd like to order the output of the previous query by the reduction. Here is how you'd do it in previous versions of RethinkDB:

r.table('users').group_by('location', r.count('age')).order_by('reduction')

And here is how to write the same query in RethinkDB as of 1.12:

r.table('users').group('location').count('age').ungroup().order_by('reduction')

Migrating the grouped_map_reduce command

Here is an example of an old-style grouped_map_reduce query:

r.table('users').grouped_map_reduce(lambda row: row['location'],
                                    lambda row: 1,
                                    lambda x, y: x + y)

And here is how to write the same query in RethinkDB as of 1.12:

r.table('users').group(lambda row: row['location'])  \
                .map(lambda row: 1)                  \
                .reduce(lambda x, y: x + y)

If you'd like to order by groups or reductions, make sure to first run ungroup:

r.table('users').group(lambda row: row['location'])  \
                .map(lambda row: 1)                  \
                .reduce(lambda x, y: x + y)          \
                .ungroup().order_by('reduction')

A note on the merge command

The merge command now accepts a function as a possible argument. For example:

r.table('users').merge(lambda user: { 'age': user['age'] + 1 })

That means that in some cases old code that uses r.row will error:

# Will error due to nested use of `r.row`
r.table("foo").map(r.row.merge({ 'full_name' : r.row["first_name"] + r.row["last_name"] }))

To fix the error, use functions instead:

r.table("foo").map(lambda row: row.merge({ 'full_name' : row["first_name"] + row["last_name"] }))

Migrating the run function call in Node.js

Prior to RethinkDB 1.12 there were two ways to call run on a query:

query.run(connection, callback)
query.run({connection: connection, useOutdated: true}, callback)

The second syntax has now been deprecated. Using it will print a warning -- instead use the following syntax:

query.run(connection, [options,] callback);

See #1890 for more information on this change.

@marshall007
Copy link

For those of us dealing with nested calls to r.row() after updates to merge.

r.table("foo").map(r.row.merge({ full_name : r.row("first_name").add(r.row("last_name")) }))

Isn't the better way to rewrite this example something like the following:

r.table("foo").merge({ full_name : r.row("first_name").add(r.row("last_name")) })

Was it possible to call .merge() on a sequence prior to 1.12, or is that new?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment