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 24 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
4 changes: 2 additions & 2 deletions models/auth/webauthn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
13 changes: 12 additions & 1 deletion models/fixtures/repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
num_watches: 4
num_projects: 1
num_closed_projects: 0
is_mirror: false
status: 0

-
Expand All @@ -33,6 +34,7 @@
num_closed_pulls: 0
num_stars: 1
close_issues_via_commit_in_any_branch: true
is_mirror: false
status: 0

-
Expand All @@ -41,6 +43,7 @@
owner_name: user3
lower_name: repo3
name: repo3
is_archived: false
is_empty: false
is_private: true
num_issues: 1
Expand All @@ -50,6 +53,7 @@
num_watches: 0
num_projects: 1
num_closed_projects: 0
is_mirror: false
status: 0

-
Expand All @@ -67,6 +71,7 @@
num_stars: 1
num_projects: 0
num_closed_projects: 1
is_mirror: false
status: 0

-
Expand Down Expand Up @@ -224,6 +229,7 @@
name: repo15
is_empty: false
is_private: true
is_mirror: false
status: 0

-
Expand All @@ -239,6 +245,7 @@
num_pulls: 0
num_closed_pulls: 0
num_watches: 0
is_mirror: false
status: 0

-
Expand Down Expand Up @@ -495,6 +502,7 @@
name: utf8
is_empty: false
is_private: false
is_mirror: false
status: 0

-
Expand Down Expand Up @@ -618,7 +626,7 @@
num_forks: 0
num_issues: 1
num_milestones: 1
is_mirror:
is_mirror: false
is_archived: false

-
Expand Down Expand Up @@ -738,6 +746,7 @@
num_watches: 0
num_projects: 0
num_closed_projects: 0
is_mirror: false
status: 0

-
Expand All @@ -758,6 +767,7 @@
num_watches: 0
num_projects: 0
num_closed_projects: 0
is_mirror: false
status: 0

-
Expand All @@ -768,4 +778,5 @@
name: empty
is_empty: true
is_private: true
is_mirror: false
status: 0
1 change: 1 addition & 0 deletions models/fixtures/user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@
avatar: avatar27
avatar_email: user27@example.com
num_repos: 3
is_active: true

-
id: 28
Expand Down
22 changes: 11 additions & 11 deletions models/issues/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
6 changes: 3 additions & 3 deletions models/issues/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
12 changes: 6 additions & 6 deletions models/issues/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
16 changes: 11 additions & 5 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions models/migrations/v165.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion models/migrations/v179.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions models/migrations/v205.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Loading