Working with databases you don't maintain

September 25, 2024

Working with databases you don't maintain

This is something we've come up against a handful of times where we are building an application where a portion of the app needs to use an existing database that we don't maintain. We didn't design it, we can't modify it (at least not easily), nothing. This by default introduces a few unique challenges but the one I struggle with most is testing. (Also, every time we've encountered this it has been with a SQL Server database which adds an additional layer of complexity.) Now thankfully for these projects the companies have been able to provide us with a "staging" database. These databases are as close to a production copy as possible that are great for our development and QA processes but we still can't really use these staging databases for tests for a couple of reasons.

  1. We need the freedom to totally wipe the database and freely create, edit and delete data which could cause issues when that data also being relied on for QA.
  2. Most times these staging databases require being connected to a VPN or are only accessible from a whitelisted IP address so we may not be able to easily access these databases from within our CI/CD pipelines where we're running our automated test suites.

So, how we can safely and confidently write tests that need to interact with a database we don't control? The best solution I've come up with so far is to get a schema dump of the database that contains just the structure, no data and using that to build up our test database. In our specific scenario, this also requires spinning up a SQL Server Docker container. The downside here is if/when the company ever makes changes to that database, we need to be alerted so that we can make sure our schema dump is in sync with the current production version. Outside of this, we still write our Factories and Seeders like we normally would, just making sure our Models are pointed to the correct database connection.


© 2024 Terrence Eisenhower. All rights reserved.