Google Cloud Spanner with Active Record

Knut Olav Løite
Google Cloud - Community
3 min readSep 17, 2021

Google Cloud Spanner is a fully managed, scalable, relational database service for regional and global application data. It is the first scalable, enterprise-grade, globally-distributed, and strongly consistent database service built for the cloud specifically to combine the benefits of relational database structure with non-relational horizontal scale.

Active Record is an ORM implementation for Ruby. Active Record can now serve as an object-relational mapper (O/RM) for Google Cloud Spanner using its open source provider. This article will help you get started with Active Record for Spanner by creating a simple Ruby application that uses Spanner through Active Record.

The entire sample application can be found here: https://github.com/olavloite/spanner-activerecord-example

Configuring Spanner Adapter and Database

First add the Spanner Active Record adapter to your project by adding the following to your Gemfile: gem 'activerecord-spanner-adapter'

Then we need to configure a database for use with the Spanner Active Record adapter. We do this in a database.yml file:

Example database configuration for Spanner Active Record

The example configuration file will connect to a Spanner emulator on localhost. Remove the emulator_host entry in the configuration file to connect to a real Spanner instance.

Create the Database and Tables

Creating the tables in your database can easily be done by creating an initial migration for your project in the db folder.

01_create_tables.rb

The example migration file will create two tables; singers and albums. Note that:

  1. The tables are defined in a ddl_batch block. DDL batches significantly reduces the execution time of multiple DDL statements, and it is highly recommended to use DDL batches for all migrations that contain more than one DDL statement.
  2. The albums table references the singers table using a foreign key constraint.
  3. The singers table defines a generated column for the full name of a singer. The value of this column is always computed, and it is not possible to write other values to this column.
  4. Both tables define a last_updated column. These columns may be updated with the commit timestamp of the transaction that last updated the record.

Fill the Database with Test Data

You can use a seeds.rb file for the initial (test) data for your database.

You can then use the rake db:seed command to seed your database. This is automatically done by the Rakefile in the example application.

Note the last_updated: :commit_timestamp hash values in the seeds file. Setting a field that has been marked with allow_commit_timestamp to the symbol :commit_timestamp will instruct the Spanner Active Record adapter to set the value of the record in the database to the commit timestamp of the transaction that inserts or updates the record.

Running the Application

You can run the sample application by executing the following command in the root of the project:

bundle exec rake run

This will invoke the following script:

Rakefile for running the Spanner Active Record Sample Application

The steps in this script are:

  1. Download and start the Spanner Emulator in a Docker container
  2. Create a test instance and test database on the emulator.
  3. Execute the migration that we defined for our sample app.
  4. Execute a db:seed command for the database. This will insert the test data in the seeds.rb file.
  5. Run the sample application.

The sample application will execute a couple of queries and update some data using ActiveRecord. Take a look at the https://github.com/olavloite/spanner-activerecord-example/blob/main/application.rb file for the full file.

--

--