Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you (Unit) Test the database schema?

When there are a number of people working on a project, all of who could alter the database schema, what's the simplest way to unit test / test / verify it? The main suggestion we've had so far is to write tests for each table to verify column names, constraints, etc.

Has anyone else done anything similar / simpler? We're using C# with SQL Server, if that makes any real difference.

Updates:

  • The segment of the project we're working on is using SSIS packages to do the bulk of the work so there is very little C# code to write unit tests agains.
  • The code for creating tables / stored procedures is spread across SQL files. Because of the build system, we could maintain a separate VS DB project file as well, but I'm not sure how that would help us verify the schema either.
like image 561
Jedidja Avatar asked Jan 13 '09 17:01

Jedidja


People also ask

Can you unit test database?

SQL unit testing is a testing method which allows us to test the smallest, atomic programmable part of a database object. SQL unit testing plays a key role in the modern database development cycle because it allows us to test individual parts of the database objects work as expected.

What is database schema testing?

What is Schema Testing? Schema Testing in database testing validates various schema formats associated with the database and verifies whether the mapping formats of tables/views/columns are compatible with mapping formats of user interface.

How do you perform a unit test in SQL?

Create a script that contains a database schema. Create a database project and import that schema. Deploy the database project to an isolated development environment. Create SQL Server unit tests.


1 Answers

That is an interesting question! There are lots of tools out there for testing stored procedures but not for testing the database schema.

Don't you find that the unit tests written for code generally find any problems with the database schema?

One approach I have used is to write stored procedures to copy test data from the developer's schema to a test schema. This is pretty rough and ready as the stored procedures generally crash when they come across any differences between the schemas but it does alert you to any changes you haven't been told about.

And nominate someone to be the DBA who monitors changes to the schema?

like image 158
James Piggot Avatar answered Sep 30 '22 13:09

James Piggot