Warning:
This version of CockroachDB is no longer supported. Cockroach Labs supports the current stable release and two releases prior. Please use one of these supported versions.

This tutorial shows you how build a simple Ruby application with CockroachDB using a PostgreSQL-compatible driver or ORM. We've tested and can recommend the Ruby pg driver and the ActiveRecord ORM, so those are featured here.

Tip:
For a more realistic use of ActiveRecord with CockroachDB, see our examples-orms repository.

Before You Begin

Make sure you have already installed CockroachDB.

Step 1. Install the ActiveRecord ORM

To install ActiveRecord as well as the pg driver and a CockroachDB Ruby package that accounts for some minor differences between CockroachDB and PostgreSQL, run the following command:

copy
icon/buttons/copy
$ gem install activerecord pg activerecord-cockroachdb-adapter

Step 2. Start a single-node cluster

For the purpose of this tutorial, you need only one CockroachDB node running in insecure mode:

copy
icon/buttons/copy
$ cockroach start \
--insecure \
--store=hello-1 \
--host=localhost

Step 3. Create a user

In a new terminal, as the root user, use the cockroach user command to create a new user, maxroach.

copy
icon/buttons/copy
$ cockroach user set maxroach --insecure

Step 4. Create a database and grant privileges

As the root user, use the built-in SQL client to create a bank database.

copy
icon/buttons/copy
$ cockroach sql --insecure -e 'CREATE DATABASE bank'

Then grant privileges to the maxroach user.

copy
icon/buttons/copy
$ cockroach sql --insecure -e 'GRANT ALL ON DATABASE bank TO maxroach'

Step 5. Run the Ruby code

The following code uses the ActiveRecord ORM to map Ruby-specific objects to SQL operations. Specifically, Schema.new.change() creates an accounts table based on the Account model (or drops and recreates the table if it already exists), Account.create() inserts rows into the table, and Account.all selects from the table so that balances can be printed.

Copy the code or download it directly.

copy
icon/buttons/copy
require 'active_record'
require 'pg'
require 'activerecord-cockroachdb-adapter'

# Connect to CockroachDB through ActiveRecord.
# In Rails, this configuration would go in config/database.yml as usual.
ActiveRecord::Base.establish_connection(
  adapter:  'cockroachdb',
  username: 'maxroach',
  password: '',
  database: 'bank',
  host:     'localhost',
  port:     26257,
)


# Define the Account model.
# In Rails, this would go in app/models/ as usual.
class Account < ActiveRecord::Base
  validates :id, presence: true
  validates :balance, presence: true
end

# Define a migration for the accounts table.
# In Rails, this would go in db/migrate/ as usual.
class Schema < ActiveRecord::Migration
  def change
    create_table :accounts, force: true do |t|
      t.integer :balance
    end
  end
end

# Run the schema migration by hand.
# In Rails, this would be done via rake db:migrate as usual.
Schema.new.change()

# Create two accounts, inserting two rows into the accounts table.
Account.create(id: 1, balance: 1000)
Account.create(id: 2, balance: 250)

# Retrieve accounts and print out the balances
Account.all.each do |acct|
  puts "#{acct.id} #{acct.balance}"
end

Then run the code:

copy
icon/buttons/copy
$ ruby activerecord-basic-sample.rb

The output should be:

-- create_table(:accounts, {:force=>true})
   -> 0.0361s
1 1000
2 250

To verify that the table and rows were created successfully, you can again use the built-in SQL client:

copy
icon/buttons/copy
$ cockroach sql --insecure -e 'SHOW TABLES' --database=bank
+----------+
|  Table   |
+----------+
| accounts |
+----------+
(1 row)
copy
icon/buttons/copy
$ cockroach sql --insecure -e 'SELECT id, balance FROM accounts' --database=bank
+----+---------+
| id | balance |
+----+---------+
|  1 |    1000 |
|  2 |     250 |
+----+---------+
(2 rows)

What's Next?

Read more about using the ActiveRecord ORM, or check out a more realistic implementation of ActiveRecord with CockroachDB in our examples-orms repository.

You might also be interested in using a local cluster to explore the following core CockroachDB features:



Yes No