Skip to content

Commit

Permalink
Unify DB tables/columns (#3806)
Browse files Browse the repository at this point in the history
Co-authored-by: Anbraten <6918444+anbraten@users.noreply.github.com>
  • Loading branch information
qwerty287 and anbraten authored Jun 27, 2024
1 parent b8b6efb commit 92cd0d0
Show file tree
Hide file tree
Showing 41 changed files with 1,005 additions and 260 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ test-cli: ## Test cli code

test-server-datastore: ## Test server datastore
go test -timeout 120s -tags 'test $(TAGS)' -run TestMigrate go.woodpecker-ci.org/woodpecker/v2/server/store/...
go test -race -timeout 30s -tags 'test $(TAGS)' -skip TestMigrate go.woodpecker-ci.org/woodpecker/v2/server/store/...
go test -race -timeout 45s -tags 'test $(TAGS)' -skip TestMigrate go.woodpecker-ci.org/woodpecker/v2/server/store/...

test-server-datastore-coverage: ## Test server datastore with coverage report
go test -race -cover -coverprofile datastore-coverage.out -timeout 180s -tags 'test $(TAGS)' go.woodpecker-ci.org/woodpecker/v2/server/store/...
Expand Down
9 changes: 9 additions & 0 deletions cmd/server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4014,6 +4014,7 @@ const docTemplate = `{
"type": "string"
},
"created_at": {
"description": "TODO change JSON field to \"created\" in 3.0",
"type": "integer"
},
"creator_id": {
Expand Down Expand Up @@ -4056,12 +4057,14 @@ const docTemplate = `{
"type": "string"
},
"created_at": {
"description": "TODO change JSON field to \"created\" in 3.0",
"type": "integer"
},
"event": {
"type": "string"
},
"finished_at": {
"description": "TODO change JSON field to \"finished\" in 3.0",
"type": "integer"
},
"id": {
Expand All @@ -4083,6 +4086,7 @@ const docTemplate = `{
"type": "integer"
},
"started_at": {
"description": "TODO change JSON field to \"started\" in 3.0",
"type": "integer"
},
"status": {
Expand Down Expand Up @@ -4240,6 +4244,7 @@ const docTemplate = `{
"type": "string"
},
"created_at": {
"description": "TODO change JSON field to \"created\" in 3.0",
"type": "integer"
},
"deploy_task": {
Expand All @@ -4258,6 +4263,7 @@ const docTemplate = `{
"$ref": "#/definitions/WebhookEvent"
},
"finished_at": {
"description": "TODO change JSON field to \"finished\" in 3.0",
"type": "integer"
},
"forge_url": {
Expand Down Expand Up @@ -4291,6 +4297,7 @@ const docTemplate = `{
"type": "string"
},
"reviewed_at": {
"description": "TODO change JSON field to \"reviewed\" in 3.0",
"type": "integer"
},
"reviewed_by": {
Expand All @@ -4301,6 +4308,7 @@ const docTemplate = `{
"type": "string"
},
"started_at": {
"description": "TODO change JSON field to \"started\" in 3.0",
"type": "integer"
},
"status": {
Expand All @@ -4313,6 +4321,7 @@ const docTemplate = `{
"type": "string"
},
"updated_at": {
"description": "TODO change JSON field to \"updated\" in 3.0",
"type": "integer"
},
"variables": {
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/92-development/03-ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ The following list contains some tools and frameworks used by the Woodpecker UI.
Woodpecker uses [Vue I18n](https://vue-i18n.intlify.dev/) as translation library. New translations have to be added to `web/src/assets/locales/en.json`. The English source file will be automatically imported into [Weblate](https://translate.woodpecker-ci.org/) (the translation system used by Woodpecker) where all other languages will be translated by the community based on the English source.
You must not provide translations except English in PRs, otherwise weblate could put git into conflicts (when someone has translated in that language file and changes are not into main branch yet)

For more information about translations see [Translations](./07-translations.md).
For more information about translations see [Translations](./08-translations.md).
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`.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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"`
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"` // TODO change JSON field to "started" in 3.0
Finished int64 `json:"finished_at,omitempty" xorm:"pipeline_finished"` // TODO change JSON field to "finished" in 3.0
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"` // TODO change JSON field to "started" in 3.0
Finished int64 `json:"finished_at" xorm:"finished"` // TODO change JSON field to "finished" in 3.0
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"` // TODO change JSON field to "reviewed" in 3.0
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

0 comments on commit 92cd0d0

Please sign in to comment.