-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instructions for querying Publisher's database
The instructions on the GOV.UK developer docs don't work for querying data in Publisher's database. Add some instructions for how to query Publisher's database in deployed environments.
- Loading branch information
1 parent
73a169f
commit 5a4f7f6
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
## Querying the database | ||
|
||
Publisher uses MongoDB for its data storage (technically, it only uses MongoDB when running locally—on AWS it uses DocumentDB, which is "Mongo-compatible"). There are two ways of querying the data stored in the database: | ||
1. using [Mongoid](https://www.mongodb.com/docs/mongoid/current/) | ||
2. using a Mongo shell. | ||
|
||
The GOV.UK developer docs document [how to open a database command-line session](https://docs.publishing.service.gov.uk/manual/databases.html#open-a-database-commmand-line-session) of a Rails application deployed to k8s, but this doesn't work for Publisher, as attempting to run the specified command results in an exception. | ||
|
||
To query the database, first [establish access to the k8s cluster](https://docs.publishing.service.gov.uk/kubernetes/get-started/access-eks-cluster/#access-a-cluster-that-you-have-accessed-before), then [open a rails console](https://docs.publishing.service.gov.uk/kubernetes/cheatsheet.html#open-a-rails-console) on one of the publisher pods. | ||
|
||
### Querying using Mongoid | ||
|
||
Mongoid is "the officially supported object-document mapper (ODM) for MongoDB in Ruby". It can be used to query the database using Rails models. | ||
|
||
From within the Rails console, query the data using [Mongoid](https://www.mongodb.com/docs/mongoid/current/reference/queries/). For example: | ||
|
||
```shell | ||
Edition.first | ||
=> #<GuideEdition _id: ...> | ||
``` | ||
returns the first edition in the database, as a Rails model object. | ||
|
||
### Querying using a Mongo shell | ||
|
||
One thing to be aware of with this technique, is that Publisher's database is actually shared with other apps, so there is data within it that does not belong to Publisher—don't be surprised when querying if you see collections that you don't recognise. | ||
|
||
From within the Rails console, query the data using the Mongo shell: | ||
|
||
```ruby | ||
client = Mongo::Client.new(ENV["MONGODB_URI"]) | ||
db = client.database | ||
|
||
db.collection_names | ||
=> | ||
["artefacts", | ||
... | ||
] | ||
``` |