2017-04-25 03:24:51 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2018-10-21 10:09:17 -04:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2017-04-25 03:24:51 -04:00
|
|
|
"gopkg.in/testfixtures.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var fixtures *testfixtures.Context
|
|
|
|
|
|
|
|
// InitFixtures initialize test fixtures for a test database
|
|
|
|
func InitFixtures(helper testfixtures.Helper, dir string) (err error) {
|
|
|
|
testfixtures.SkipDatabaseNameCheck(true)
|
|
|
|
fixtures, err = testfixtures.NewFolder(x.DB().DB, helper, dir)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFixtures load fixtures for a test database
|
|
|
|
func LoadFixtures() error {
|
2018-10-20 16:48:33 -04:00
|
|
|
var err error
|
|
|
|
// Database transaction conflicts could occur and result in ROLLBACK
|
2018-10-21 10:09:17 -04:00
|
|
|
// As a simple workaround, we just retry 20 times.
|
|
|
|
for i := 0; i < 20; i++ {
|
2018-10-20 16:48:33 -04:00
|
|
|
err = fixtures.Load()
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2018-10-21 10:09:17 -04:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("LoadFixtures failed after retries: %v\n", err)
|
2018-10-20 16:48:33 -04:00
|
|
|
}
|
|
|
|
return err
|
2017-04-25 03:24:51 -04:00
|
|
|
}
|