Skip to content
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

Converts columns from NULL to NOT NULL DEFAULT #18871

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
eb3ec0f
Converts columns from NULL to NOT NULL DEFAULT.
KN4CK3R Feb 23, 2022
b8dd4f2
Fixed unit tests.
KN4CK3R Feb 23, 2022
3688b8d
Merge branch 'main' of https://github.com/go-gitea/gitea into fix-nul…
KN4CK3R Feb 26, 2022
de70352
Merge branch 'main' into fix-null-fields
KN4CK3R Mar 8, 2022
0f7fe3a
Merge branch 'main' of https://github.com/go-gitea/gitea into fix-nul…
KN4CK3R Mar 23, 2022
fa3065e
Merge branch 'main' of https://github.com/go-gitea/gitea into fix-nul…
KN4CK3R May 2, 2022
df7fcbf
Merge branch 'main' of https://github.com/go-gitea/gitea into fix-nul…
KN4CK3R Sep 20, 2022
40bb73b
Add more NOT NULLs.
KN4CK3R Sep 20, 2022
881278f
lint
KN4CK3R Sep 20, 2022
c241285
Set value for is_mirror.
KN4CK3R Sep 26, 2022
334e494
Merge branch 'main' of https://github.com/go-gitea/gitea into fix-nul…
KN4CK3R Sep 26, 2022
28bff39
Fix WebAuthnCredential table name.
KN4CK3R Sep 26, 2022
9ca0bf6
Check for errors.
KN4CK3R Sep 26, 2022
22321e4
Merge branch 'main' into fix-null-fields
6543 Sep 27, 2022
9548f35
Use modifyColumns.
KN4CK3R Sep 27, 2022
f9074b6
Merge branch 'main' of https://github.com/go-gitea/gitea into fix-nul…
KN4CK3R Sep 27, 2022
faeeaa5
Merge branch 'fix-null-fields' of https://github.com/KN4CK3R/gitea in…
KN4CK3R Sep 27, 2022
0622a24
lint
KN4CK3R Sep 27, 2022
29f8443
Set default value before alter.
KN4CK3R Sep 27, 2022
1719a6c
Merge branch 'main' of https://github.com/go-gitea/gitea into fix-nul…
KN4CK3R Oct 7, 2022
e3e00dc
Use table recreate for MSSQL.
KN4CK3R Oct 7, 2022
b738ad0
Merge branch 'main' into fix-null-fields
KN4CK3R Oct 10, 2022
ceb577e
Merge branch 'main' into fix-null-fields
KN4CK3R Oct 11, 2022
79adee3
Merge branch 'main' into fix-null-fields
KN4CK3R Oct 11, 2022
5ca1d59
Merge branch 'main' into fix-null-fields
6543 Oct 16, 2022
61c2ea6
Merge branch 'main' into fix-null-fields
6543 Oct 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions models/fixtures/repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
owner_name: user3
lower_name: repo3
name: repo3
is_archived: false
is_empty: false
is_private: true
num_issues: 1
Expand Down
1 change: 1 addition & 0 deletions models/fixtures/user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@
avatar: avatar27
avatar_email: user27@example.com
num_repos: 3
is_active: true

-
id: 28
Expand Down
12 changes: 6 additions & 6 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,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"`
Expand Down
6 changes: 3 additions & 3 deletions models/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
12 changes: 6 additions & 6 deletions models/issue_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,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
6543 marked this conversation as resolved.
Show resolved Hide resolved
NewMigration("Convert columns from NULL to NOT NULL DEFAULT", convertFromNullToDefault),
}

// GetCurrentDBVersion returns the current db version
Expand Down
248 changes: 248 additions & 0 deletions models/migrations/v211.go
Original file line number Diff line number Diff line change
@@ -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{})
6543 marked this conversation as resolved.
Show resolved Hide resolved

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()
}
12 changes: 6 additions & 6 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 6 additions & 6 deletions models/repo/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
Loading