From eb3ec0fe6f1139e6e23858245ff27bbb98125e12 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Wed, 23 Feb 2022 20:33:30 +0000 Subject: [PATCH 01/11] Converts columns from NULL to NOT NULL DEFAULT. --- models/issue.go | 12 +- models/issue_label.go | 6 +- models/issue_milestone.go | 12 +- models/migrations/migrations.go | 2 + models/migrations/v211.go | 248 ++++++++++++++++++++++++++++++++ models/org_team.go | 12 +- models/repo/attachment.go | 12 +- models/repo/repo.go | 26 ++-- models/repo/topic.go | 6 +- models/user/user.go | 22 +-- 10 files changed, 304 insertions(+), 54 deletions(-) create mode 100644 models/migrations/v211.go diff --git a/models/issue.go b/models/issue.go index 91d4df32d1cd8..ffc20aba66857 100644 --- a/models/issue.go +++ b/models/issue.go @@ -38,25 +38,25 @@ type Issue struct { RepoID int64 `xorm:"INDEX UNIQUE(repo_index)"` Repo *repo_model.Repository `xorm:"-"` Index int64 `xorm:"UNIQUE(repo_index)"` // Index in one repository. - PosterID int64 `xorm:"INDEX"` + PosterID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` Poster *user_model.User `xorm:"-"` OriginalAuthor string - OriginalAuthorID int64 `xorm:"index"` + OriginalAuthorID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` Title string `xorm:"name"` Content string `xorm:"LONGTEXT"` RenderedContent string `xorm:"-"` Labels []*Label `xorm:"-"` - MilestoneID int64 `xorm:"INDEX"` + MilestoneID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` Milestone *Milestone `xorm:"-"` Project *Project `xorm:"-"` Priority int AssigneeID int64 `xorm:"-"` Assignee *user_model.User `xorm:"-"` - IsClosed bool `xorm:"INDEX"` + IsClosed bool `xorm:"INDEX NOT NULL DEFAULT false"` IsRead bool `xorm:"-"` - IsPull bool `xorm:"INDEX"` // Indicates whether is a pull request or not. + IsPull bool `xorm:"INDEX NOT NULL DEFAULT false"` // Indicates whether is a pull request or not. PullRequest *PullRequest `xorm:"-"` - NumComments int + NumComments int `xorm:"NOT NULL DEFAULT 0"` Ref string DeadlineUnix timeutil.TimeStamp `xorm:"INDEX"` diff --git a/models/issue_label.go b/models/issue_label.go index 0aea620773791..e721ca7b6e52b 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -31,9 +31,9 @@ type Label struct { OrgID int64 `xorm:"INDEX"` Name string Description string - Color string `xorm:"VARCHAR(7)"` - NumIssues int - NumClosedIssues int + Color string `xorm:"VARCHAR(7)"` + NumIssues int `xorm:"NOT NULL DEFAULT 0"` + NumClosedIssues int `xorm:"NOT NULL DEFAULT 0"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` diff --git a/models/issue_milestone.go b/models/issue_milestone.go index a3217185134e4..45330f54d5240 100644 --- a/models/issue_milestone.go +++ b/models/issue_milestone.go @@ -28,12 +28,12 @@ type Milestone struct { Name string Content string `xorm:"TEXT"` RenderedContent string `xorm:"-"` - IsClosed bool - NumIssues int - NumClosedIssues int - NumOpenIssues int `xorm:"-"` - Completeness int // Percentage(1-100). - IsOverdue bool `xorm:"-"` + IsClosed bool `xorm:"NOT NULL DEFAULT false"` + NumIssues int `xorm:"NOT NULL DEFAULT 0"` + NumClosedIssues int `xorm:"NOT NULL DEFAULT 0"` + NumOpenIssues int `xorm:"-"` + Completeness int `xorm:"NOT NULL DEFAULT 0"` // Percentage(1-100). + IsOverdue bool `xorm:"-"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 63d1c32259964..732ac6f65f114 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -374,6 +374,8 @@ var migrations = []Migration{ NewMigration("Increase WebAuthentication CredentialID size to 410 - NO-OPED", increaseCredentialIDTo410), // v210 -> v211 NewMigration("v208 was completely broken - remigrate", remigrateU2FCredentials), + // v211 -> v212 + NewMigration("Convert columns from NULL to NOT NULL DEFAULT", convertFromNullToDefault), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v211.go b/models/migrations/v211.go new file mode 100644 index 0000000000000..0b7c2c4c96895 --- /dev/null +++ b/models/migrations/v211.go @@ -0,0 +1,248 @@ +// Copyright 2022 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 migrations + +import ( + "code.gitea.io/gitea/models/auth" + "code.gitea.io/gitea/models/perm" + "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/user" + api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/timeutil" + + "xorm.io/xorm" +) + +func convertFromNullToDefault(x *xorm.Engine) error { + sess := x.NewSession() + defer sess.Close() + if err := sess.Begin(); err != nil { + return err + } + + type Label struct { + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"INDEX"` + OrgID int64 `xorm:"INDEX"` + Name string + Description string + Color string `xorm:"VARCHAR(7)"` + NumIssues int `xorm:"NOT NULL DEFAULT 0"` + NumClosedIssues int `xorm:"NOT NULL DEFAULT 0"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` + UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` + } + recreateTable(sess, &Label{}) + + type Milestone struct { + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"INDEX"` + Name string + Content string `xorm:"TEXT"` + IsClosed bool `xorm:"NOT NULL DEFAULT false"` + NumIssues int `xorm:"NOT NULL DEFAULT 0"` + NumClosedIssues int `xorm:"NOT NULL DEFAULT 0"` + Completeness int `xorm:"NOT NULL DEFAULT 0"` // Percentage(1-100). + + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` + UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` + DeadlineUnix timeutil.TimeStamp + ClosedDateUnix timeutil.TimeStamp + } + recreateTable(sess, &Milestone{}) + + type Issue struct { + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"INDEX UNIQUE(repo_index)"` + Index int64 `xorm:"UNIQUE(repo_index)"` // Index in one repository. + PosterID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` + OriginalAuthor string + OriginalAuthorID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` + Title string `xorm:"name"` + Content string `xorm:"LONGTEXT"` + MilestoneID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` + Priority int + IsClosed bool `xorm:"INDEX NOT NULL DEFAULT false"` + IsPull bool `xorm:"INDEX NOT NULL DEFAULT false"` // Indicates whether is a pull request or not. + NumComments int `xorm:"NOT NULL DEFAULT 0"` + Ref string + + DeadlineUnix timeutil.TimeStamp `xorm:"INDEX"` + + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` + UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` + ClosedUnix timeutil.TimeStamp `xorm:"INDEX"` + + // IsLocked limits commenting abilities to users on an issue + // with write access + IsLocked bool `xorm:"NOT NULL DEFAULT false"` + } + recreateTable(sess, &Issue{}) + + type Team struct { + ID int64 `xorm:"pk autoincr"` + OrgID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` + LowerName string + Name string + Description string + AccessMode perm.AccessMode `xorm:"'authorize'"` + NumRepos int `xorm:"NOT NULL DEFAULT 0"` + NumMembers int `xorm:"NOT NULL DEFAULT 0"` + IncludesAllRepositories bool `xorm:"NOT NULL DEFAULT false"` + CanCreateOrgRepo bool `xorm:"NOT NULL DEFAULT false"` + } + recreateTable(sess, &Team{}) + + type Attachment struct { + ID int64 `xorm:"pk autoincr"` + UUID string `xorm:"uuid UNIQUE"` + RepoID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // this should not be zero + IssueID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // maybe zero when creating + ReleaseID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // maybe zero when creating + UploaderID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // Notice: will be zero before this column added + CommentID int64 + Name string + DownloadCount int64 `xorm:"NOT NULL DEFAULT 0"` + Size int64 `xorm:"NOT NULL DEFAULT 0"` + CreatedUnix timeutil.TimeStamp `xorm:"created"` + } + recreateTable(sess, &Attachment{}) + + type Repository struct { + ID int64 `xorm:"pk autoincr"` + OwnerID int64 `xorm:"UNIQUE(s) index"` + OwnerName string + LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"` + Name string `xorm:"INDEX NOT NULL"` + Description string `xorm:"TEXT"` + Website string `xorm:"VARCHAR(2048)"` + OriginalServiceType api.GitServiceType `xorm:"index"` + OriginalURL string `xorm:"VARCHAR(2048)"` + DefaultBranch string + + NumWatches int `xorm:"NOT NULL DEFAULT 0"` + NumStars int `xorm:"NOT NULL DEFAULT 0"` + NumForks int `xorm:"NOT NULL DEFAULT 0"` + NumIssues int `xorm:"NOT NULL DEFAULT 0"` + NumClosedIssues int `xorm:"NOT NULL DEFAULT 0"` + NumPulls int `xorm:"NOT NULL DEFAULT 0"` + NumClosedPulls int `xorm:"NOT NULL DEFAULT 0"` + NumMilestones int `xorm:"NOT NULL DEFAULT 0"` + NumClosedMilestones int `xorm:"NOT NULL DEFAULT 0"` + NumProjects int `xorm:"NOT NULL DEFAULT 0"` + NumClosedProjects int `xorm:"NOT NULL DEFAULT 0"` + + IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"` + IsEmpty bool `xorm:"INDEX NOT NULL DEFAULT true"` + IsArchived bool `xorm:"INDEX NOT NULL DEFAULT false"` + IsMirror bool `xorm:"INDEX NOT NULL DEFAULT false"` + Status repo.RepositoryStatus `xorm:"NOT NULL DEFAULT 0"` + + IsFork bool `xorm:"INDEX NOT NULL DEFAULT false"` + ForkID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` + IsTemplate bool `xorm:"INDEX NOT NULL DEFAULT false"` + TemplateID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` + Size int64 `xorm:"NOT NULL DEFAULT 0"` + IsFsckEnabled bool `xorm:"NOT NULL DEFAULT true"` + CloseIssuesViaCommitInAnyBranch bool `xorm:"NOT NULL DEFAULT false"` + Topics []string `xorm:"TEXT JSON"` + + TrustModel repo.TrustModelType + + // Avatar: ID(10-20)-md5(32) - must fit into 64 symbols + Avatar string `xorm:"VARCHAR(64)"` + + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` + UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` + } + recreateTable(sess, &Repository{}) + + type Topic struct { + ID int64 `xorm:"pk autoincr"` + Name string `xorm:"UNIQUE VARCHAR(50)"` + RepoCount int `xorm:"NOT NULL DEFAULT 0"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` + UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` + } + recreateTable(sess, &Topic{}) + + type User struct { + ID int64 `xorm:"pk autoincr"` + LowerName string `xorm:"UNIQUE NOT NULL"` + Name string `xorm:"UNIQUE NOT NULL"` + FullName string + // Email is the primary email address (to be used for communication) + Email string `xorm:"NOT NULL"` + KeepEmailPrivate bool + EmailNotificationsPreference string `xorm:"VARCHAR(20) NOT NULL DEFAULT 'enabled'"` + Passwd string `xorm:"NOT NULL"` + PasswdHashAlgo string `xorm:"NOT NULL DEFAULT 'argon2'"` + + // MustChangePassword is an attribute that determines if a user + // is to change his/her password after registration. + MustChangePassword bool `xorm:"NOT NULL DEFAULT false"` + + LoginType auth.Type + LoginSource int64 `xorm:"NOT NULL DEFAULT 0"` + LoginName string + Type user.UserType + Location string + Website string + Rands string `xorm:"VARCHAR(32)"` + Salt string `xorm:"VARCHAR(32)"` + Language string `xorm:"VARCHAR(5)"` + Description string + + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` + UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` + LastLoginUnix timeutil.TimeStamp `xorm:"INDEX"` + + // Remember visibility choice for convenience, true for private + LastRepoVisibility bool + // Maximum repository creation limit, -1 means use global default + MaxRepoCreation int `xorm:"NOT NULL DEFAULT -1"` + + // IsActive true: primary email is activated, user can access Web UI and Git SSH. + // false: an inactive user can only log in Web UI for account operations (ex: activate the account by email), no other access. + IsActive bool `xorm:"INDEX NOT NULL DEFAULT false"` + // the user is a Gitea admin, who can access all repositories and the admin pages. + IsAdmin bool `xorm:"NOT NULL DEFAULT false"` + // true: the user is only allowed to see organizations/repositories that they has explicit rights to. + // (ex: in private Gitea instances user won't be allowed to see even organizations/repositories that are set as public) + IsRestricted bool `xorm:"NOT NULL DEFAULT false"` + + AllowGitHook bool `xorm:"NOT NULL DEFAULT false"` + AllowImportLocal bool `xorm:"NOT NULL DEFAULT false"` // Allow migrate repository by local path + AllowCreateOrganization bool `xorm:"NOT NULL DEFAULT true"` + + // true: the user is not allowed to log in Web UI. Git/SSH access could still be allowed (please refer to Git/SSH access related code/documents) + ProhibitLogin bool `xorm:"NOT NULL DEFAULT false"` + + // Avatar + Avatar string `xorm:"VARCHAR(2048) NOT NULL"` + AvatarEmail string `xorm:"NOT NULL"` + UseCustomAvatar bool `xorm:"NOT NULL DEFAULT false"` + + // Counters + NumFollowers int `xorm:"NOT NULL DEFAULT 0"` + NumFollowing int `xorm:"NOT NULL DEFAULT 0"` + NumStars int `xorm:"NOT NULL DEFAULT 0"` + NumRepos int `xorm:"NOT NULL DEFAULT 0"` + + // For organization + NumTeams int `xorm:"NOT NULL DEFAULT 0"` + NumMembers int `xorm:"NOT NULL DEFAULT 0"` + Visibility api.VisibleType `xorm:"NOT NULL DEFAULT 0"` + RepoAdminChangeTeamAccess bool `xorm:"NOT NULL DEFAULT false"` + + // Preferences + DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"` + Theme string `xorm:"NOT NULL DEFAULT ''"` + KeepActivityPrivate bool `xorm:"NOT NULL DEFAULT false"` + } + recreateTable(sess, &User{}) + + return sess.Commit() +} diff --git a/models/org_team.go b/models/org_team.go index 17f95bb5b08be..159f3409fba0f 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -28,18 +28,18 @@ const ownerTeamName = "Owners" // Team represents a organization team. type Team struct { ID int64 `xorm:"pk autoincr"` - OrgID int64 `xorm:"INDEX"` + OrgID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` LowerName string Name string Description string AccessMode perm.AccessMode `xorm:"'authorize'"` Repos []*repo_model.Repository `xorm:"-"` Members []*user_model.User `xorm:"-"` - NumRepos int - NumMembers int - Units []*TeamUnit `xorm:"-"` - IncludesAllRepositories bool `xorm:"NOT NULL DEFAULT false"` - CanCreateOrgRepo bool `xorm:"NOT NULL DEFAULT false"` + NumRepos int `xorm:"NOT NULL DEFAULT 0"` + NumMembers int `xorm:"NOT NULL DEFAULT 0"` + Units []*TeamUnit `xorm:"-"` + IncludesAllRepositories bool `xorm:"NOT NULL DEFAULT false"` + CanCreateOrgRepo bool `xorm:"NOT NULL DEFAULT false"` } func init() { diff --git a/models/repo/attachment.go b/models/repo/attachment.go index f5351578fbc5c..9e88cc8db904d 100644 --- a/models/repo/attachment.go +++ b/models/repo/attachment.go @@ -20,14 +20,14 @@ import ( type Attachment struct { ID int64 `xorm:"pk autoincr"` UUID string `xorm:"uuid UNIQUE"` - RepoID int64 `xorm:"INDEX"` // this should not be zero - IssueID int64 `xorm:"INDEX"` // maybe zero when creating - ReleaseID int64 `xorm:"INDEX"` // maybe zero when creating - UploaderID int64 `xorm:"INDEX DEFAULT 0"` // Notice: will be zero before this column added + RepoID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // this should not be zero + IssueID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // maybe zero when creating + ReleaseID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // maybe zero when creating + UploaderID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // Notice: will be zero before this column added CommentID int64 Name string - DownloadCount int64 `xorm:"DEFAULT 0"` - Size int64 `xorm:"DEFAULT 0"` + DownloadCount int64 `xorm:"NOT NULL DEFAULT 0"` + Size int64 `xorm:"NOT NULL DEFAULT 0"` CreatedUnix timeutil.TimeStamp `xorm:"created"` } diff --git a/models/repo/repo.go b/models/repo/repo.go index 28d976773df43..262961e578838 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -105,14 +105,14 @@ type Repository struct { OriginalURL string `xorm:"VARCHAR(2048)"` DefaultBranch string - NumWatches int - NumStars int - NumForks int - NumIssues int - NumClosedIssues int + NumWatches int `xorm:"NOT NULL DEFAULT 0"` + NumStars int `xorm:"NOT NULL DEFAULT 0"` + NumForks int `xorm:"NOT NULL DEFAULT 0"` + NumIssues int `xorm:"NOT NULL DEFAULT 0"` + NumClosedIssues int `xorm:"NOT NULL DEFAULT 0"` NumOpenIssues int `xorm:"-"` - NumPulls int - NumClosedPulls int + NumPulls int `xorm:"NOT NULL DEFAULT 0"` + NumClosedPulls int `xorm:"NOT NULL DEFAULT 0"` NumOpenPulls int `xorm:"-"` NumMilestones int `xorm:"NOT NULL DEFAULT 0"` NumClosedMilestones int `xorm:"NOT NULL DEFAULT 0"` @@ -121,10 +121,10 @@ type Repository struct { NumClosedProjects int `xorm:"NOT NULL DEFAULT 0"` NumOpenProjects int `xorm:"-"` - IsPrivate bool `xorm:"INDEX"` - IsEmpty bool `xorm:"INDEX"` - IsArchived bool `xorm:"INDEX"` - IsMirror bool `xorm:"INDEX"` + IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"` + IsEmpty bool `xorm:"INDEX NOT NULL DEFAULT true"` + IsArchived bool `xorm:"INDEX NOT NULL DEFAULT false"` + IsMirror bool `xorm:"INDEX NOT NULL DEFAULT false"` *Mirror `xorm:"-"` Status RepositoryStatus `xorm:"NOT NULL DEFAULT 0"` @@ -134,10 +134,10 @@ type Repository struct { PrimaryLanguage *LanguageStat `xorm:"-"` IsFork bool `xorm:"INDEX NOT NULL DEFAULT false"` - ForkID int64 `xorm:"INDEX"` + ForkID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` BaseRepo *Repository `xorm:"-"` IsTemplate bool `xorm:"INDEX NOT NULL DEFAULT false"` - TemplateID int64 `xorm:"INDEX"` + TemplateID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` Size int64 `xorm:"NOT NULL DEFAULT 0"` CodeIndexerStatus *RepoIndexerStatus `xorm:"-"` StatsIndexerStatus *RepoIndexerStatus `xorm:"-"` diff --git a/models/repo/topic.go b/models/repo/topic.go index 121863519bb47..3c84b566bcfb3 100644 --- a/models/repo/topic.go +++ b/models/repo/topic.go @@ -25,9 +25,9 @@ var topicPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*$`) // Topic represents a topic of repositories type Topic struct { - ID int64 `xorm:"pk autoincr"` - Name string `xorm:"UNIQUE VARCHAR(50)"` - RepoCount int + ID int64 `xorm:"pk autoincr"` + Name string `xorm:"UNIQUE VARCHAR(50)"` + RepoCount int `xorm:"NOT NULL DEFAULT 0"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } diff --git a/models/user/user.go b/models/user/user.go index b61ffd707248e..eb25d3d5e546e 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -111,16 +111,16 @@ type User struct { // IsActive true: primary email is activated, user can access Web UI and Git SSH. // false: an inactive user can only log in Web UI for account operations (ex: activate the account by email), no other access. - IsActive bool `xorm:"INDEX"` + IsActive bool `xorm:"INDEX NOT NULL DEFAULT false"` // the user is a Gitea admin, who can access all repositories and the admin pages. - IsAdmin bool + IsAdmin bool `xorm:"NOT NULL DEFAULT false"` // true: the user is only allowed to see organizations/repositories that they has explicit rights to. // (ex: in private Gitea instances user won't be allowed to see even organizations/repositories that are set as public) IsRestricted bool `xorm:"NOT NULL DEFAULT false"` - AllowGitHook bool - AllowImportLocal bool // Allow migrate repository by local path - AllowCreateOrganization bool `xorm:"DEFAULT true"` + AllowGitHook bool `xorm:"NOT NULL DEFAULT false"` + AllowImportLocal bool `xorm:"NOT NULL DEFAULT false"` // Allow migrate repository by local path + AllowCreateOrganization bool `xorm:"NOT NULL DEFAULT true"` // true: the user is not allowed to log in Web UI. Git/SSH access could still be allowed (please refer to Git/SSH access related code/documents) ProhibitLogin bool `xorm:"NOT NULL DEFAULT false"` @@ -128,17 +128,17 @@ type User struct { // Avatar Avatar string `xorm:"VARCHAR(2048) NOT NULL"` AvatarEmail string `xorm:"NOT NULL"` - UseCustomAvatar bool + UseCustomAvatar bool `xorm:"NOT NULL DEFAULT false"` // Counters - NumFollowers int + NumFollowers int `xorm:"NOT NULL DEFAULT 0"` NumFollowing int `xorm:"NOT NULL DEFAULT 0"` - NumStars int - NumRepos int + NumStars int `xorm:"NOT NULL DEFAULT 0"` + NumRepos int `xorm:"NOT NULL DEFAULT 0"` // For organization - NumTeams int - NumMembers int + NumTeams int `xorm:"NOT NULL DEFAULT 0"` + NumMembers int `xorm:"NOT NULL DEFAULT 0"` Visibility structs.VisibleType `xorm:"NOT NULL DEFAULT 0"` RepoAdminChangeTeamAccess bool `xorm:"NOT NULL DEFAULT false"` From b8dd4f219500012ada47273b480374e38005b234 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Wed, 23 Feb 2022 22:38:23 +0000 Subject: [PATCH 02/11] Fixed unit tests. --- models/fixtures/repository.yml | 1 + models/fixtures/user.yml | 1 + models/user/user_test.go | 2 +- routers/web/user/home_test.go | 2 +- 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/models/fixtures/repository.yml b/models/fixtures/repository.yml index dc8ebecfc5c99..33479c633c2fc 100644 --- a/models/fixtures/repository.yml +++ b/models/fixtures/repository.yml @@ -40,6 +40,7 @@ owner_name: user3 lower_name: repo3 name: repo3 + is_archived: false is_private: true num_issues: 1 num_closed_issues: 0 diff --git a/models/fixtures/user.yml b/models/fixtures/user.yml index cf07542eed16c..f21aac92d3033 100644 --- a/models/fixtures/user.yml +++ b/models/fixtures/user.yml @@ -470,6 +470,7 @@ avatar: avatar27 avatar_email: user27@example.com num_repos: 3 + is_active: true - id: 28 diff --git a/models/user/user_test.go b/models/user/user_test.go index a5f47172eebc0..4f6077db043cd 100644 --- a/models/user/user_test.go +++ b/models/user/user_test.go @@ -101,7 +101,7 @@ func TestSearchUsers(t *testing.T) { []int64{9}) testUserSuccess(&SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, - []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 28, 29, 30, 32}) + []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32}) testUserSuccess(&SearchUserOptions{Keyword: "user1", OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, []int64{1, 10, 11, 12, 13, 14, 15, 16, 18}) diff --git a/routers/web/user/home_test.go b/routers/web/user/home_test.go index cd599abd047e7..9e476e8f6ace9 100644 --- a/routers/web/user/home_test.go +++ b/routers/web/user/home_test.go @@ -76,7 +76,7 @@ func TestPulls(t *testing.T) { Pulls(ctx) assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) - assert.Len(t, ctx.Data["Issues"], 3) + assert.Len(t, ctx.Data["Issues"], 4) } func TestMilestones(t *testing.T) { From 40bb73bdd45613c32b86f23f29f80c0e52d78b21 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Tue, 20 Sep 2022 10:36:16 +0000 Subject: [PATCH 03/11] Add more NOT NULLs. --- models/auth/webauthn.go | 2 +- models/issues/issue.go | 2 +- models/migrations/v226.go | 29 ++++++++++++++++++++++++++--- models/repo/attachment.go | 2 +- models/user/badge.go | 4 ++-- models/user/user.go | 2 +- 6 files changed, 32 insertions(+), 9 deletions(-) diff --git a/models/auth/webauthn.go b/models/auth/webauthn.go index d3062342f545b..90905df3c6d89 100644 --- a/models/auth/webauthn.go +++ b/models/auth/webauthn.go @@ -47,7 +47,7 @@ type WebAuthnCredential struct { AttestationType string AAGUID []byte SignCount uint32 `xorm:"BIGINT"` - CloneWarning bool + CloneWarning bool `xorm:"NOT NULL DEFAULT false"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } diff --git a/models/issues/issue.go b/models/issues/issue.go index d9be7d395e743..1aadb89c968ad 100644 --- a/models/issues/issue.go +++ b/models/issues/issue.go @@ -117,7 +117,7 @@ type Issue struct { MilestoneID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` Milestone *Milestone `xorm:"-"` Project *project_model.Project `xorm:"-"` - Priority int + Priority int `xorm:"NOT NULL DEFAULT 0"` AssigneeID int64 `xorm:"-"` Assignee *user_model.User `xorm:"-"` IsClosed bool `xorm:"INDEX NOT NULL DEFAULT false"` diff --git a/models/migrations/v226.go b/models/migrations/v226.go index 8539f0d4be130..6e06069bbe49e 100644 --- a/models/migrations/v226.go +++ b/models/migrations/v226.go @@ -63,7 +63,7 @@ func convertFromNullToDefault(x *xorm.Engine) error { Title string `xorm:"name"` Content string `xorm:"LONGTEXT"` MilestoneID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` - Priority int + Priority int `xorm:"NOT NULL DEFAULT 0"` IsClosed bool `xorm:"INDEX NOT NULL DEFAULT false"` IsPull bool `xorm:"INDEX NOT NULL DEFAULT false"` // Indicates whether is a pull request or not. NumComments int `xorm:"NOT NULL DEFAULT 0"` @@ -102,7 +102,7 @@ func convertFromNullToDefault(x *xorm.Engine) error { IssueID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // maybe zero when creating ReleaseID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // maybe zero when creating UploaderID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // Notice: will be zero before this column added - CommentID int64 + CommentID int64 `xorm:"NOT NULL DEFAULT 0"` Name string DownloadCount int64 `xorm:"NOT NULL DEFAULT 0"` Size int64 `xorm:"NOT NULL DEFAULT 0"` @@ -175,7 +175,7 @@ func convertFromNullToDefault(x *xorm.Engine) error { FullName string // Email is the primary email address (to be used for communication) Email string `xorm:"NOT NULL"` - KeepEmailPrivate bool + KeepEmailPrivate bool `xorm:"NOT NULL DEFAULT false"` EmailNotificationsPreference string `xorm:"VARCHAR(20) NOT NULL DEFAULT 'enabled'"` Passwd string `xorm:"NOT NULL"` PasswdHashAlgo string `xorm:"NOT NULL DEFAULT 'argon2'"` @@ -244,5 +244,28 @@ func convertFromNullToDefault(x *xorm.Engine) error { } recreateTable(sess, &User{}) + type WebAuthnCredential struct { + ID int64 `xorm:"pk autoincr"` + Name string + LowerName string `xorm:"unique(s)"` + UserID int64 `xorm:"INDEX unique(s)"` + CredentialID []byte `xorm:"INDEX VARBINARY(1024)"` + PublicKey []byte + AttestationType string + AAGUID []byte + SignCount uint32 `xorm:"BIGINT"` + CloneWarning bool `xorm:"NOT NULL DEFAULT false"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` + UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` + } + recreateTable(sess, &WebAuthnCredential{}) + + type UserBadge struct { + ID int64 `xorm:"pk autoincr"` + BadgeID int64 `xorm:"NOT NULL DEFAULT 0"` + UserID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` + } + recreateTable(sess, &WebAuthnCredential{}) + return sess.Commit() } diff --git a/models/repo/attachment.go b/models/repo/attachment.go index 88d6a87f52837..9603599517bff 100644 --- a/models/repo/attachment.go +++ b/models/repo/attachment.go @@ -24,7 +24,7 @@ type Attachment struct { IssueID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // maybe zero when creating ReleaseID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // maybe zero when creating UploaderID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` // Notice: will be zero before this column added - CommentID int64 + CommentID int64 `xorm:"NOT NULL DEFAULT 0"` Name string DownloadCount int64 `xorm:"NOT NULL DEFAULT 0"` Size int64 `xorm:"NOT NULL DEFAULT 0"` diff --git a/models/user/badge.go b/models/user/badge.go index 5ff840cb8c35e..5cbbe7b7197d0 100644 --- a/models/user/badge.go +++ b/models/user/badge.go @@ -20,8 +20,8 @@ type Badge struct { // UserBadge represents a user badge type UserBadge struct { ID int64 `xorm:"pk autoincr"` - BadgeID int64 - UserID int64 `xorm:"INDEX"` + BadgeID int64 `xorm:"NOT NULL DEFAULT 0"` + UserID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` } func init() { diff --git a/models/user/user.go b/models/user/user.go index 46ae650d81ebd..8484f1753be87 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -82,7 +82,7 @@ type User struct { FullName string // Email is the primary email address (to be used for communication) Email string `xorm:"NOT NULL"` - KeepEmailPrivate bool + KeepEmailPrivate bool `xorm:"NOT NULL DEFAULT false"` EmailNotificationsPreference string `xorm:"VARCHAR(20) NOT NULL DEFAULT 'enabled'"` Passwd string `xorm:"NOT NULL"` PasswdHashAlgo string `xorm:"NOT NULL DEFAULT 'argon2'"` From 881278f6177cb2966bd2d5c46f92822cfe4489f1 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Tue, 20 Sep 2022 10:50:13 +0000 Subject: [PATCH 04/11] lint --- models/auth/webauthn.go | 2 +- models/issues/issue.go | 16 ++++++++-------- models/migrations/v226.go | 10 +++++----- models/user/badge.go | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/models/auth/webauthn.go b/models/auth/webauthn.go index 90905df3c6d89..8c8591d904340 100644 --- a/models/auth/webauthn.go +++ b/models/auth/webauthn.go @@ -46,7 +46,7 @@ type WebAuthnCredential struct { PublicKey []byte AttestationType string AAGUID []byte - SignCount uint32 `xorm:"BIGINT"` + SignCount uint32 `xorm:"BIGINT"` CloneWarning bool `xorm:"NOT NULL DEFAULT false"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` diff --git a/models/issues/issue.go b/models/issues/issue.go index 1aadb89c968ad..9df76a84a9b90 100644 --- a/models/issues/issue.go +++ b/models/issues/issue.go @@ -117,14 +117,14 @@ type Issue struct { MilestoneID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` Milestone *Milestone `xorm:"-"` Project *project_model.Project `xorm:"-"` - Priority int `xorm:"NOT NULL DEFAULT 0"` - AssigneeID int64 `xorm:"-"` - Assignee *user_model.User `xorm:"-"` - IsClosed bool `xorm:"INDEX NOT NULL DEFAULT false"` - IsRead bool `xorm:"-"` - IsPull bool `xorm:"INDEX NOT NULL DEFAULT false"` // Indicates whether is a pull request or not. - PullRequest *PullRequest `xorm:"-"` - NumComments int `xorm:"NOT NULL DEFAULT 0"` + Priority int `xorm:"NOT NULL DEFAULT 0"` + AssigneeID int64 `xorm:"-"` + Assignee *user_model.User `xorm:"-"` + IsClosed bool `xorm:"INDEX NOT NULL DEFAULT false"` + IsRead bool `xorm:"-"` + IsPull bool `xorm:"INDEX NOT NULL DEFAULT false"` // Indicates whether is a pull request or not. + PullRequest *PullRequest `xorm:"-"` + NumComments int `xorm:"NOT NULL DEFAULT 0"` Ref string DeadlineUnix timeutil.TimeStamp `xorm:"INDEX"` diff --git a/models/migrations/v226.go b/models/migrations/v226.go index 6e06069bbe49e..2190cfdb3cd3e 100644 --- a/models/migrations/v226.go +++ b/models/migrations/v226.go @@ -63,10 +63,10 @@ func convertFromNullToDefault(x *xorm.Engine) error { Title string `xorm:"name"` Content string `xorm:"LONGTEXT"` MilestoneID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` - Priority int `xorm:"NOT NULL DEFAULT 0"` - IsClosed bool `xorm:"INDEX NOT NULL DEFAULT false"` - IsPull bool `xorm:"INDEX NOT NULL DEFAULT false"` // Indicates whether is a pull request or not. - NumComments int `xorm:"NOT NULL DEFAULT 0"` + Priority int `xorm:"NOT NULL DEFAULT 0"` + IsClosed bool `xorm:"INDEX NOT NULL DEFAULT false"` + IsPull bool `xorm:"INDEX NOT NULL DEFAULT false"` // Indicates whether is a pull request or not. + NumComments int `xorm:"NOT NULL DEFAULT 0"` Ref string DeadlineUnix timeutil.TimeStamp `xorm:"INDEX"` @@ -253,7 +253,7 @@ func convertFromNullToDefault(x *xorm.Engine) error { PublicKey []byte AttestationType string AAGUID []byte - SignCount uint32 `xorm:"BIGINT"` + SignCount uint32 `xorm:"BIGINT"` CloneWarning bool `xorm:"NOT NULL DEFAULT false"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` diff --git a/models/user/badge.go b/models/user/badge.go index 5cbbe7b7197d0..94ec2712b799d 100644 --- a/models/user/badge.go +++ b/models/user/badge.go @@ -21,7 +21,7 @@ type Badge struct { type UserBadge struct { ID int64 `xorm:"pk autoincr"` BadgeID int64 `xorm:"NOT NULL DEFAULT 0"` - UserID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` + UserID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` } func init() { From c24128509b2730bb0e3d6bf3e771a494463552fd Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Mon, 26 Sep 2022 20:06:49 +0000 Subject: [PATCH 05/11] Set value for is_mirror. --- models/fixtures/repository.yml | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/models/fixtures/repository.yml b/models/fixtures/repository.yml index b63c676c9154c..5f885740fe8b6 100644 --- a/models/fixtures/repository.yml +++ b/models/fixtures/repository.yml @@ -16,6 +16,7 @@ num_watches: 4 num_projects: 1 num_closed_projects: 0 + is_mirror: false status: 0 - @@ -33,6 +34,7 @@ num_closed_pulls: 0 num_stars: 1 close_issues_via_commit_in_any_branch: true + is_mirror: false status: 0 - @@ -51,6 +53,7 @@ num_watches: 0 num_projects: 1 num_closed_projects: 0 + is_mirror: false status: 0 - @@ -68,6 +71,7 @@ num_stars: 1 num_projects: 0 num_closed_projects: 1 + is_mirror: false status: 0 - @@ -225,6 +229,7 @@ name: repo15 is_empty: false is_private: true + is_mirror: false status: 0 - @@ -240,6 +245,7 @@ num_pulls: 0 num_closed_pulls: 0 num_watches: 0 + is_mirror: false status: 0 - @@ -496,6 +502,7 @@ name: utf8 is_empty: false is_private: false + is_mirror: false status: 0 - @@ -619,7 +626,7 @@ num_forks: 0 num_issues: 1 num_milestones: 1 - is_mirror: + is_mirror: false is_archived: false - @@ -739,6 +746,7 @@ num_watches: 0 num_projects: 0 num_closed_projects: 0 + is_mirror: false status: 0 - @@ -759,6 +767,7 @@ num_watches: 0 num_projects: 0 num_closed_projects: 0 + is_mirror: false status: 0 - @@ -769,4 +778,5 @@ name: empty is_empty: true is_private: true + is_mirror: false status: 0 From 28bff399b5fcf15256d79cf0b09d6b36ea75795d Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Mon, 26 Sep 2022 22:01:42 +0000 Subject: [PATCH 06/11] Fix WebAuthnCredential table name. --- models/migrations/v226.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/migrations/v226.go b/models/migrations/v226.go index 2190cfdb3cd3e..782314076c7bb 100644 --- a/models/migrations/v226.go +++ b/models/migrations/v226.go @@ -244,7 +244,7 @@ func convertFromNullToDefault(x *xorm.Engine) error { } recreateTable(sess, &User{}) - type WebAuthnCredential struct { + type webauthnCredential struct { ID int64 `xorm:"pk autoincr"` Name string LowerName string `xorm:"unique(s)"` @@ -258,14 +258,14 @@ func convertFromNullToDefault(x *xorm.Engine) error { CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } - recreateTable(sess, &WebAuthnCredential{}) + recreateTable(sess, &webauthnCredential{}) type UserBadge struct { ID int64 `xorm:"pk autoincr"` BadgeID int64 `xorm:"NOT NULL DEFAULT 0"` UserID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` } - recreateTable(sess, &WebAuthnCredential{}) + recreateTable(sess, &UserBadge{}) return sess.Commit() } From 9ca0bf69ab8314025ba0f5a5b0355bee7fd39561 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Mon, 26 Sep 2022 22:26:13 +0000 Subject: [PATCH 07/11] Check for errors. --- models/migrations/v226.go | 40 +++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/models/migrations/v226.go b/models/migrations/v226.go index 782314076c7bb..4375674518152 100644 --- a/models/migrations/v226.go +++ b/models/migrations/v226.go @@ -34,7 +34,9 @@ func convertFromNullToDefault(x *xorm.Engine) error { CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } - recreateTable(sess, &Label{}) + if err := recreateTable(sess, &Label{}); err != nil { + return err + } type Milestone struct { ID int64 `xorm:"pk autoincr"` @@ -51,7 +53,9 @@ func convertFromNullToDefault(x *xorm.Engine) error { DeadlineUnix timeutil.TimeStamp ClosedDateUnix timeutil.TimeStamp } - recreateTable(sess, &Milestone{}) + if err := recreateTable(sess, &Milestone{}); err != nil { + return err + } type Issue struct { ID int64 `xorm:"pk autoincr"` @@ -79,7 +83,9 @@ func convertFromNullToDefault(x *xorm.Engine) error { // with write access IsLocked bool `xorm:"NOT NULL DEFAULT false"` } - recreateTable(sess, &Issue{}) + if err := recreateTable(sess, &Issue{}); err != nil { + return err + } type Team struct { ID int64 `xorm:"pk autoincr"` @@ -93,7 +99,9 @@ func convertFromNullToDefault(x *xorm.Engine) error { IncludesAllRepositories bool `xorm:"NOT NULL DEFAULT false"` CanCreateOrgRepo bool `xorm:"NOT NULL DEFAULT false"` } - recreateTable(sess, &Team{}) + if err := recreateTable(sess, &Team{}); err != nil { + return err + } type Attachment struct { ID int64 `xorm:"pk autoincr"` @@ -108,7 +116,9 @@ func convertFromNullToDefault(x *xorm.Engine) error { Size int64 `xorm:"NOT NULL DEFAULT 0"` CreatedUnix timeutil.TimeStamp `xorm:"created"` } - recreateTable(sess, &Attachment{}) + if err := recreateTable(sess, &Attachment{}); err != nil { + return err + } type Repository struct { ID int64 `xorm:"pk autoincr"` @@ -157,7 +167,9 @@ func convertFromNullToDefault(x *xorm.Engine) error { CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } - recreateTable(sess, &Repository{}) + if err := recreateTable(sess, &Repository{}); err != nil { + return err + } type Topic struct { ID int64 `xorm:"pk autoincr"` @@ -166,7 +178,9 @@ func convertFromNullToDefault(x *xorm.Engine) error { CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } - recreateTable(sess, &Topic{}) + if err := recreateTable(sess, &Topic{}); err != nil { + return err + } type User struct { ID int64 `xorm:"pk autoincr"` @@ -242,7 +256,9 @@ func convertFromNullToDefault(x *xorm.Engine) error { Theme string `xorm:"NOT NULL DEFAULT ''"` KeepActivityPrivate bool `xorm:"NOT NULL DEFAULT false"` } - recreateTable(sess, &User{}) + if err := recreateTable(sess, &User{}); err != nil { + return err + } type webauthnCredential struct { ID int64 `xorm:"pk autoincr"` @@ -258,14 +274,18 @@ func convertFromNullToDefault(x *xorm.Engine) error { CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } - recreateTable(sess, &webauthnCredential{}) + if err := recreateTable(sess, &webauthnCredential{}); err != nil { + return err + } type UserBadge struct { ID int64 `xorm:"pk autoincr"` BadgeID int64 `xorm:"NOT NULL DEFAULT 0"` UserID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` } - recreateTable(sess, &UserBadge{}) + if err := recreateTable(sess, &UserBadge{}); err != nil { + return err + } return sess.Commit() } From 9548f35bb958869f72c6b478a744aa147ca2f33c Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Tue, 27 Sep 2022 10:04:54 +0000 Subject: [PATCH 08/11] Use modifyColumns. --- models/migrations/migrations.go | 13 +++-- models/migrations/v165.go | 4 +- models/migrations/v179.go | 2 +- models/migrations/v205.go | 4 +- models/migrations/v226.go | 90 ++++++++++++++++++++------------- 5 files changed, 68 insertions(+), 45 deletions(-) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index a124f08665ff1..20517e768fcbc 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -981,8 +981,8 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin return nil } -// modifyColumn will modify column's type or other property. SQLITE is not supported -func modifyColumn(x *xorm.Engine, tableName string, col *schemas.Column) error { +// modifyColumns will modify columns type or other property. SQLITE is not supported +func modifyColumns(x *xorm.Engine, tableName string, cols ...*schemas.Column) error { var indexes map[string]*schemas.Index var err error // MSSQL have to remove index at first, otherwise alter column will fail @@ -1010,9 +1010,12 @@ func modifyColumn(x *xorm.Engine, tableName string, col *schemas.Column) error { } }() - alterSQL := x.Dialect().ModifyColumnSQL(tableName, col) - if _, err := x.Exec(alterSQL); err != nil { - return err + for _, col := range cols { + alterSQL := x.Dialect().ModifyColumnSQL(tableName, col) + if _, err := x.Exec(alterSQL); err != nil { + return err + } } + return nil } diff --git a/models/migrations/v165.go b/models/migrations/v165.go index 87e1a24f2812a..e1185e65307d6 100644 --- a/models/migrations/v165.go +++ b/models/migrations/v165.go @@ -19,7 +19,7 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { Typ string `xorm:"VARCHAR(16) index"` } - if err := modifyColumn(x, "hook_task", &schemas.Column{ + if err := modifyColumns(x, "hook_task", &schemas.Column{ Name: "typ", SQLType: schemas.SQLType{ Name: "VARCHAR", @@ -45,7 +45,7 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { Type string `xorm:"VARCHAR(16) index"` } - if err := modifyColumn(x, "webhook", &schemas.Column{ + if err := modifyColumns(x, "webhook", &schemas.Column{ Name: "type", SQLType: schemas.SQLType{ Name: "VARCHAR", diff --git a/models/migrations/v179.go b/models/migrations/v179.go index e6dddef27333a..20ec7950de154 100644 --- a/models/migrations/v179.go +++ b/models/migrations/v179.go @@ -16,7 +16,7 @@ func convertAvatarURLToText(x *xorm.Engine) error { } // Some oauth2 providers may give very long avatar urls (i.e. Google) - return modifyColumn(x, "external_login_user", &schemas.Column{ + return modifyColumns(x, "external_login_user", &schemas.Column{ Name: "avatar_url", SQLType: schemas.SQLType{ Name: schemas.Text, diff --git a/models/migrations/v205.go b/models/migrations/v205.go index 7aefa0431ac19..9bf78f67eda0c 100644 --- a/models/migrations/v205.go +++ b/models/migrations/v205.go @@ -16,7 +16,7 @@ func migrateUserPasswordSalt(x *xorm.Engine) error { return nil } - if err := modifyColumn(x, "user", &schemas.Column{ + if err := modifyColumns(x, "user", &schemas.Column{ Name: "rands", SQLType: schemas.SQLType{ Name: "VARCHAR", @@ -29,7 +29,7 @@ func migrateUserPasswordSalt(x *xorm.Engine) error { return err } - return modifyColumn(x, "user", &schemas.Column{ + return modifyColumns(x, "user", &schemas.Column{ Name: "salt", SQLType: schemas.SQLType{ Name: "VARCHAR", diff --git a/models/migrations/v226.go b/models/migrations/v226.go index 4375674518152..32cd24b415c6b 100644 --- a/models/migrations/v226.go +++ b/models/migrations/v226.go @@ -9,19 +9,15 @@ import ( "code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "xorm.io/xorm" + "xorm.io/xorm/schemas" ) func convertFromNullToDefault(x *xorm.Engine) error { - sess := x.NewSession() - defer sess.Close() - if err := sess.Begin(); err != nil { - return err - } - type Label struct { ID int64 `xorm:"pk autoincr"` RepoID int64 `xorm:"INDEX"` @@ -34,9 +30,6 @@ func convertFromNullToDefault(x *xorm.Engine) error { CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } - if err := recreateTable(sess, &Label{}); err != nil { - return err - } type Milestone struct { ID int64 `xorm:"pk autoincr"` @@ -53,9 +46,6 @@ func convertFromNullToDefault(x *xorm.Engine) error { DeadlineUnix timeutil.TimeStamp ClosedDateUnix timeutil.TimeStamp } - if err := recreateTable(sess, &Milestone{}); err != nil { - return err - } type Issue struct { ID int64 `xorm:"pk autoincr"` @@ -83,9 +73,6 @@ func convertFromNullToDefault(x *xorm.Engine) error { // with write access IsLocked bool `xorm:"NOT NULL DEFAULT false"` } - if err := recreateTable(sess, &Issue{}); err != nil { - return err - } type Team struct { ID int64 `xorm:"pk autoincr"` @@ -99,9 +86,6 @@ func convertFromNullToDefault(x *xorm.Engine) error { IncludesAllRepositories bool `xorm:"NOT NULL DEFAULT false"` CanCreateOrgRepo bool `xorm:"NOT NULL DEFAULT false"` } - if err := recreateTable(sess, &Team{}); err != nil { - return err - } type Attachment struct { ID int64 `xorm:"pk autoincr"` @@ -116,9 +100,6 @@ func convertFromNullToDefault(x *xorm.Engine) error { Size int64 `xorm:"NOT NULL DEFAULT 0"` CreatedUnix timeutil.TimeStamp `xorm:"created"` } - if err := recreateTable(sess, &Attachment{}); err != nil { - return err - } type Repository struct { ID int64 `xorm:"pk autoincr"` @@ -167,9 +148,6 @@ func convertFromNullToDefault(x *xorm.Engine) error { CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } - if err := recreateTable(sess, &Repository{}); err != nil { - return err - } type Topic struct { ID int64 `xorm:"pk autoincr"` @@ -178,9 +156,6 @@ func convertFromNullToDefault(x *xorm.Engine) error { CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } - if err := recreateTable(sess, &Topic{}); err != nil { - return err - } type User struct { ID int64 `xorm:"pk autoincr"` @@ -256,9 +231,6 @@ func convertFromNullToDefault(x *xorm.Engine) error { Theme string `xorm:"NOT NULL DEFAULT ''"` KeepActivityPrivate bool `xorm:"NOT NULL DEFAULT false"` } - if err := recreateTable(sess, &User{}); err != nil { - return err - } type webauthnCredential struct { ID int64 `xorm:"pk autoincr"` @@ -274,18 +246,66 @@ func convertFromNullToDefault(x *xorm.Engine) error { CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } - if err := recreateTable(sess, &webauthnCredential{}); err != nil { - return err - } type UserBadge struct { ID int64 `xorm:"pk autoincr"` BadgeID int64 `xorm:"NOT NULL DEFAULT 0"` UserID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` } - if err := recreateTable(sess, &UserBadge{}); err != nil { + + beans := []interface{}{ + &Label{}, + &Milestone{}, + &Issue{}, + &Team{}, + &Attachment{}, + &Repository{}, + &Topic{}, + &User{}, + &webauthnCredential{}, + &UserBadge{}, + } + + if setting.Database.UseSQLite3 { + sess := x.NewSession() + defer sess.Close() + if err := sess.Begin(); err != nil { + return err + } + + for _, bean := range beans { + if err := recreateTable(sess, bean); err != nil { + return err + } + } + + return sess.Commit() + } else { + for _, bean := range beans { + if err := setDefaultsForColumns(x, bean); err != nil { + return err + } + } + } + + return nil +} + +func setDefaultsForColumns(x *xorm.Engine, bean interface{}) error { + table, err := x.TableInfo(bean) + if err != nil { return err } - return sess.Commit() + columns := []*schemas.Column{} + + for _, c := range table.Columns() { + if c.Nullable || c.IsPrimaryKey { + continue + } + + columns = append(columns, c) + } + + return modifyColumns(x, table.Name, columns...) } From 0622a247a18f96b217780128cf706e955ef2d17f Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Tue, 27 Sep 2022 10:22:29 +0000 Subject: [PATCH 09/11] lint --- models/migrations/v226.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/models/migrations/v226.go b/models/migrations/v226.go index 32cd24b415c6b..fcaec9ac945a0 100644 --- a/models/migrations/v226.go +++ b/models/migrations/v226.go @@ -280,11 +280,11 @@ func convertFromNullToDefault(x *xorm.Engine) error { } return sess.Commit() - } else { - for _, bean := range beans { - if err := setDefaultsForColumns(x, bean); err != nil { - return err - } + } + + for _, bean := range beans { + if err := setDefaultsForColumns(x, bean); err != nil { + return err } } From 29f844326b9d88d1089bbeb72fb8f0bf623913d5 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Tue, 27 Sep 2022 11:58:10 +0000 Subject: [PATCH 10/11] Set default value before alter. --- models/migrations/migrations.go | 1 + models/migrations/v226.go | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 20517e768fcbc..72e1d95364125 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -1013,6 +1013,7 @@ func modifyColumns(x *xorm.Engine, tableName string, cols ...*schemas.Column) er for _, col := range cols { alterSQL := x.Dialect().ModifyColumnSQL(tableName, col) if _, err := x.Exec(alterSQL); err != nil { + log.Error("Error modifying column: %v", col.Name) return err } } diff --git a/models/migrations/v226.go b/models/migrations/v226.go index fcaec9ac945a0..82dba3d18342c 100644 --- a/models/migrations/v226.go +++ b/models/migrations/v226.go @@ -5,10 +5,13 @@ package migrations import ( + "reflect" + "code.gitea.io/gitea/models/auth" "code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" @@ -275,6 +278,7 @@ func convertFromNullToDefault(x *xorm.Engine) error { for _, bean := range beans { if err := recreateTable(sess, bean); err != nil { + log.Error("Error recreating table '%v': %v", reflect.TypeOf(bean).Elem().Name(), err) return err } } @@ -284,6 +288,7 @@ func convertFromNullToDefault(x *xorm.Engine) error { for _, bean := range beans { if err := setDefaultsForColumns(x, bean); err != nil { + log.Error("Error modifying columns of table '%v': %v", reflect.TypeOf(bean).Elem().Name(), err) return err } } @@ -300,10 +305,15 @@ func setDefaultsForColumns(x *xorm.Engine, bean interface{}) error { columns := []*schemas.Column{} for _, c := range table.Columns() { - if c.Nullable || c.IsPrimaryKey { + if c.Nullable || c.IsPrimaryKey || c.DefaultIsEmpty { continue } + _, err := x.Exec("UPDATE `" + table.Name + "` SET `" + c.Name + "` = " + c.Default + " WHERE `" + c.Name + "` IS NULL") + if err != nil { + return err + } + columns = append(columns, c) } From e3e00dc55e7e91c410402ce5e5455bec388f0750 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Fri, 7 Oct 2022 12:32:19 +0000 Subject: [PATCH 11/11] Use table recreate for MSSQL. --- models/migrations/v227.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/migrations/v227.go b/models/migrations/v227.go index 82dba3d18342c..a464492768998 100644 --- a/models/migrations/v227.go +++ b/models/migrations/v227.go @@ -269,7 +269,7 @@ func convertFromNullToDefault(x *xorm.Engine) error { &UserBadge{}, } - if setting.Database.UseSQLite3 { + if setting.Database.UseSQLite3 || setting.Database.UseMSSQL { sess := x.NewSession() defer sess.Close() if err := sess.Begin(); err != nil {