mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-10 06:53:25 +09:00
test: release fixtures loader lock before database work (#39263)
This commit is contained in:
@@ -34,8 +34,7 @@ type FixtureItem struct {
|
||||
|
||||
type fixturesLoaderInternal struct {
|
||||
xormEngine *xorm.Engine
|
||||
tableSyncMu sync.Mutex
|
||||
tableSynced map[string]bool
|
||||
tableSyncMap sync.Map
|
||||
db *sql.DB
|
||||
dbType schemas.DBType
|
||||
fixtures map[string]*FixtureItem
|
||||
@@ -153,35 +152,33 @@ func (f *fixturesLoaderInternal) Load() error {
|
||||
|
||||
ctx := context.WithValue(context.Background(), db.ContextKeyTestFixtures, true)
|
||||
|
||||
f.tableSyncMu.Lock()
|
||||
defer f.tableSyncMu.Unlock()
|
||||
|
||||
for _, fixture := range f.fixtures {
|
||||
synced, existing := f.tableSynced[fixture.tableName]
|
||||
if synced || !existing {
|
||||
synced, existing := f.tableSyncMap.Load(fixture.tableName)
|
||||
if synced == true || !existing {
|
||||
continue
|
||||
}
|
||||
if err := f.loadFixtures(tx, fixture); err != nil {
|
||||
return fmt.Errorf("failed to load fixtures from %s: %w", fixture.fileFullPath, err)
|
||||
}
|
||||
f.tableSynced[fixture.tableName] = true
|
||||
f.tableSyncMap.Store(fixture.tableName, true)
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
for tableName, synced := range f.tableSynced {
|
||||
f.tableSyncMap.Range(func(k, v any) bool {
|
||||
tableName, _ := k.(string)
|
||||
synced, _ := v.(bool)
|
||||
if !synced && f.fixtures[tableName] == nil {
|
||||
_, _ = f.xormEngine.Context(ctx).Exec("DELETE FROM `" + tableName + "`")
|
||||
}
|
||||
f.tableSynced[tableName] = true
|
||||
}
|
||||
f.tableSyncMap.Store(tableName, true)
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fixturesLoaderInternal) MarkTableChanged(tableName string) {
|
||||
f.tableSyncMu.Lock()
|
||||
defer f.tableSyncMu.Unlock()
|
||||
f.tableSynced[tableName] = false
|
||||
f.tableSyncMap.Store(tableName, false)
|
||||
}
|
||||
|
||||
func FixturesFileFullPaths(dir string, files []string) (map[string]*FixtureItem, error) {
|
||||
@@ -216,7 +213,7 @@ func NewFixturesLoader(x *xorm.Engine, opts FixturesOptions) (FixturesLoader, er
|
||||
return nil, fmt.Errorf("failed to get fixtures files: %w", err)
|
||||
}
|
||||
|
||||
f := &fixturesLoaderInternal{xormEngine: x, db: x.DB().DB, dbType: x.Dialect().URI().DBType, fixtures: fixtureItems, tableSynced: map[string]bool{}}
|
||||
f := &fixturesLoaderInternal{xormEngine: x, db: x.DB().DB, dbType: x.Dialect().URI().DBType, fixtures: fixtureItems}
|
||||
switch f.dbType {
|
||||
case schemas.SQLITE:
|
||||
f.quoteObject = func(s string) string { return fmt.Sprintf(`"%s"`, s) }
|
||||
@@ -237,7 +234,7 @@ func NewFixturesLoader(x *xorm.Engine, opts FixturesOptions) (FixturesLoader, er
|
||||
xormBeans, _ := db.NamesToBean()
|
||||
for _, bean := range xormBeans {
|
||||
beanTableName := x.TableName(bean)
|
||||
f.tableSynced[trimTableNameQuotes(beanTableName)] = false
|
||||
f.tableSyncMap.Store(trimTableNameQuotes(beanTableName), false)
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user