-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Improve install code to avoid low-level mistakes. #17779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
c5ff560
Improve install code to avoid low-level mistakes.
wxiaoguang 2bd7628
Check app.ini in cmd
wxiaoguang fc2404a
Fix setting i18n order
wxiaoguang 384a32a
fix
wxiaoguang 86fec99
fix
wxiaoguang 2721c0c
fix
wxiaoguang 0bf4489
fix typo
wxiaoguang 61e17ca
Update models/db/engine.go
wxiaoguang 2ae647a
Merge branch 'main' into improve-install
wxiaoguang 6c9564a
refactor database package, check SECRET_KEY to prevent overwriting
wxiaoguang 430c48e
Merge branch 'main' into improve-install
wxiaoguang 7bc1792
optimize installation check
wxiaoguang 056989d
fix rename
wxiaoguang bd8ff1e
rename generateSaveInternalToken
wxiaoguang 0f6df8d
Merge branch 'main' into improve-install
wxiaoguang 4b1ae31
Merge branch 'main' into improve-install
wxiaoguang 44d8c8e
Merge branch 'main' into improve-install
wxiaoguang a0a438d
Add logs
wxiaoguang ce8846e
Remove unnecessary db quote
wxiaoguang 89c37b6
fix log
wxiaoguang 4378c90
Merge branch 'main' into improve-install
wxiaoguang 15e7bab
Merge remote-tracking branch 'origin/main' into improve-install
zeripath 61fb4d9
Adjust install page messages and appearances
zeripath c1235fa
Add testing for empty app.ini on setting.NewContext and only allow we…
zeripath edc5ed0
Merge pull request #2 from zeripath/improve-install
wxiaoguang f15d610
Improve setting.NewContext
wxiaoguang 8f4a4ec
Merge branch 'main' into improve-install
wxiaoguang fbaf635
Only create APP_DATA_PATH if necessary
wxiaoguang c75e9e7
Merge branch 'improve-install' of github.com:wxiaoguang/gitea into im…
wxiaoguang 4fb54ff
fix lint
wxiaoguang bef7edd
Fix NewContext call
wxiaoguang 7fe5264
Fix unit test
wxiaoguang 1bcd824
fix unit test
wxiaoguang 13eaef5
Fix unit test
wxiaoguang acda572
test
wxiaoguang 47dded1
fix test
wxiaoguang 007a7d4
Update modules/setting/setting.go
wxiaoguang bc36dc5
Update modules/setting/directory.go
wxiaoguang 815d8f9
Update modules/setting/directory.go
wxiaoguang 3c240c5
Update cmd/web.go
wxiaoguang 17b98f9
Update contrib/environment-to-ini/environment-to-ini.go
wxiaoguang cedb452
Update models/db/engine.go
wxiaoguang fc0b0d3
Update modules/private/internal.go
wxiaoguang d95d1db
Update modules/setting/directory.go
wxiaoguang 8dbbc10
Rename setting.NewContextXxx to setting.LoadXxx
wxiaoguang eb6bb8e
Merge branch 'main' into improve-install
wxiaoguang 30965a6
Merge branch 'main' into improve-install
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2021 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 install | ||
|
||
import ( | ||
"code.gitea.io/gitea/models/db" | ||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
func getXORMEngine() *xorm.Engine { | ||
return db.DefaultContext.(*db.Context).Engine().(*xorm.Engine) | ||
} | ||
|
||
// CheckDatabaseConnection checks the database connection | ||
func CheckDatabaseConnection() error { | ||
e := db.GetEngine(db.DefaultContext) | ||
_, err := e.Exec("SELECT 1") | ||
return err | ||
} | ||
|
||
// GetMigrationVersion gets the database migration version | ||
func GetMigrationVersion() (int64, error) { | ||
var installedDbVersion int64 | ||
x := getXORMEngine() | ||
exist, err := x.IsTableExist("version") | ||
if err != nil { | ||
return 0, err | ||
} | ||
if !exist { | ||
return 0, nil | ||
} | ||
_, err = x.Table("version").Cols("version").Get(&installedDbVersion) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return installedDbVersion, nil | ||
} | ||
|
||
// HasPostInstallationUsers checks whether there are users after installation | ||
func HasPostInstallationUsers() (bool, error) { | ||
x := getXORMEngine() | ||
exist, err := x.IsTableExist("user") | ||
if err != nil { | ||
return false, err | ||
} | ||
if !exist { | ||
return false, nil | ||
} | ||
|
||
// if there are 2 or more users in database, we consider there are users created after installation | ||
threshold := 2 | ||
if !setting.IsProd { | ||
// to debug easily, with non-prod RUN_MODE, we only check the count to 1 | ||
threshold = 1 | ||
} | ||
res, err := x.Table("user").Cols("id").Limit(threshold).Query() | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return false, err | ||
} | ||
return len(res) >= threshold, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.