diff --git a/models/auth/webauthn.go b/models/auth/webauthn.go index d3062342f545b..8c8591d904340 100644 --- a/models/auth/webauthn.go +++ b/models/auth/webauthn.go @@ -46,8 +46,8 @@ type WebAuthnCredential struct { PublicKey []byte AttestationType string AAGUID []byte - SignCount uint32 `xorm:"BIGINT"` - CloneWarning bool + 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/fixtures/repository.yml b/models/fixtures/repository.yml index 82b3ed16dcccf..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 - @@ -41,6 +43,7 @@ owner_name: user3 lower_name: repo3 name: repo3 + is_archived: false is_empty: false is_private: true num_issues: 1 @@ -50,6 +53,7 @@ num_watches: 0 num_projects: 1 num_closed_projects: 0 + is_mirror: false status: 0 - @@ -67,6 +71,7 @@ num_stars: 1 num_projects: 0 num_closed_projects: 1 + is_mirror: false status: 0 - @@ -224,6 +229,7 @@ name: repo15 is_empty: false is_private: true + is_mirror: false status: 0 - @@ -239,6 +245,7 @@ num_pulls: 0 num_closed_pulls: 0 num_watches: 0 + is_mirror: false status: 0 - @@ -495,6 +502,7 @@ name: utf8 is_empty: false is_private: false + is_mirror: false status: 0 - @@ -618,7 +626,7 @@ num_forks: 0 num_issues: 1 num_milestones: 1 - is_mirror: + is_mirror: false is_archived: false - @@ -738,6 +746,7 @@ num_watches: 0 num_projects: 0 num_closed_projects: 0 + is_mirror: false status: 0 - @@ -758,6 +767,7 @@ num_watches: 0 num_projects: 0 num_closed_projects: 0 + is_mirror: false status: 0 - @@ -768,4 +778,5 @@ name: empty is_empty: true is_private: true + is_mirror: false status: 0 diff --git a/models/fixtures/user.yml b/models/fixtures/user.yml index 790156189a463..7aea31fd006d8 100644 --- a/models/fixtures/user.yml +++ b/models/fixtures/user.yml @@ -497,6 +497,7 @@ avatar: avatar27 avatar_email: user27@example.com num_repos: 3 + is_active: true - id: 28 diff --git a/models/issues/issue.go b/models/issues/issue.go index 737b625abc160..3e86c0e180963 100644 --- a/models/issues/issue.go +++ b/models/issues/issue.go @@ -106,25 +106,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_model.Project `xorm:"-"` - Priority int - AssigneeID int64 `xorm:"-"` - Assignee *user_model.User `xorm:"-"` - IsClosed bool `xorm:"INDEX"` - IsRead bool `xorm:"-"` - IsPull bool `xorm:"INDEX"` // Indicates whether is a pull request or not. - PullRequest *PullRequest `xorm:"-"` - NumComments int + 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/issues/label.go b/models/issues/label.go index 667a608687436..77c1e8140aa2a 100644 --- a/models/issues/label.go +++ b/models/issues/label.go @@ -79,9 +79,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/issues/milestone.go b/models/issues/milestone.go index 1021938b205ae..09cf13363a01e 100644 --- a/models/issues/milestone.go +++ b/models/issues/milestone.go @@ -47,12 +47,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 2a38772180457..210041ad1c0f0 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -415,6 +415,8 @@ var migrations = []Migration{ NewMigration("Alter gpg_key/public_key content TEXT fields to MEDIUMTEXT", alterPublicGPGKeyContentFieldsToMediumText), // v226 -> v227 NewMigration("Conan and generic packages do not need to be semantically versioned", fixPackageSemverField), + // v227 -> v228 + NewMigration("Convert columns from NULL to NOT NULL DEFAULT", convertFromNullToDefault), } // GetCurrentDBVersion returns the current db version @@ -981,8 +983,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 +1012,13 @@ 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 { + log.Error("Error modifying column: %v", col.Name) + 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/v227.go b/models/migrations/v227.go new file mode 100644 index 0000000000000..a464492768998 --- /dev/null +++ b/models/migrations/v227.go @@ -0,0 +1,321 @@ +// 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 ( + "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" + + "xorm.io/xorm" + "xorm.io/xorm/schemas" +) + +func convertFromNullToDefault(x *xorm.Engine) error { + 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"` + } + + 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 + } + + 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 `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"` + + 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"` + } + + 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"` + } + + 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 `xorm:"NOT NULL DEFAULT 0"` + Name string + DownloadCount int64 `xorm:"NOT NULL DEFAULT 0"` + Size int64 `xorm:"NOT NULL DEFAULT 0"` + CreatedUnix timeutil.TimeStamp `xorm:"created"` + } + + 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"` + } + + 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"` + } + + 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 `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'"` + + // 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"` + } + + 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"` + } + + type UserBadge struct { + ID int64 `xorm:"pk autoincr"` + BadgeID int64 `xorm:"NOT NULL DEFAULT 0"` + UserID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` + } + + beans := []interface{}{ + &Label{}, + &Milestone{}, + &Issue{}, + &Team{}, + &Attachment{}, + &Repository{}, + &Topic{}, + &User{}, + &webauthnCredential{}, + &UserBadge{}, + } + + if setting.Database.UseSQLite3 || setting.Database.UseMSSQL { + 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 { + log.Error("Error recreating table '%v': %v", reflect.TypeOf(bean).Elem().Name(), err) + return err + } + } + + return sess.Commit() + } + + 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 + } + } + + return nil +} + +func setDefaultsForColumns(x *xorm.Engine, bean interface{}) error { + table, err := x.TableInfo(bean) + if err != nil { + return err + } + + columns := []*schemas.Column{} + + for _, c := range table.Columns() { + 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) + } + + return modifyColumns(x, table.Name, columns...) +} diff --git a/models/organization/team.go b/models/organization/team.go index bd80b1a8c7888..6e9f05d7c7571 100644 --- a/models/organization/team.go +++ b/models/organization/team.go @@ -66,18 +66,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 afec78a4254b7..9603599517bff 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 - CommentID int64 + 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 `xorm:"NOT NULL DEFAULT 0"` 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 4fd97dcd1ba34..bf8de623311d8 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -123,14 +123,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"` @@ -139,10 +139,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"` @@ -152,10 +152,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 7ba9a49e894e7..5e3d80e5f4967 100644 --- a/models/repo/topic.go +++ b/models/repo/topic.go @@ -26,9 +26,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/badge.go b/models/user/badge.go index 5ff840cb8c35e..94ec2712b799d 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 a3c10c2492dd7..a3affea95123e 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'"` @@ -113,16 +113,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"` @@ -130,17 +130,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"` diff --git a/models/user/user_test.go b/models/user/user_test.go index 678d6c186cf1f..5f2ac0a60c17d 100644 --- a/models/user/user_test.go +++ b/models/user/user_test.go @@ -102,7 +102,7 @@ func TestSearchUsers(t *testing.T) { []int64{9}) testUserSuccess(&user_model.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(&user_model.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 9ad0711dc0ade..36e99bba5e018 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) {