Skip to content

[WIP] Add NOT NULL constraints to repository #978

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ var migrations = []Migration{
NewMigration("create user column allow create organization", createAllowCreateOrganizationColumn),
// V16 -> v17
NewMigration("create repo unit table and add units for all repos", addUnitsToTables),
// v17 -> v18
NewMigration("add not null constraints for repository", addNotNullConstraintsToRepository),
}

// Migrate database to current version
Expand Down
68 changes: 68 additions & 0 deletions models/migrations/v17.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2017 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 (
"github.com/go-xorm/xorm"
)

// RepoV17 describe the added constraints
type RepoV17 struct {
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"`
}

// TableName will be invoked by XORM
func (*RepoV17) TableName() string {
return "repository"
}

func addNotNullConstraintsToRepository(x *xorm.Engine) error {
sess := x.NewSession()
defer sess.Close()

if err := sess.Begin(); err != nil {
return err
}

if _, err := sess.Exec("UPDATE `repository` SET num_watches = 0 WHERE num_watches IS NULL"); err != nil {
return err
}

if _, err := sess.Exec("UPDATE `repository` SET num_stars = 0 WHERE num_stars IS NULL"); err != nil {
return err
}

if _, err := sess.Exec("UPDATE `repository` SET num_forks = 0 WHERE num_forks IS NULL"); err != nil {
return err
}

if _, err := sess.Exec("UPDATE `repository` SET num_issues = 0 WHERE num_issues IS NULL"); err != nil {
return err
}

if _, err := sess.Exec("UPDATE `repository` SET num_closed_issues = 0 WHERE num_closed_issues IS NULL"); err != nil {
return err
}

if _, err := sess.Exec("UPDATE `repository` SET num_pulls = 0 WHERE num_pulls IS NULL"); err != nil {
return err
}

if _, err := sess.Exec("UPDATE `repository` SET num_closed_pulls = 0 WHERE num_closed_pulls IS NULL"); err != nil {
return err
}

if err := sess.Sync2(new(RepoV17)); err != nil {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had assumed that calling Sync2(..) would add the new NOT NULL constraints to the table, but it doesn't seem to be working.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. It will not.

Copy link
Member Author

@ethantkoenig ethantkoenig Feb 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what should I do?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have to write SQL yourself.

return err
}

return sess.Commit()
}
14 changes: 7 additions & 7 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ type Repository struct {
Website string
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"`
Expand Down