Testing Database Changes
Lint and policy checks, throwaway databases in containers, unit tests with utPLSQL, rollback tests, and performance on realistic data.
Testing is what makes CI/CD safe
A pipeline is only as trustworthy as its tests. For database changes, that means more than "the script ran without errors": the SQL follows the team's standards, the change applies cleanly to a fresh database, the code inside the database behaves correctly, the rollback works, and the change is fast enough on production-sized data.
1. Lint and policy checks
The fastest checks read the SQL without running it:
- Linters, such as
sqlfluff, enforce formatting and naming standards and catch common mistakes. - Policy checks flag risky changes for a closer look:
DROP,TRUNCATE,GRANT ... TO PUBLIC, a changeset without a rollback, or a column added asNOT NULLwithout a default to a large table. Liquibase offers built-in policy checks, and simple scripts can do the same.
2. Apply to a fresh database
The pipeline starts a throwaway database, usually in a container, applies every migration from the beginning, and then the new one. If anything fails, the change is broken, or the migrations depend on something that isn't in Git.
| Database | Container images for testing |
|---|---|
| Oracle | Oracle Database Free, from Oracle's container registry, or the popular community images gvenzl/oracle-free, built for CI |
| PostgreSQL, MySQL | The official postgres and mysql images |
| SQL Server | Microsoft's SQL Server images for Linux |
For changes that depend on things containers can't provide, such as specific cloud features, Terraform can build a short-lived cloud database for the test and destroy it afterward. This is an ephemeral environment.
3. Unit tests
Logic inside the database, such as stored procedures, functions, triggers, and constraints, can be tested like application code, with frameworks that run inside the database:
| Database | Framework |
|---|---|
| Oracle | utPLSQL |
| PostgreSQL | pgTAP |
| SQL Server | tSQLt |
-- utPLSQL: a test that a cancelled order earns no V-Buck points
CREATE OR REPLACE PACKAGE test_vbuck_points AS
--%suite(V-Buck points)
--%test(Cancelled orders earn no points)
PROCEDURE cancelled_order_earns_nothing;
END test_vbuck_points;
/
CREATE OR REPLACE PACKAGE BODY test_vbuck_points AS
PROCEDURE cancelled_order_earns_nothing IS
BEGIN
ut.expect(vbuck_points.points_for(p_order_status => 'CANCELLED', p_amount => 250))
.to_equal(0);
END;
END test_vbuck_points;
/
4. Rollback test
A rollback that's never been tested is a hope, not a plan. Liquibase's update-testing-rollback applies the new changes, rolls them back, and applies them again, proving both directions work. With Flyway, teams test their undo scripts the same way in the pipeline.
5. Performance on realistic data
A change that takes a second on an empty test database might lock a 500-million-row production table for an hour. Before production, run the change on a production-sized copy, with sensitive data masked, and check:
- How long the change takes, and whether it locks the table.
- Whether key queries' execution plans change, for better or worse.
- How much extra space, redo, or archive logs it generates.
Cloud clones, such as Autonomous Database clones or Aurora fast clones, make such copies quick and cheap. A staging environment refreshed regularly from masked production data serves the same purpose.
In practice: the "simple" change that would have locked orders
A migration adds a column to the orders table and fills it for every existing row with an UPDATE. On the empty CI database, it takes a second. Kofi's pipeline also runs it on staging, a masked copy of production with 200 million orders. There, it runs for 40 minutes, holding locks the whole time and generating huge amounts of redo. In production, it would have blocked new orders for most of that time.
The performance check fails the build. The team rewrites the change: add the column with a default, which is a quick metadata-only change in recent Oracle versions, then backfill old rows in small batches outside peak hours. Same result, no outage. Only a test on production-sized data could have shown the difference.
Key takeaways
- Lint and policy checks catch style problems and risky statements in seconds.
- Apply every migration to a fresh, throwaway database, usually a container, to prove the change works from scratch.
- Unit-test database code with utPLSQL, pgTAP, or tSQLt.
- Test rollbacks automatically, for example with
update-testing-rollback. - Check duration, locking, plans, and space on production-sized, masked data before production.
- Run fast checks on every pull request and slower ones before production.