Improve testing: Truncate all tables but migrations - #128
Conversation
| } | ||
| for _, table := range tableNames { | ||
| if err := g2.Exec(fmt.Sprintf("TRUNCATE TABLE %s CASCADE", table)).Error; err != nil { | ||
| glog.Errorf("Error truncating table %s: %s", table, err) |
There was a problem hiding this comment.
Truncating all tables by query (instead of a hard-coded list of table names) is a good improvement, but I wonder if it's needed at all?
The improved way of resetting between tests is to dump the database entirely and recreate it from a template. The template (template1, part of default postgres) gets all the migrations applied once, and then new DBs are restored from that template.
https://github.com/openshift-online/rh-trex/blob/main/pkg/db/db_session/test.go#L50
I think we might be able to remove table truncation entirely.
There was a problem hiding this comment.
Then, that mechanism is not properly working in the current tests, since it failed for me.
I will revisit this.
When using test containers, it also allows us to run tests in parallel.
Than means, one PostgreSQL instance per test
Or something in between, having a subset of tests sharing the same instance
There was a problem hiding this comment.
Test containers are great. Restoring all those postgres instances from template1 is a significant performance improvement, especially when they number of migrations grows over time.
There was a problem hiding this comment.
That is why I mentioned the "something in between"
Here is a somewhat crazy idea
Generate a testcontainer image with a PostgreSQL already migrated
After that, spinning up a new PostgreSQL is the same as creating instances from template1
And then we can use some parallelization for tests
There was a problem hiding this comment.
Yes that sounds good, too.
This PR introduces an script for testing automatically:
Why an script and not an integration tests?
Truncating tables
When generating a new entity with the
generator.go, the filepkg/db/db_session/testcontainer.godoesn't get updated.This makes the tests fail since the tables are not cleaned after each test.
Since this ResetDB is only for testscontainers, it is safe to truncate all the tables but the
migrationsone.This will allow us to auto