The purpose of this step by step tutorial is to provide a very simple example of configuring and using Cockroach database engine with the Ruby Language.
-
CockroachDB 22.2.0 or newer
-
Libpq 7.2.0 or newer
-
Node 18.12.1 or newer
-
Rails 7.0.4.1 or newer
-
Ruby 3.1.2 or newer
-
Yarn 1.22.19 or newer
Note: This tutorial was created using macOS 12.6.2.
- If you need help, use Stack Overflow. (Tag 'cockroachdb')
- If you'd like to ask a general question, use Stack Overflow.
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.
-
Open new terminal window
-
Create node1 of CockroachDB Cluster
cockroach start \ --insecure \ --store=node1 \ --listen-addr=localhost:26257 \ --http-addr=localhost:8080 \ --join=localhost:26257,localhost:26258,localhost:26259 \ --background
-
Create node2 of CockroachDB Cluster
cockroach start \ --insecure \ --store=node2 \ --listen-addr=localhost:26258 \ --http-addr=localhost:8081 \ --join=localhost:26257,localhost:26258,localhost:26259 \ --background
-
Create node3 of CockroachDB Cluster
cockroach start \ --insecure \ --store=node3 \ --listen-addr=localhost:26259 \ --http-addr=localhost:8082 \ --join=localhost:26257,localhost:26258,localhost:26259 \ --background
-
Perform one-time initialization the CockroachDB Cluster
cockroach init --insecure --host=localhost:26257
Note: One should see the following successful message:
Cluster successfully initialized
-
Connect to the built-in SQL Client
cockroach sql --insecure --host=localhost:26257
-
Create the database by entering the following within the console:
e.g.
root@127.0.0.1:26257/defaultdb> CREATE DATABASE cockroach_example_using_rails_development;
-
Open another terminal window
-
Generate a new Rails application
rails new cockroach-example-using-ruby -d postgresql --skip-active-storage --skip-javascript -T --no-rc
-
Add the ActiveRecord CockroachDB Adapter by doing the following:
bundle remove pg bundle add activerecord-cockroachdb-adapter --version "~> 7.0.0" bundle add sassc-rails
-
Update the database adapter as follows within
database.yml
:replace
adapter: postgresql
with
adapter: cockroachdb
-
Add the following after the
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
setting withindatabase.yml
:host: <%= ENV.fetch("COCKROACH_HOST") { 'localhost' } %> port: <%= ENV.fetch("COCKROACH_PORT") { 26257 } %> user: <%= ENV.fetch("COCKROACH_USER") { 'root' } %> password: <%= ENV.fetch("COCKROACH_PASSWORD") { 'admin' } %> requiressl: true
-
Generate scaffold of the application
rails g scaffold post title body:text
-
Migrate the database.
rails db:migrate
-
Add the following as the first route within config/routes.rb file:
root 'posts#index'
-
Start the Rails server
rails s
-
Play with the application
open http://localhost:3000
-
Clean CockroachDB Cluster
cockroach quit --insecure --host=localhost:26257 cockroach quit --insecure --host=localhost:26258 cockroach quit --insecure --host=localhost:26259 rm -rf node1 node2 node3
Bug reports and feature requests can be filed for the cassandra-example-using-ruby project here:
Follow Conrad Taylor on Twitter (@conradwt)
This repository is released under the MIT License.
© Copyright 2021 - 2023 Conrad Taylor. All Rights Reserved.