This adapter only supports GoogleSQL-dialect Spanner databases. PostgreSQL-dialect
databases are not supported. You can use the standard PostgreSQL ActiveRecord adapter in
combination with PGAdapter
for Spanner PostgreSQL-dialect databases.
This project provides a Spanner adapter for ActiveRecord. It supports the following versions:
ActiveRecord 7.0.x with Ruby 3.1 and higher.
ActiveRecord 7.1.x with Ruby 3.1 and higher.
ActiveRecord 7.2.x with Ruby 3.1 and higher.
ActiveRecord 8.0.x with Ruby 3.2 and higher.
Known limitations are listed in the Limitations section.
Please report any problems that you might encounter by creating a new issue.
Installation
Add this line to your application's Gemfile:
gem'activerecord-spanner-adapter'
If you would like to use latest adapter version from github then specify
Use DDL batching when executing migrations for the best possible performance.
Executing multiple schema changes on Spanner can take a long time. It is therefore
strongly recommended that you limit the number of schema change operations. You can do
this by using DDL batching in your migrations. See the migrations examples
for how to do this.
Database Connection
In Rails application config/database.yml, make the change as the following:
You can also find a list of short self-contained code examples that show how
to use ActiveRecord with Spanner under the directory examples/snippets. Each example is directly runnable without the need to setup a Spanner
database, as all samples will automatically start a Spanner emulator in a Docker container and execute the sample
code against that emulator. All samples can be executed by navigating to the sample directory on your local machine and
then executing the command bundle exec rake run. Example:
cd ruby-spanner-activerecord/examples/snippets/quickstart
bundle exec rake run
NOTE: You do need to have Docker installed on your local system to run these examples.
Some noteworthy examples in the snippets directory:
quickstart: A simple application that shows how to create and query a simple database containing two tables.
migrations: Shows a best-practice for executing migrations on Spanner.
bulk-insert: Shows the best way to insert a large number of new records.
mutations: Shows how you can use mutations instead of DML
for inserting, updating and deleting data in a Spanner database. Mutations can have a significant performance
advantage compared to DML statements, but do not allow read-your-writes semantics during a transaction.
Spanner requires composite primary keys for interleaved tables. See this example for an example on how to use interleaved tables with ActiveRecord
Use composite primary keys.
Lack of sequential IDs
Spanner uses either bit-reversed IDENTITY columns or sequences to generated primary keys to avoid hotspotting so you SHOULD NOT rely on IDs being sorted
Use either IDENTITY columns or bit-reversed sequences to automatically generate primary keys.
Table without Primary Key
Spanner support does not support tables without a primary key.
Always define a primary key for your table.
Table names CANNOT have spaces within them whether back-ticked or not
Spanner DOES NOT support tables with spaces in them for example Entity ID
Ensure that your table names don't contain spaces.
Table names CANNOT have punctuation marks and MUST contain valid UTF-8
Spanner DOES NOT support punctuation marks e.g. periods ".", question marks "?" in table names
Ensure that your table names don't contain punctuation marks.
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Activerecord::Spanner project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["Version latestkeyboard_arrow_down\n\n- [2.2.0 (latest)](/ruby/docs/reference/activerecord-spanner-adapter/latest)\n- [2.1.0](/ruby/docs/reference/activerecord-spanner-adapter/2.1.0)\n- [2.0.0](/ruby/docs/reference/activerecord-spanner-adapter/2.0.0) \n\nActiveRecord Cloud Spanner Adapter\n==================================\n\n[Google Cloud Spanner](https://cloud.google.com/spanner) adapter for ActiveRecord.\n\n**This adapter only supports GoogleSQL-dialect Spanner databases. PostgreSQL-dialect\ndatabases are not supported. You can use the standard PostgreSQL ActiveRecord adapter in\n[combination with PGAdapter](https://github.com/GoogleCloudPlatform/pgadapter/blob/-/samples/ruby/activerecord)\nfor Spanner PostgreSQL-dialect databases.**\n\nThis project provides a Spanner adapter for ActiveRecord. It supports the following versions:\n\n- ActiveRecord 7.0.x with Ruby 3.1 and higher.\n- ActiveRecord 7.1.x with Ruby 3.1 and higher.\n- ActiveRecord 7.2.x with Ruby 3.1 and higher.\n- ActiveRecord 8.0.x with Ruby 3.2 and higher.\n\nKnown limitations are listed in the [Limitations](#limitations) section.\nPlease report any problems that you might encounter by [creating a new issue](https://github.com/googleapis/ruby-spanner-activerecord/issues/new).\n\nInstallation\n------------\n\nAdd this line to your application's Gemfile: \n\n```ruby\ngem 'activerecord-spanner-adapter'\n```\n\nIf you would like to use latest adapter version from github then specify \n\n```ruby\ngem 'activerecord-spanner-adapter', :git =\u003e 'git@github.com:googleapis/ruby-spanner-activerecord.git'\n```\n\nAnd then execute: \n\n $ bundle\n\nUsage\n-----\n\n### Migrations\n\n**Use DDL batching when executing migrations for the best possible performance.**\n\nExecuting multiple schema changes on Spanner can take a long time. It is therefore\nstrongly recommended that you limit the number of schema change operations. You can do\nthis by using DDL batching in your migrations. See [the migrations examples](/ruby/docs/reference/activerecord-spanner-adapter/latest/examples/snippets/migrations)\nfor how to do this.\n\n### Database Connection\n\nIn Rails application `config/database.yml`, make the change as the following: \n\n development:\n adapter: \"spanner\"\n project: \"\u003cgoogle project name\u003e\"\n instance: \"\u003cgoogle instance name\u003e\"\n credentials: \"\u003cgoogle credentails file path\u003e\"\n database: \"app-dev\"\n\nExamples\n--------\n\nTo get started with Rails, read the tutorial under [examples/rails/README.md](./README \"examples/rails/README.md\").\n\nYou can also find a list of short self-contained code examples that show how\nto use ActiveRecord with Spanner under the directory [examples/snippets](/ruby/docs/reference/activerecord-spanner-adapter/latest/examples/snippets). Each example is directly runnable without the need to setup a Spanner\ndatabase, as all samples will automatically start a Spanner emulator in a Docker container and execute the sample\ncode against that emulator. All samples can be executed by navigating to the sample directory on your local machine and\nthen executing the command `bundle exec rake run`. Example: \n\n cd ruby-spanner-activerecord/examples/snippets/quickstart\n bundle exec rake run\n\n**NOTE** : You do need to have [Docker](https://docs.docker.com/get-docker/) installed on your local system to run these examples.\n\nSome noteworthy examples in the snippets directory:\n\n- [quickstart](/ruby/docs/reference/activerecord-spanner-adapter/latest/examples/snippets/quickstart): A simple application that shows how to create and query a simple database containing two tables.\n- [migrations](/ruby/docs/reference/activerecord-spanner-adapter/latest/examples/snippets/migrations): Shows a best-practice for executing migrations on Spanner.\n- [auto-generated-primary-key](/ruby/docs/reference/activerecord-spanner-adapter/latest/examples/snippets/auto-generated-primary-key): Shows how to use IDENTITY columns for primary keys.\n- [bit-reversed-sequences](/ruby/docs/reference/activerecord-spanner-adapter/latest/examples/snippets/bit-reversed-sequence): Shows how to use bit-reversed sequences for primary keys.\n- [read-write-transactions](/ruby/docs/reference/activerecord-spanner-adapter/latest/examples/snippets/read-write-transactions): Shows how to execute transactions on Spanner.\n- [read-only-transactions](/ruby/docs/reference/activerecord-spanner-adapter/latest/examples/snippets/read-only-transactions): Shows how to execute read-only transactions on Spanner.\n- [bulk-insert](/ruby/docs/reference/activerecord-spanner-adapter/latest/examples/snippets/bulk-insert): Shows the best way to insert a large number of new records.\n- [mutations](/ruby/docs/reference/activerecord-spanner-adapter/latest/examples/snippets/mutations): Shows how you can use [mutations instead of DML](https://cloud.google.com/spanner/docs/dml-versus-mutations) for inserting, updating and deleting data in a Spanner database. Mutations can have a significant performance advantage compared to DML statements, but do not allow read-your-writes semantics during a transaction.\n- [array-data-type](/ruby/docs/reference/activerecord-spanner-adapter/latest/examples/snippets/array-data-type): Shows how to work with `ARRAY` data types.\n- [interleaved-tables](/ruby/docs/reference/activerecord-spanner-adapter/latest/examples/snippets/interleaved-tables-before-7.1): Shows how to work with [Interleaved Tables](https://cloud.google.com/spanner/docs/schema-and-data-model#create-interleaved-tables).\n\nLimitations\n-----------\n\nContributing\n------------\n\nBug reports and pull requests are welcome on GitHub at \u003chttps://github.com/googleapis/ruby-spanner-activerecord\u003e. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\nLicense\n-------\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\nCode of Conduct\n---------------\n\nEveryone interacting in the Activerecord::Spanner project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/googleapis/ruby-spanner-activerecord/blob/main/CODE_OF_CONDUCT.md)."]]