1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-07-25 12:39:40 +02:00

chore: do not require empty fixtures to clean tables (#8353)

- A mistake that is often made is to not put a empty fixture for tables that gets populated in a test and should be cleaned up between runs. The CI does not detect such issues as these never arise on the first run of the test suite and are only noticed when developers run a test for a second time, unless you are aware of this behavior (which very few do as this is not documented and pure folk knowledge) can end up in chasing a bug that does not exist for hours. forgejo/forgejo#7041, forgejo/forgejo#7419, meissa/forgejo#21, forgejo/forgejo#5771
- Because we now own the fixture loader (forgejo/forgejo#7715), its rather simple to ensure that all tables that did not receive fixtures should be cleaned between runs and removes the need to place empty fixture files.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8353
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Reviewed-by: Otto <otto@codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-06-30 23:04:16 +02:00 committed by Gusted
parent 6e58d285c7
commit 0ecb25fdcb
23 changed files with 96 additions and 21 deletions

View file

@ -12,6 +12,8 @@ import (
"path/filepath"
"strings"
"forgejo.org/modules/container"
"gopkg.in/yaml.v3"
)
@ -32,13 +34,15 @@ type loader struct {
fixtureFiles []*fixtureFile
}
func newFixtureLoader(db *sql.DB, dialect string, fixturePaths []string) (*loader, error) {
func newFixtureLoader(db *sql.DB, dialect string, fixturePaths []string, allTableNames container.Set[string]) (*loader, error) {
l := &loader{
db: db,
dialect: dialect,
fixtureFiles: []*fixtureFile{},
}
tablesWithoutFixture := allTableNames
// Load fixtures
for _, fixturePath := range fixturePaths {
stat, err := os.Stat(fixturePath)
@ -60,6 +64,7 @@ func newFixtureLoader(db *sql.DB, dialect string, fixturePaths []string) (*loade
return nil, err
}
l.fixtureFiles = append(l.fixtureFiles, fixtureFile)
tablesWithoutFixture.Remove(fixtureFile.name)
}
}
} else {
@ -71,6 +76,14 @@ func newFixtureLoader(db *sql.DB, dialect string, fixturePaths []string) (*loade
}
}
// Even though these tables have no fixtures, they can still be used and ensure
// they are cleaned.
for table := range tablesWithoutFixture.Seq() {
l.fixtureFiles = append(l.fixtureFiles, &fixtureFile{
name: table,
})
}
return l, nil
}
@ -178,13 +191,13 @@ func (l *loader) Load() error {
}()
// Clean the table and re-insert the fixtures.
tableDeleted := map[string]struct{}{}
tableDeleted := make(container.Set[string])
for _, fixture := range l.fixtureFiles {
if _, ok := tableDeleted[fixture.name]; !ok {
if !tableDeleted.Contains(fixture.name) {
if _, err := tx.Exec(fmt.Sprintf("DELETE FROM %s", l.quoteKeyword(fixture.name))); err != nil {
return fmt.Errorf("cannot delete table %s: %w", fixture.name, err)
}
tableDeleted[fixture.name] = struct{}{}
tableDeleted.Add(fixture.name)
}
for _, insertSQL := range fixture.insertSQLs {