delete owner_id column from table repository

This commit is contained in:
Wenkai Yin 2017-04-27 18:19:28 +08:00
parent eb39fbf814
commit 4f9d9ed5d8
17 changed files with 182 additions and 222 deletions

View File

@ -116,7 +116,6 @@ create table repository (
repository_id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
project_id int NOT NULL,
owner_id int NOT NULL,
description text,
pull_count int DEFAULT 0 NOT NULL,
star_count int DEFAULT 0 NOT NULL,

View File

@ -113,13 +113,11 @@ create table repository (
repository_id INTEGER PRIMARY KEY,
name varchar(255) NOT NULL,
project_id int NOT NULL,
owner_id int NOT NULL,
description text,
pull_count int DEFAULT 0 NOT NULL,
star_count int DEFAULT 0 NOT NULL,
creation_time timestamp default CURRENT_TIMESTAMP,
update_time timestamp default CURRENT_TIMESTAMP,
FOREIGN KEY (owner_id) REFERENCES user(user_id),
FOREIGN KEY (project_id) REFERENCES project(project_id),
UNIQUE (name)
);

View File

@ -215,24 +215,6 @@ func GetRecentLogs(userID, linesNum int, startTime, endTime string) ([]models.Ac
return logs, nil
}
// GetAccessLogCreator ...
func GetAccessLogCreator(repoName string) (string, error) {
o := GetOrmer()
sql := "select * from user where user_id = (select user_id from access_log where operation = 'push' and repo_name = ? order by op_time desc limit 1)"
var u []models.User
n, err := o.Raw(sql, repoName).QueryRows(&u)
if err != nil {
return "", err
}
if n == 0 {
return "", nil
}
return u[0].Username, nil
}
// CountPull ...
func CountPull(repoName string) (int64, error) {
o := GetOrmer()

View File

@ -192,7 +192,7 @@ func testForMySQL(m *testing.M) int {
},
}
log.Infof("MYSQL_HOST: %s, MYSQL_USR: %s, MYSQL_PORT: %s, MYSQL_PWD: %s\n", dbHost, dbUser, dbPort, dbPassword)
log.Infof("MYSQL_HOST: %s, MYSQL_USR: %s, MYSQL_PORT: %d, MYSQL_PWD: %s\n", dbHost, dbUser, dbPort, dbPassword)
return testForAll(m, database)
}
@ -650,26 +650,6 @@ func TestAccessLog(t *testing.T) {
}
}
func TestGetAccessLogCreator(t *testing.T) {
var err error
err = AccessLog(currentUser.Username, currentProject.Name, currentProject.Name+"/tomcat", repoTag2, "push")
if err != nil {
t.Errorf("Error occurred in AccessLog: %v", err)
}
err = AccessLog(currentUser.Username, currentProject.Name, currentProject.Name+"/tomcat", repoTag2, "push")
if err != nil {
t.Errorf("Error occurred in AccessLog: %v", err)
}
user, err := GetAccessLogCreator(currentProject.Name + "/tomcat")
if err != nil {
t.Errorf("Error occurred in GetAccessLogCreator: %v", err)
}
if user != currentUser.Username {
t.Errorf("The access log creator does not match, expected: %s, actual: %s", currentUser.Username, user)
}
}
func TestCountPull(t *testing.T) {
var err error
err = AccessLog(currentUser.Username, currentProject.Name, currentProject.Name+"/tomcat", repoTag2, "pull")
@ -1595,8 +1575,7 @@ func TestGetOrmer(t *testing.T) {
func TestAddRepository(t *testing.T) {
repoRecord := models.RepoRecord{
Name: currentProject.Name + "/" + repositoryName,
OwnerName: currentUser.Username,
ProjectName: currentProject.Name,
ProjectID: currentProject.ProjectID,
Description: "testing repo",
PullCount: 0,
StarCount: 0,

View File

@ -24,13 +24,15 @@ import (
// AddRepository adds a repo to the database.
func AddRepository(repo models.RepoRecord) error {
o := GetOrmer()
sql := "insert into repository (owner_id, project_id, name, description, pull_count, star_count, creation_time, update_time) " +
"select (select user_id as owner_id from user where username=?), " +
"(select project_id as project_id from project where name=?), ?, ?, ?, ?, ?, NULL "
if repo.ProjectID == 0 {
return fmt.Errorf("invalid project ID: %d", repo.ProjectID)
}
_, err := o.Raw(sql, repo.OwnerName, repo.ProjectName, repo.Name, repo.Description,
repo.PullCount, repo.StarCount, time.Now()).Exec()
o := GetOrmer()
now := time.Now()
repo.CreationTime = now
repo.UpdateTime = now
_, err := o.Insert(&repo)
return err
}
@ -104,7 +106,7 @@ func GetRepositoryByProjectName(name string) ([]*models.RepoRecord, error) {
//GetTopRepos returns the most popular repositories
func GetTopRepos(userID int, count int) ([]*models.RepoRecord, error) {
sql :=
`select r.repository_id, r.name, r.owner_id,
`select r.repository_id, r.name,
r.project_id, r.description, r.pull_count,
r.star_count, r.creation_time, r.update_time
from repository r

View File

@ -27,9 +27,8 @@ var (
project = "library"
name = "library/repository-test"
repository = &models.RepoRecord{
Name: name,
OwnerName: "admin",
ProjectName: project,
Name: name,
ProjectID: 1,
}
)
@ -214,9 +213,8 @@ func TestGetTopRepos(t *testing.T) {
require.NoError(err)
repository1 := &models.RepoRecord{
Name: fmt.Sprintf("%v/repository1", project1.Name),
OwnerName: admin.Username,
ProjectName: project1.Name,
Name: fmt.Sprintf("%v/repository1", project1.Name),
ProjectID: project1.ProjectID,
}
err = AddRepository(*repository1)
require.NoError(err)
@ -225,9 +223,8 @@ func TestGetTopRepos(t *testing.T) {
require.NoError(err)
repository2 := &models.RepoRecord{
Name: fmt.Sprintf("%v/repository2", project1.Name),
OwnerName: admin.Username,
ProjectName: project1.Name,
Name: fmt.Sprintf("%v/repository2", project1.Name),
ProjectID: project1.ProjectID,
}
err = AddRepository(*repository2)
require.NoError(err)
@ -237,9 +234,8 @@ func TestGetTopRepos(t *testing.T) {
require.NoError(err)
repository3 := &models.RepoRecord{
Name: fmt.Sprintf("%v/repository3", project2.Name),
OwnerName: admin.Username,
ProjectName: project2.Name,
Name: fmt.Sprintf("%v/repository3", project2.Name),
ProjectID: project2.ProjectID,
}
err = AddRepository(*repository3)
require.NoError(err)
@ -259,9 +255,8 @@ func TestGetTopRepos(t *testing.T) {
deletedPublicProject.ProjectID, err = AddProject(deletedPublicProject)
require.NoError(err)
deletedPublicRepository1 := &models.RepoRecord{
Name: fmt.Sprintf("%v/repository1", deletedPublicProject.Name),
OwnerName: admin.Username,
ProjectName: deletedPublicProject.Name,
Name: fmt.Sprintf("%v/repository1", deletedPublicProject.Name),
ProjectID: deletedPublicProject.ProjectID,
}
err = AddRepository(*deletedPublicRepository1)
require.NoError(err)
@ -303,9 +298,8 @@ func TestGetTotalOfRepositoriesByProject(t *testing.T) {
}
if err := addRepository(&models.RepoRecord{
Name: repoName,
OwnerName: "admin",
ProjectName: "library",
Name: repoName,
ProjectID: projectID,
}); err != nil {
t.Errorf("failed to add repository %s: %v", repoName, err)
return
@ -333,9 +327,8 @@ func TestGetRepositoriesByProject(t *testing.T) {
repoName := "library/repository"
if err := addRepository(&models.RepoRecord{
Name: repoName,
OwnerName: "admin",
ProjectName: "library",
Name: repoName,
ProjectID: projectID,
}); err != nil {
t.Errorf("failed to add repository %s: %v", repoName, err)
return

View File

@ -20,7 +20,7 @@ import (
// AccessLog holds information about logs which are used to record the actions that user take to the resourses.
type AccessLog struct {
LogID int `orm:"pk;column(log_id)" json:"log_id"`
LogID int `orm:"pk;auto;column(log_id)" json:"log_id"`
UserID int `orm:"column(user_id)" json:"user_id"`
ProjectID int64 `orm:"column(project_id)" json:"project_id"`
RepoName string `orm:"column(repo_name)" json:"repo_name"`

View File

@ -20,7 +20,7 @@ import (
// Project holds the details of a project.
type Project struct {
ProjectID int64 `orm:"pk;column(project_id)" json:"project_id"`
ProjectID int64 `orm:"pk;auto;column(project_id)" json:"project_id"`
OwnerID int `orm:"column(owner_id)" json:"owner_id"`
Name string `orm:"column(name)" json:"name"`
CreationTime time.Time `orm:"column(creation_time)" json:"creation_time"`

View File

@ -48,7 +48,7 @@ const (
// RepPolicy is the model for a replication policy, which associate to a project and a target (destination)
type RepPolicy struct {
ID int64 `orm:"column(id)" json:"id"`
ID int64 `orm:"pk;auto;column(id)" json:"id"`
ProjectID int64 `orm:"column(project_id)" json:"project_id"`
ProjectName string `json:"project_name,omitempty"`
TargetID int64 `orm:"column(target_id)" json:"target_id"`
@ -95,7 +95,7 @@ func (r *RepPolicy) Valid(v *validation.Validation) {
// RepJob is the model for a replication job, which is the execution unit on job service, currently it is used to transfer/remove
// a repository to/from a remote registry instance.
type RepJob struct {
ID int64 `orm:"column(id)" json:"id"`
ID int64 `orm:"pk;auto;column(id)" json:"id"`
Status string `orm:"column(status)" json:"status"`
Repository string `orm:"column(repository)" json:"repository"`
PolicyID int64 `orm:"column(policy_id)" json:"policy_id"`
@ -109,7 +109,7 @@ type RepJob struct {
// RepTarget is the model for a replication targe, i.e. destination, which wraps the endpoint URL and username/password of a remote registry.
type RepTarget struct {
ID int64 `orm:"column(id)" json:"id"`
ID int64 `orm:"pk;auto;column(id)" json:"id"`
URL string `orm:"column(url)" json:"endpoint"`
Name string `orm:"column(name)" json:"name"`
Username string `orm:"column(username)" json:"username"`

View File

@ -20,11 +20,8 @@ import (
// RepoRecord holds the record of an repository in DB, all the infors are from the registry notification event.
type RepoRecord struct {
RepositoryID string `orm:"column(repository_id);pk" json:"repository_id"`
RepositoryID int64 `orm:"pk;auto;column(repository_id)" json:"repository_id"`
Name string `orm:"column(name)" json:"name"`
OwnerName string `orm:"-"`
OwnerID int64 `orm:"column(owner_id)" json:"owner_id"`
ProjectName string `orm:"-"`
ProjectID int64 `orm:"column(project_id)" json:"project_id"`
Description string `orm:"column(description)" json:"description"`
PullCount int64 `orm:"column(pull_count)" json:"pull_count"`

View File

@ -25,7 +25,7 @@ const (
// Role holds the details of a role.
type Role struct {
RoleID int `orm:"pk;column(role_id)" json:"role_id"`
RoleID int `orm:"pk;auto;column(role_id)" json:"role_id"`
RoleCode string `orm:"column(role_code)" json:"role_code"`
Name string `orm:"column(name)" json:"role_name"`

View File

@ -20,7 +20,7 @@ import (
// User holds the details of a user.
type User struct {
UserID int `orm:"pk;column(user_id)" json:"user_id"`
UserID int `orm:"pk;auto;column(user_id)" json:"user_id"`
Username string `orm:"column(username)" json:"username"`
Email string `orm:"column(email)" json:"email"`
Password string `orm:"column(password)" json:"password"`

View File

@ -124,11 +124,8 @@ func CommonPolicyEabled(policyID int, enabled int) {
func CommonAddRepository() {
commonRepository := &models.RepoRecord{
RepositoryID: "1",
RepositoryID: 1,
Name: TestRepoName,
OwnerName: AdminName,
OwnerID: 1,
ProjectName: DefaultProjectName,
ProjectID: 1,
PullCount: 1,
}

View File

@ -45,9 +45,8 @@ type RepositoryAPI struct {
}
type repoResp struct {
ID string `json:"id"`
ID int64 `json:"id"`
Name string `json:"name"`
OwnerID int64 `json:"owner_id"`
ProjectID int64 `json:"project_id"`
Description string `json:"description"`
PullCount int64 `json:"pull_count"`
@ -147,7 +146,6 @@ func populateTagsCount(repositories []*models.RepoRecord) ([]*repoResp, error) {
repo := &repoResp{
ID: repository.RepositoryID,
Name: repository.Name,
OwnerID: repository.OwnerID,
ProjectID: repository.ProjectID,
Description: repository.Description,
PullCount: repository.PullCount,

View File

@ -270,18 +270,21 @@ func SyncRegistry() error {
log.Debugf("Start adding repositories into DB... ")
for _, repoToAdd := range reposToAdd {
project, _ := utils.ParseRepository(repoToAdd)
user, err := dao.GetAccessLogCreator(repoToAdd)
if err != nil {
log.Errorf("Error happens when getting the repository owner from access log: %v", err)
}
if len(user) == 0 {
user = "anonymous"
}
pullCount, err := dao.CountPull(repoToAdd)
if err != nil {
log.Errorf("Error happens when counting pull count from access log: %v", err)
}
repoRecord := models.RepoRecord{Name: repoToAdd, OwnerName: user, ProjectName: project, PullCount: pullCount}
pro, err := dao.GetProjectByName(project)
if err != nil {
log.Errorf("failed to get project %s: %v", project, err)
continue
}
repoRecord := models.RepoRecord{
Name: repoToAdd,
ProjectID: pro.ProjectID,
PullCount: pullCount,
}
if err := dao.AddRepository(repoRecord); err != nil {
log.Errorf("Error happens when adding the missing repository: %v", err)
} else {

View File

@ -76,7 +76,15 @@ func (n *NotificationHandler) Post() {
return
}
log.Debugf("Add repository %s into DB.", repository)
repoRecord := models.RepoRecord{Name: repository, OwnerName: user, ProjectName: project}
pro, err := dao.GetProjectByName(project)
if err != nil {
log.Errorf("failed to get project %s: %v", project, err)
return
}
repoRecord := models.RepoRecord{
Name: repository,
ProjectID: pro.ProjectID,
}
if err := dao.AddRepository(repoRecord); err != nil {
log.Errorf("Error happens when adding repository: %v", err)
}

View File

@ -37,3 +37,7 @@ Changelog for harbor database schema
- alter column `name` on table `project`: varchar(30)->varchar(41)
- create table `repository`
- alter column `password` on table `replication_target`: varchar(40)->varchar(128)
## 1.2.0
- delete column `owner_id` from table `repository`