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

Unify DB tables/columns #3806

Merged
merged 17 commits into from
Jun 27, 2024
7 changes: 7 additions & 0 deletions docs/docs/92-development/06-conventions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Conventions

## Database naming

Database tables are named plural, columns don't have any prefix.

Example: Table name `agent`, columns `id`, `name`.
18 changes: 13 additions & 5 deletions server/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ package model

// Config represents a pipeline configuration.
type Config struct {
ID int64 `json:"-" xorm:"pk autoincr 'config_id'"`
RepoID int64 `json:"-" xorm:"UNIQUE(s) 'config_repo_id'"`
Hash string `json:"hash" xorm:"UNIQUE(s) 'config_hash'"`
Name string `json:"name" xorm:"UNIQUE(s) 'config_name'"`
Data []byte `json:"data" xorm:"LONGBLOB 'config_data'"`
ID int64 `json:"-" xorm:"pk autoincr 'id'"`
RepoID int64 `json:"-" xorm:"UNIQUE(s) 'repo_id'"`
Hash string `json:"hash" xorm:"UNIQUE(s) 'hash'"`
Name string `json:"name" xorm:"UNIQUE(s) 'name'"`
Data []byte `json:"data" xorm:"LONGBLOB 'data'"`
} // @name Config

func (Config) TableName() string {
return "configs"
}

// PipelineConfig is the n:n relation between Pipeline and Config.
type PipelineConfig struct {
ConfigID int64 `json:"-" xorm:"UNIQUE(s) NOT NULL 'config_id'"`
PipelineID int64 `json:"-" xorm:"UNIQUE(s) NOT NULL 'pipeline_id'"`
}

func (PipelineConfig) TableName() string {
return "pipeline_configs"
}
10 changes: 5 additions & 5 deletions server/model/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
)

type Cron struct {
ID int64 `json:"id" xorm:"pk autoincr"`
Name string `json:"name" xorm:"UNIQUE(s) INDEX"`
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
Name string `json:"name" xorm:"name UNIQUE(s) INDEX"`
RepoID int64 `json:"repo_id" xorm:"repo_id UNIQUE(s) INDEX"`
CreatorID int64 `json:"creator_id" xorm:"creator_id INDEX"`
NextExec int64 `json:"next_exec"`
Schedule string `json:"schedule" xorm:"NOT NULL"` // @weekly, 3min, ...
NextExec int64 `json:"next_exec" xorm:"next_exec"`
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
Schedule string `json:"schedule" xorm:"schedule NOT NULL"` // @weekly, 3min, ...
Created int64 `json:"created_at" xorm:"created NOT NULL DEFAULT 0"`
Branch string `json:"branch"`
Branch string `json:"branch" xorm:"branch"`
} // @name Cron

// TableName returns the database table name for xorm.
Expand Down
34 changes: 17 additions & 17 deletions server/model/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ package model

// Feed represents an item in the user's feed or timeline.
type Feed struct {
RepoID int64 `json:"repo_id" xorm:"feed_repo_id"`
ID int64 `json:"id,omitempty" xorm:"feed_pipeline_id"`
Number int64 `json:"number,omitempty" xorm:"feed_pipeline_number"`
Event string `json:"event,omitempty" xorm:"feed_pipeline_event"`
Status string `json:"status,omitempty" xorm:"feed_pipeline_status"`
Created int64 `json:"created_at,omitempty" xorm:"feed_pipeline_created"`
Started int64 `json:"started_at,omitempty" xorm:"feed_pipeline_started"`
Finished int64 `json:"finished_at,omitempty" xorm:"feed_pipeline_finished"`
Commit string `json:"commit,omitempty" xorm:"feed_pipeline_commit"`
Branch string `json:"branch,omitempty" xorm:"feed_pipeline_branch"`
Ref string `json:"ref,omitempty" xorm:"feed_pipeline_ref"`
Refspec string `json:"refspec,omitempty" xorm:"feed_pipeline_refspec"`
Title string `json:"title,omitempty" xorm:"feed_pipeline_title"`
Message string `json:"message,omitempty" xorm:"feed_pipeline_message"`
Author string `json:"author,omitempty" xorm:"feed_pipeline_author"`
Avatar string `json:"author_avatar,omitempty" xorm:"feed_pipeline_avatar"`
Email string `json:"author_email,omitempty" xorm:"feed_pipeline_email"`
RepoID int64 `json:"repo_id" xorm:"repo_id"`
anbraten marked this conversation as resolved.
Show resolved Hide resolved
ID int64 `json:"id,omitempty" xorm:"pipeline_id"`
Number int64 `json:"number,omitempty" xorm:"pipeline_number"`
Event string `json:"event,omitempty" xorm:"pipeline_event"`
Status string `json:"status,omitempty" xorm:"pipeline_status"`
Created int64 `json:"created_at,omitempty" xorm:"pipeline_created"`
Started int64 `json:"started_at,omitempty" xorm:"pipeline_started"`
Finished int64 `json:"finished_at,omitempty" xorm:"pipeline_finished"`
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
Commit string `json:"commit,omitempty" xorm:"pipeline_commit"`
Branch string `json:"branch,omitempty" xorm:"pipeline_branch"`
Ref string `json:"ref,omitempty" xorm:"pipeline_ref"`
Refspec string `json:"refspec,omitempty" xorm:"pipeline_refspec"`
Title string `json:"title,omitempty" xorm:"pipeline_title"`
Message string `json:"message,omitempty" xorm:"pipeline_message"`
Author string `json:"author,omitempty" xorm:"pipeline_author"`
Avatar string `json:"author_avatar,omitempty" xorm:"pipeline_avatar"`
Email string `json:"author_email,omitempty" xorm:"pipeline_email"`
} // @name Feed
5 changes: 5 additions & 0 deletions server/model/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ type Forge struct {
AdditionalOptions map[string]any `json:"additional_options,omitempty" xorm:"json"`
} // @name Forge

