Skip to main content

RavenDB

Overview

RavenDB is our primary database, its a document database with integrated Lucene search indexes for powerful & fast querying capabilities

https://ravendb.net/

Rather than hosting RavenDB locally we have a hctr-dev cluster running in AWS London.

Each developer has their own database to allow them to work in isolation without impacting other developers or testing

Hctr-Dev Cluster Studio Access

RavenDB studio is available over HTTP, it is secured via an x509 certificate, you need to install the certificate in your local key chain in order to be able to access the studio from the cloud portal.

The certificate for the Hectare dev cluster is in the Engineering\RavenDB\Certificates folder on Google Drive, download the hctr-dev.hectare.client.certificate.with.password.pfx file to your local PC.

Follow these instructions to install the certificate on MacOS

  1. Open KeyChain app
  2. Click on Login keychain
  3. In the top menu choose File > Import Items
  4. Choose the .pfx file you downloaded from Google Drive
  5. When prompted enter the password from the Backend Secrets secure note in LastPass under section RAVEN CERTIFICATE PASSWORDS (DEV/UAT password)
  6. After the certificate has been imported click on the My Certificates tab and locate the hctr-dev.hectare certificate
  7. Expand the certificate by clicking the arrow so you can see the private key
  8. Right click the private key and choose "Get Info"
  9. Click the access control tab and check the "Allow all applications to access this item"
  10. Click "Save Changes"
  11. Authenticate if required

Once the above steps have been completed the certificate will be available for your browser to use when accessing the RavenDB Studio

Click this link to access Raven Studio

You will be prompted to choose a certificate from a pop up, choose the hctr-dev.hectare certificate file, if you are prompted to authenticate again ensure you choose Allow Always so you don't have to keep doing this.

Baseline Database

We maintain a baseline dev database for use by developers in their local environments and in the Dev environment. This data can be added to at any time by following these steps.

  1. Open overrides.json in your local environment and change the developer setting to Baseline
  2. Start the APIs
  3. Create or modify any data via the APIs or Client as necessary (or direct to the database if this is easier)
  4. Revert the overrides.json

Once you have added more data to the baseline database, you need to create a new backup of the Baseline database. Backups are stored in an S3 bucket, create a new backup by following these steps.

  1. Open the Platform_Baseline database in RavenDb
  2. Open the Tasks menu and click Backups
  3. You should see an existing backup called Baseline Database Backup, click the Info icon to the right of the backup
  4. Click Backup Now

This will create a backup and upload it to S3, you can then restore the backup to your local database by running this script (make sure your developer setting is set to your name in overrides.json before restoring)

pnpm run db:local

RavenDB Node.js Client

The RavenDB Node.js client is open source and a very useful resource for understanding how to use the database from Node.js. There is a large suite of unit tests which cover the vast majority of the use cases we'll need. You can clone the repo from here

The documentation for the client also covers many of the common use cases

RavenDB Data Migrations

Each service has its own database and therefore its own migrations app for updating each database. We maintain a directory of migration scripts for each database, locate here...

./services/inventory/api/migrations/scripts
./services/system/api/migrations/scripts
./services/logistics/api/migrations/scripts

etc...

When you want to run migrations for a specific database simply run the appropriate command as follows

pnpm run db:customers
pnpm run db:inventory
pnpm run db:system
pnpm run db:trading

This will update the database on https://a.hctr-dev.hectare.ravendb.cloud that you are connected to via your configuration, the default is InventoryDev unless you have overridden the database through your local config overrides.json, see configuration for more info on local config overrides.

Schema and Index Changes

When you run migrations you are potentially updating search indexes and changing document structure, this can obviously cause breaking changes in the applications using the database being updated.

We should always approach schema and index changes as a 2 phase update.

When we identify a schema or index change that our under development feature relies upon the changes need to be deployed in a safe manner before we release the feature so that when we come to deploy the feature any database changes are already in place.

IDs

In RavenDB you IDs can be auto generated using a hilo algorithm or they can be manually specified.

We use HiLo IDs in general, this works by the RavenDB client requesting a set of IDs (usually 32) from the server and using up these IDs as when new entities are created, when the 32 have been used another batch is retrieved from the server.

HiLo IDs are in for format [collection]/[numeric_id]-[node] i.e. for an asset

assets/1-A

where assets is the collection name, 1 is the ID and A is the node where the document was created.

One issue with this format is it can intefere with URLs as the collection name will be interpreted as part of the URL path, therefore when returning IDs from an API we should strip the collection name from the ID and map it back when IDs are passed into APIs.