Skip to content

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.

License

Notifications You must be signed in to change notification settings

conradwt/cockroach-example-using-ruby

Repository files navigation

Cockroach Example Using Ruby

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.

Requirements

  • 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.

Communication

  • 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.

Installation, Setup, and Usage

  1. Open new terminal window

  2. 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
  3. 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
  4. 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
  5. Perform one-time initialization the CockroachDB Cluster

    cockroach init --insecure --host=localhost:26257

    Note: One should see the following successful message:

    Cluster successfully initialized
  6. Connect to the built-in SQL Client

    cockroach sql --insecure --host=localhost:26257
  7. 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;
  8. Open another terminal window

  9. Generate a new Rails application

    rails new cockroach-example-using-ruby -d postgresql --skip-active-storage --skip-javascript -T --no-rc
  10. 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
  11. Update the database adapter as follows within database.yml:

    replace

    adapter: postgresql

    with

    adapter: cockroachdb
  12. Add the following after the pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> setting within database.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
  13. Generate scaffold of the application

    rails g scaffold post title body:text
  14. Migrate the database.

    rails db:migrate
  15. Add the following as the first route within config/routes.rb file:

    root 'posts#index'
  16. Start the Rails server

    rails s
  17. Play with the application

    open http://localhost:3000
  18. 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

References

Support

Bug reports and feature requests can be filed for the cassandra-example-using-ruby project here:

Contact

Follow Conrad Taylor on Twitter (@conradwt)

Creator

License

This repository is released under the MIT License.

Copyright

© Copyright 2021 - 2023 Conrad Taylor. All Rights Reserved.

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published