Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update bit-reversed sequence sample #303

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ __NOTE__: You do need to have [Docker](https://docs.docker.com/get-docker/) inst
Some noteworthy examples in the snippets directory:
- [quickstart](examples/snippets/quickstart): A simple application that shows how to create and query a simple database containing two tables.
- [migrations](examples/snippets/migrations): Shows a best-practice for executing migrations on Cloud Spanner.
- [bit-reversed-sequences](examples/snippets/bit-reversed-sequence): Shows how to use bit-reversed sequences for primary keys.
- [read-write-transactions](examples/snippets/read-write-transactions): Shows how to execute transactions on Cloud Spanner.
- [read-only-transactions](examples/snippets/read-only-transactions): Shows how to execute read-only transactions on Cloud Spanner.
- [bulk-insert](examples/snippets/bulk-insert): Shows the best way to insert a large number of new records.
Expand Down
6 changes: 6 additions & 0 deletions examples/snippets/bit-reversed-sequence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ This example shows how to use a bit-reversed sequence to generate the primary ke
See https://cloud.google.com/spanner/docs/primary-key-default-value#bit-reversed-sequence for more information
about bit-reversed sequences in Cloud Spanner.

## Requirements
Using bit-reversed sequences for generating primary key values in ActiveRecord has the following requirements:
1. You must use __ActiveRecord version 7.1 or higher__.
2. Your models must include a sequence name like this: `self.sequence_name = :singer_sequence`
3. You must create the bit-reversed sequence using a SQL statement in your migrations.

## Creating Tables with Bit-Reversed Sequences in ActiveRecord
You can create bit-reversed sequences using migrations in ActiveRecord by executing a SQL statement using the underlying
connection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ class CreateTables < ActiveRecord::Migration[7.1]
def change
# Execute the entire migration as one DDL batch.
connection.ddl_batch do
# TODO: Uncomment when bit-reversed sequences are supported in the emulator.
# connection.execute "create sequence singer_sequence OPTIONS (sequence_kind = 'bit_reversed_positive')"
connection.execute "create sequence singer_sequence OPTIONS (sequence_kind = 'bit_reversed_positive')"

# Explicitly define the primary key.
create_table :singers, id: false, primary_key: :singerid do |t|
# TODO: Uncomment when bit-reversed sequences are supported in the emulator.
# t.integer :singerid, primary_key: true, null: false,
# default: -> { "GET_NEXT_SEQUENCE_VALUE(SEQUENCE singer_sequence)" }
t.integer :singerid, primary_key: true, null: false, default: -> { "FARM_FINGERPRINT(GENERATE_UUID())" }
t.integer :singerid, primary_key: true, null: false,
default: -> { "GET_NEXT_SEQUENCE_VALUE(SEQUENCE singer_sequence)" }
t.string :first_name
t.string :last_name
end
Expand Down
2 changes: 1 addition & 1 deletion examples/snippets/bit-reversed-sequence/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
t.string "title"
end

create_table "singers", primary_key: "singerid", default: -> { "FARM_FINGERPRINT(GENERATE_UUID())" }, force: :cascade do |t|
create_table "singers", primary_key: "singerid", default: -> { "GET_NEXT_SEQUENCE_VALUE(SEQUENCE singer_sequence)" }, force: :cascade do |t|
t.string "first_name"
t.string "last_name"
end
Expand Down
Loading