// TableName returns the database table name for xorm.
func (Forge) TableName() string {
return "forges"
}

// PublicCopy returns a copy of the forge without sensitive information and technical details.
func (f *Forge) PublicCopy() *Forge {
forge := &Forge{
Expand Down
12 changes: 6 additions & 6 deletions server/model/perm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package model

// Perm defines a repository permission for an individual user.
type Perm struct {
UserID int64 `json:"-" xorm:"UNIQUE(s) INDEX NOT NULL 'perm_user_id'"`
RepoID int64 `json:"-" xorm:"UNIQUE(s) INDEX NOT NULL 'perm_repo_id'"`
UserID int64 `json:"-" xorm:"UNIQUE(s) INDEX NOT NULL 'user_id'"`
RepoID int64 `json:"-" xorm:"UNIQUE(s) INDEX NOT NULL 'repo_id'"`
Repo *Repo `json:"-" xorm:"-"`
Pull bool `json:"pull" xorm:"perm_pull"`
Push bool `json:"push" xorm:"perm_push"`
Admin bool `json:"admin" xorm:"perm_admin"`
Synced int64 `json:"synced" xorm:"perm_synced"`
Pull bool `json:"pull" xorm:"pull"`
Push bool `json:"push" xorm:"push"`
Admin bool `json:"admin" xorm:"admin"`
Synced int64 `json:"synced" xorm:"synced"`
Created int64 `json:"created" xorm:"created"`
Updated int64 `json:"updated" xorm:"updated"`
} // @name Perm
Expand Down
64 changes: 32 additions & 32 deletions server/model/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,50 @@ import (
)

type Pipeline struct {
ID int64 `json:"id" xorm:"pk autoincr 'pipeline_id'"`
RepoID int64 `json:"-" xorm:"UNIQUE(s) INDEX 'pipeline_repo_id'"`
Number int64 `json:"number" xorm:"UNIQUE(s) 'pipeline_number'"`
Author string `json:"author" xorm:"INDEX 'pipeline_author'"`
Parent int64 `json:"parent" xorm:"pipeline_parent"`
Event WebhookEvent `json:"event" xorm:"pipeline_event"`
Status StatusValue `json:"status" xorm:"INDEX 'pipeline_status'"`
Errors []*types.PipelineError `json:"errors" xorm:"json 'pipeline_errors'"`
Created int64 `json:"created_at" xorm:"pipeline_created"`
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
RepoID int64 `json:"-" xorm:"UNIQUE(s) INDEX 'repo_id'"`
Number int64 `json:"number" xorm:"UNIQUE(s) 'number'"`
Author string `json:"author" xorm:"INDEX 'author'"`
Parent int64 `json:"parent" xorm:"parent"`
Event WebhookEvent `json:"event" xorm:"event"`
Status StatusValue `json:"status" xorm:"INDEX 'status'"`
Errors []*types.PipelineError `json:"errors" xorm:"json 'errors'"`
Created int64 `json:"created_at" xorm:"created NOT NULL DEFAULT 0 'created'"`
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
Updated int64 `json:"updated_at" xorm:"updated NOT NULL DEFAULT 0 'updated'"`
Started int64 `json:"started_at" xorm:"pipeline_started"`
Finished int64 `json:"finished_at" xorm:"pipeline_finished"`
Deploy string `json:"deploy_to" xorm:"pipeline_deploy"`
DeployTask string `json:"deploy_task" xorm:"pipeline_deploy_task"`
Commit string `json:"commit" xorm:"pipeline_commit"`
Branch string `json:"branch" xorm:"pipeline_branch"`
Ref string `json:"ref" xorm:"pipeline_ref"`
Refspec string `json:"refspec" xorm:"pipeline_refspec"`
Title string `json:"title" xorm:"pipeline_title"`
Message string `json:"message" xorm:"TEXT 'pipeline_message'"`
Timestamp int64 `json:"timestamp" xorm:"pipeline_timestamp"`
Sender string `json:"sender" xorm:"pipeline_sender"` // uses reported user for webhooks and name of cron for cron pipelines
Avatar string `json:"author_avatar" xorm:"pipeline_avatar"`
Email string `json:"author_email" xorm:"pipeline_email"`
ForgeURL string `json:"forge_url" xorm:"pipeline_forge_url"`
Reviewer string `json:"reviewed_by" xorm:"pipeline_reviewer"`
Reviewed int64 `json:"reviewed_at" xorm:"pipeline_reviewed"`
Started int64 `json:"started_at" xorm:"started"`
Finished int64 `json:"finished_at" xorm:"finished"`
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
Deploy string `json:"deploy_to" xorm:"deploy"`
DeployTask string `json:"deploy_task" xorm:"deploy_task"`
Commit string `json:"commit" xorm:"commit"`
Branch string `json:"branch" xorm:"branch"`
Ref string `json:"ref" xorm:"ref"`
Refspec string `json:"refspec" xorm:"refspec"`
Title string `json:"title" xorm:"title"`
Message string `json:"message" xorm:"TEXT 'message'"`
Timestamp int64 `json:"timestamp" xorm:"timestamp"`
Sender string `json:"sender" xorm:"sender"` // uses reported user for webhooks and name of cron for cron pipelines
Avatar string `json:"author_avatar" xorm:"avatar"`
Email string `json:"author_email" xorm:"email"`
ForgeURL string `json:"forge_url" xorm:"forge_url"`
Reviewer string `json:"reviewed_by" xorm:"reviewer"`
Reviewed int64 `json:"reviewed_at" xorm:"reviewed"`
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
Workflows []*Workflow `json:"workflows,omitempty" xorm:"-"`
ChangedFiles []string `json:"changed_files,omitempty" xorm:"LONGTEXT 'changed_files'"`
AdditionalVariables map[string]string `json:"variables,omitempty" xorm:"json 'additional_variables'"`
PullRequestLabels []string `json:"pr_labels,omitempty" xorm:"json 'pr_labels'"`
IsPrerelease bool `json:"is_prerelease,omitempty" xorm:"is_prerelease"`
IsPrerelease bool `json:"is_prerelease,omitempty" xorm:"is_prerelease"`
} // @name Pipeline

type PipelineFilter struct {
Before int64
After int64
}

// TableName return database table name for xorm.
func (Pipeline) TableName() string {
return "pipelines"
}

type PipelineFilter struct {
Before int64
After int64
}

// IsMultiPipeline checks if step list contain more than one parent step.
func (p Pipeline) IsMultiPipeline() bool {
return len(p.Workflows) > 1
Expand Down
2 changes: 1 addition & 1 deletion server/model/redirection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package model

type Redirection struct {
ID int64 `xorm:"pk autoincr 'redirection_id'"`
ID int64 `xorm:"pk autoincr 'id'"`
RepoID int64 `xorm:"'repo_id'"`
FullName string `xorm:"UNIQUE INDEX 'repo_full_name'"`
}
Expand Down
14 changes: 9 additions & 5 deletions server/model/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ var (

// Registry represents a docker registry with credentials.
type Registry struct {
ID int64 `json:"id" xorm:"pk autoincr 'registry_id'"`
RepoID int64 `json:"-" xorm:"UNIQUE(s) INDEX 'registry_repo_id'"`
Address string `json:"address" xorm:"UNIQUE(s) INDEX 'registry_addr'"`
Username string `json:"username" xorm:"varchar(2000) 'registry_username'"`
Password string `json:"password" xorm:"TEXT 'registry_password'"`
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
RepoID int64 `json:"-" xorm:"UNIQUE(s) INDEX 'repo_id'"`
Address string `json:"address" xorm:"UNIQUE(s) INDEX 'address'"`
Username string `json:"username" xorm:"varchar(2000) 'username'"`
Password string `json:"password" xorm:"TEXT 'password'"`
} // @name Registry

func (r Registry) TableName() string {
return "registries"
}

// Validate validates the registry information.
func (r *Registry) Validate() error {
switch {
Expand Down
46 changes: 23 additions & 23 deletions server/model/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@ import (

// Repo represents a repository.
type Repo struct {
ID int64 `json:"id,omitempty" xorm:"pk autoincr 'repo_id'"`
UserID int64 `json:"-" xorm:"repo_user_id"`
ID int64 `json:"id,omitempty" xorm:"pk autoincr 'id'"`
UserID int64 `json:"-" xorm:"user_id"`
ForgeID int64 `json:"forge_id,omitempty" xorm:"forge_id"`
// ForgeRemoteID is the unique identifier for the repository on the forge.
ForgeRemoteID ForgeRemoteID `json:"forge_remote_id" xorm:"forge_remote_id"`
OrgID int64 `json:"org_id" xorm:"repo_org_id"`
Owner string `json:"owner" xorm:"UNIQUE(name) 'repo_owner'"`
Name string `json:"name" xorm:"UNIQUE(name) 'repo_name'"`
FullName string `json:"full_name" xorm:"UNIQUE 'repo_full_name'"`
Avatar string `json:"avatar_url,omitempty" xorm:"varchar(500) 'repo_avatar'"`
ForgeURL string `json:"forge_url,omitempty" xorm:"varchar(1000) 'repo_forge_url'"`
Clone string `json:"clone_url,omitempty" xorm:"varchar(1000) 'repo_clone'"`
CloneSSH string `json:"clone_url_ssh" xorm:"varchar(1000) 'repo_clone_ssh'"`
Branch string `json:"default_branch,omitempty" xorm:"varchar(500) 'repo_branch'"`
SCMKind SCMKind `json:"scm,omitempty" xorm:"varchar(50) 'repo_scm'"`
PREnabled bool `json:"pr_enabled" xorm:"DEFAULT TRUE 'repo_pr_enabled'"`
Timeout int64 `json:"timeout,omitempty" xorm:"repo_timeout"`
Visibility RepoVisibility `json:"visibility" xorm:"varchar(10) 'repo_visibility'"`
IsSCMPrivate bool `json:"private" xorm:"repo_private"`
IsTrusted bool `json:"trusted" xorm:"repo_trusted"`
IsGated bool `json:"gated" xorm:"repo_gated"`
IsActive bool `json:"active" xorm:"repo_active"`
AllowPull bool `json:"allow_pr" xorm:"repo_allow_pr"`
AllowDeploy bool `json:"allow_deploy" xorm:"repo_allow_deploy"`
Config string `json:"config_file" xorm:"varchar(500) 'repo_config_path'"`
Hash string `json:"-" xorm:"varchar(500) 'repo_hash'"`
OrgID int64 `json:"org_id" xorm:"org_id"`
Owner string `json:"owner" xorm:"UNIQUE(name) 'owner'"`
Name string `json:"name" xorm:"UNIQUE(name) 'name'"`
FullName string `json:"full_name" xorm:"UNIQUE 'full_name'"`
Avatar string `json:"avatar_url,omitempty" xorm:"varchar(500) 'avatar'"`
ForgeURL string `json:"forge_url,omitempty" xorm:"varchar(1000) 'forge_url'"`
Clone string `json:"clone_url,omitempty" xorm:"varchar(1000) 'clone'"`
CloneSSH string `json:"clone_url_ssh" xorm:"varchar(1000) 'clone_ssh'"`
Branch string `json:"default_branch,omitempty" xorm:"varchar(500) 'branch'"`
SCMKind SCMKind `json:"scm,omitempty" xorm:"varchar(50) 'scm'"`
PREnabled bool `json:"pr_enabled" xorm:"DEFAULT TRUE 'pr_enabled'"`
Timeout int64 `json:"timeout,omitempty" xorm:"timeout"`
Visibility RepoVisibility `json:"visibility" xorm:"varchar(10) 'visibility'"`
IsSCMPrivate bool `json:"private" xorm:"private"`
IsTrusted bool `json:"trusted" xorm:"trusted"`
IsGated bool `json:"gated" xorm:"gated"`
IsActive bool `json:"active" xorm:"active"`
AllowPull bool `json:"allow_pr" xorm:"allow_pr"`
AllowDeploy bool `json:"allow_deploy" xorm:"allow_deploy"`
Config string `json:"config_file" xorm:"varchar(500) 'config_path'"`
Hash string `json:"-" xorm:"varchar(500) 'hash'"`
Perm *Perm `json:"-" xorm:"-"`
CancelPreviousPipelineEvents []WebhookEvent `json:"cancel_previous_pipeline_events" xorm:"json 'cancel_previous_pipeline_events'"`
NetrcOnlyTrusted bool `json:"netrc_only_trusted" xorm:"NOT NULL DEFAULT true 'netrc_only_trusted'"`
Expand Down
Loading