Skip to content

sql: Pre-fill database with data before tests/migrations run

Pavlo Strokov requested to merge ps-enhanced-sql-testing into master

To get better tests we should run test scenarios on top of pre-populated database. It will give us more confidence about our queries as well as highlight any hidden conditions as triggers execution during run of the migration scripts, etc.

The population of the database is done via a migration script that is injected into a common migration set during template database creation. It inserts data into storage_repositories and repositories tables at the moment as we had problems with running migrations on them because of the triggers. The set could be extended as well. We also could add additional code checks to verify migrations were applied as expected.

And as not we have pre-existing data we don't want to remove we change the way we do the testing. Now we rely on the transactions to run the tests and get into initial database state once the test is completed because of transaction rollback. If it is not possible to use a transaction (nested transactions are not supported) we remove any created artefacts after test completion.

Because the sequence of the repository_id column is dynamic now we change repository_id hardcoded values to the value returned by the ReserveRepositoryID method for particular virtual storage and relative path as we can't predict auto-generated values for the column anymore.

Because of those changes we don't need TruncateAll method anymore as we don't plan to clean up the database and should preserve initial state of it for the tests.
The Truncate method doesn't reset table sequences anymore as it would result to attempt to insert not unique values into the table.

Part of: #3820 (closed)

Merge request reports