Skip to content

Commit

Permalink
Add Branch model
Browse files Browse the repository at this point in the history
This adds a `Branch` model and a representation of a data store's
default branch, allowing us to remove the more specific environment
variable/app configuration.

Branches are annoying because they are undocumented and don't formally
form part of the Discovery Engine API. Every data store has exactly one
branch (the default one), and you don't interact with it in any way
_other than_ when you add/update/remove documents on the datastore.
  • Loading branch information
csutter committed Feb 7, 2025
1 parent e2c211a commit 1ef8275
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 5 deletions.
21 changes: 21 additions & 0 deletions app/models/branch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Represents a branch on a Discovery Engine data store.
#
# Currently, every data store on Discovery Engine has exactly *one* branch (the default branch), and
# we are not able to make any changes to that resource. However, we still need to model it here
# because documents are children of the branch, not the datastore itself.
#
# see https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1/latest/Google-Cloud-DiscoveryEngine-V1-DocumentService-Client
# (there is no documentation specific to branches)
Branch = Data.define(:remote_resource_id) do
include DiscoveryEngineNameable

# The default branch automatically available on a data store
def self.default
new("default_branch")
end

def parent
# We only use a single data store in our architecture, so we can hardcode it here.
DataStore.default
end
end
2 changes: 1 addition & 1 deletion app/services/discovery_engine/sync/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def version_cache
end

def document_name
"#{Rails.configuration.discovery_engine_datastore_branch}/documents/#{content_id}"
[Branch.default.name, "documents", content_id].join("/")
end

def log(level, message)
Expand Down
1 change: 0 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Application < Rails::Application
# Google Discovery Engine configuration
config.discovery_engine_default_collection_name = ENV.fetch("DISCOVERY_ENGINE_DEFAULT_COLLECTION_NAME")
config.discovery_engine_serving_config = ENV.fetch("DISCOVERY_ENGINE_SERVING_CONFIG")
config.discovery_engine_datastore_branch = ENV.fetch("DISCOVERY_ENGINE_DATASTORE_BRANCH")
config.google_cloud_project_id = ENV.fetch("GOOGLE_CLOUD_PROJECT_ID")

# Document sync configuration
Expand Down
15 changes: 15 additions & 0 deletions spec/models/branch_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
RSpec.describe Branch do
subject(:branch) { described_class.new("my-branch") }

describe ".default" do
it "returns the default branch" do
expect(described_class.default).to eq(described_class.new("default_branch"))
end
end

describe "#name" do
it "returns the fully qualified name of the branch" do
expect(subject.name).to eq("[collection]/dataStores/govuk_content/branches/my-branch")
end
end
end
2 changes: 1 addition & 1 deletion spec/services/discovery_engine/sync/delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

it "deletes the document" do
expect(client).to have_received(:delete_document)
.with(name: "branch/documents/some_content_id")
.with(name: "#{Branch.default.name}/documents/some_content_id")
end

it_behaves_like "a successful sync operation", "delete"
Expand Down
2 changes: 1 addition & 1 deletion spec/services/discovery_engine/sync/put_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
expect(client).to have_received(:update_document).with(
document: {
id: "some_content_id",
name: "branch/documents/some_content_id",
name: "#{Branch.default.name}/documents/some_content_id",
json_data: "{\"foo\":\"bar\",\"payload_version\":\"1\"}",
content: {
mime_type: "text/html",
Expand Down
1 change: 0 additions & 1 deletion spec/services/discovery_engine/sync/shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
before do
allow(Kernel).to receive(:sleep).and_return(nil)
allow(Rails).to receive(:logger).and_return(logger)
allow(Rails.configuration).to receive(:discovery_engine_datastore_branch).and_return("branch")
allow(GovukError).to receive(:notify)

allow(Coordination::DocumentLock).to receive(:new).with("some_content_id").and_return(lock)
Expand Down

0 comments on commit 1ef8275

Please sign in to comment.