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"
}
12 changes: 6 additions & 6 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, ...
Created int64 `json:"created_at" xorm:"created NOT NULL DEFAULT 0"`
Branch string `json:"branch"`
NextExec int64 `json:"next_exec" xorm:"next_exec"`
Schedule string `json:"schedule" xorm:"schedule NOT NULL"` // @weekly, 3min, ...
Created int64 `json:"created_at" xorm:"created NOT NULL DEFAULT 0"` // TODO change JSON field to "created" in 3.0
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"` // TODO change JSON field to "created" in 3.0
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
6 changes: 3 additions & 3 deletions server/model/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ const (
type LogEntry struct {
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
StepID int64 `json:"step_id" xorm:"INDEX 'step_id'"`
Time int64 `json:"time"`
Line int `json:"line"`
Time int64 `json:"time" xorm:"'time'"`
Line int `json:"line" xorm:"'line'"`
Data []byte `json:"data" xorm:"LONGBLOB"`
Created int64 `json:"-" xorm:"created"`
Type LogEntryType `json:"type"`
Type LogEntryType `json:"type" xorm:"'type'"`
} // @name LogEntry

// TODO: store info what specific command the line belongs to (must be optional and impl. by backend)
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
66 changes: 33 additions & 33 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"`
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"`
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'"` // TODO change JSON field to "created" in 3.0
Updated int64 `json:"updated_at" xorm:"updated NOT NULL DEFAULT 0 'updated'"` // TODO change JSON field to "updated" in 3.0
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
Loading