Skip to content

Commit 63c0dc8

Browse files
lunnywxiaoguang
andauthored
Rename db Engines related functions (#17481)
* Rename db Engines related functions Co-authored-by: wxiaoguang <[email protected]>
1 parent 76a3190 commit 63c0dc8

File tree

14 files changed

+31
-31
lines changed

14 files changed

+31
-31
lines changed

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func initDBDisableConsole(disableConsole bool) error {
6565
setting.InitDBConfig()
6666

6767
setting.NewXORMLogService(disableConsole)
68-
if err := db.SetEngine(); err != nil {
68+
if err := db.InitEngine(); err != nil {
6969
return fmt.Errorf("models.SetEngine: %v", err)
7070
}
7171
return nil

cmd/doctor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func runRecreateTable(ctx *cli.Context) error {
9696
setting.Cfg.Section("log").Key("XORM").SetValue(",")
9797

9898
setting.NewXORMLogService(!ctx.Bool("debug"))
99-
if err := db.SetEngine(); err != nil {
99+
if err := db.InitEngine(); err != nil {
100100
fmt.Println(err)
101101
fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.")
102102
return nil
@@ -114,7 +114,7 @@ func runRecreateTable(ctx *cli.Context) error {
114114
}
115115
recreateTables := migrations.RecreateTables(beans...)
116116

117-
return db.NewEngine(context.Background(), func(x *xorm.Engine) error {
117+
return db.InitEngineWithMigration(context.Background(), func(x *xorm.Engine) error {
118118
if err := migrations.EnsureUpToDate(x); err != nil {
119119
return err
120120
}

cmd/dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func runDump(ctx *cli.Context) error {
173173
}
174174
setting.NewServices() // cannot access session settings otherwise
175175

176-
err := db.SetEngine()
176+
err := db.InitEngine()
177177
if err != nil {
178178
return err
179179
}

cmd/migrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func runMigrate(ctx *cli.Context) error {
3535
log.Info("Configuration file: %s", setting.CustomConf)
3636
setting.InitDBConfig()
3737

38-
if err := db.NewEngine(context.Background(), migrations.Migrate); err != nil {
38+
if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil {
3939
log.Fatal("Failed to initialize ORM engine: %v", err)
4040
return err
4141
}

cmd/migrate_storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func runMigrateStorage(ctx *cli.Context) error {
118118
log.Info("Configuration file: %s", setting.CustomConf)
119119
setting.InitDBConfig()
120120

121-
if err := db.NewEngine(context.Background(), migrations.Migrate); err != nil {
121+
if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil {
122122
log.Fatal("Failed to initialize ORM engine: %v", err)
123123
return err
124124
}

contrib/pr/checkout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func runPR() {
9595
setting.Database.LogSQL = true
9696
//x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
9797

98-
db.NewEngine(context.Background(), func(_ *xorm.Engine) error {
98+
db.InitEngineWithMigration(context.Background(), func(_ *xorm.Engine) error {
9999
return nil
100100
})
101101
db.HasEngine = true

integrations/migration-test/migration_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,21 @@ func doMigrationTest(t *testing.T, version string) {
256256

257257
setting.NewXORMLogService(false)
258258

259-
err := db.NewEngine(context.Background(), wrappedMigrate)
259+
err := db.InitEngineWithMigration(context.Background(), wrappedMigrate)
260260
assert.NoError(t, err)
261261
currentEngine.Close()
262262

263263
beans, _ := db.NamesToBean()
264264

265-
err = db.NewEngine(context.Background(), func(x *xorm.Engine) error {
265+
err = db.InitEngineWithMigration(context.Background(), func(x *xorm.Engine) error {
266266
currentEngine = x
267267
return migrations.RecreateTables(beans...)(x)
268268
})
269269
assert.NoError(t, err)
270270
currentEngine.Close()
271271

272272
// We do this a second time to ensure that there is not a problem with retained indices
273-
err = db.NewEngine(context.Background(), func(x *xorm.Engine) error {
273+
err = db.InitEngineWithMigration(context.Background(), func(x *xorm.Engine) error {
274274
currentEngine = x
275275
return migrations.RecreateTables(beans...)(x)
276276
})

models/db/engine.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ func init() {
9595
}
9696
}
9797

98-
// GetNewEngine returns a new xorm engine from the configuration
99-
func GetNewEngine() (*xorm.Engine, error) {
98+
// NewEngine returns a new xorm engine from the configuration
99+
func NewEngine() (*xorm.Engine, error) {
100100
connStr, err := setting.DBConnStr()
101101
if err != nil {
102102
return nil, err
@@ -128,11 +128,11 @@ func syncTables() error {
128128
return x.StoreEngine("InnoDB").Sync2(tables...)
129129
}
130130

131-
// NewInstallTestEngine creates a new xorm.Engine for testing during install
131+
// InitInstallEngineWithMigration creates a new xorm.Engine for testing during install
132132
//
133133
// This function will cause the basic database schema to be created
134-
func NewInstallTestEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
135-
x, err = GetNewEngine()
134+
func InitInstallEngineWithMigration(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
135+
x, err = NewEngine()
136136
if err != nil {
137137
return fmt.Errorf("failed to connect to database: %w", err)
138138
}
@@ -160,9 +160,9 @@ func NewInstallTestEngine(ctx context.Context, migrateFunc func(*xorm.Engine) er
160160
return syncTables()
161161
}
162162

163-
// SetEngine sets the xorm.Engine
164-
func SetEngine() (err error) {
165-
x, err = GetNewEngine()
163+
// InitEngine sets the xorm.Engine
164+
func InitEngine() (err error) {
165+
x, err = NewEngine()
166166
if err != nil {
167167
return fmt.Errorf("Failed to connect to database: %v", err)
168168
}
@@ -178,13 +178,13 @@ func SetEngine() (err error) {
178178
return nil
179179
}
180180

181-
// NewEngine initializes a new xorm.Engine
181+
// InitEngineWithMigration initializes a new xorm.Engine
182182
// This function must never call .Sync2() if the provided migration function fails.
183183
// When called from the "doctor" command, the migration function is a version check
184184
// that prevents the doctor from fixing anything in the database if the migration level
185185
// is different from the expected value.
186-
func NewEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
187-
if err = SetEngine(); err != nil {
186+
func InitEngineWithMigration(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
187+
if err = InitEngine(); err != nil {
188188
return err
189189
}
190190

models/migrations/migrations_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ func removeAllWithRetry(dir string) error {
8484
return err
8585
}
8686

87-
// SetEngine sets the xorm.Engine
88-
func SetEngine() (*xorm.Engine, error) {
89-
x, err := db.GetNewEngine()
87+
// newEngine sets the xorm.Engine
88+
func newEngine() (*xorm.Engine, error) {
89+
x, err := db.NewEngine()
9090
if err != nil {
9191
return x, fmt.Errorf("Failed to connect to database: %v", err)
9292
}
@@ -212,7 +212,7 @@ func prepareTestEnv(t *testing.T, skip int, syncModels ...interface{}) (*xorm.En
212212
return nil, deferFn
213213
}
214214

215-
x, err := SetEngine()
215+
x, err := newEngine()
216216
assert.NoError(t, err)
217217
if x != nil {
218218
oldDefer := deferFn

modules/doctor/dbconsistency.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func genericOrphanCheck(name, subject, refobject, joincond string) consistencyCh
7474

7575
func checkDBConsistency(logger log.Logger, autofix bool) error {
7676
// make sure DB version is uptodate
77-
if err := db.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil {
77+
if err := db.InitEngineWithMigration(context.Background(), migrations.EnsureUpToDate); err != nil {
7878
logger.Critical("Model version on the database does not match the current Gitea version. Model consistency will not be checked until the database is upgraded")
7979
return err
8080
}

0 commit comments

Comments
 (0)