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

Inline model types for migrations #4293

Merged
merged 7 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
14 changes: 5 additions & 9 deletions server/store/datastore/migration/000_legacy_to_xormigrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
"xorm.io/xorm"
)

type v000Migrations struct {
Name string `xorm:"UNIQUE"`
}

func (m *v000Migrations) TableName() string {
return "migrations"
}

var legacyToXormigrate = xormigrate.Migration{
ID: "legacy-to-xormigrate",
MigrateSession: func(sess *xorm.Session) error {
var mig []*v000Migrations
type migrations struct {
Name string `xorm:"UNIQUE"`
}

var mig []*migrations

Check warning on line 15 in server/store/datastore/migration/000_legacy_to_xormigrate.go

View check run for this annotation

Codecov / codecov/patch

server/store/datastore/migration/000_legacy_to_xormigrate.go#L11-L15

Added lines #L11 - L15 were not covered by tests
if err := sess.Find(&mig); err != nil {
return err
}
Expand Down
25 changes: 17 additions & 8 deletions server/store/datastore/migration/001_add_org_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,39 @@ import (

"src.techknowlogick.com/xormigrate"
"xorm.io/xorm"

"go.woodpecker-ci.org/woodpecker/v2/server/model"
)

var addOrgID = xormigrate.Migration{
ID: "add-org-id",
MigrateSession: func(sess *xorm.Session) error {
if err := sess.Sync(new(userV009)); err != nil {
type users struct {
ID int64 `xorm:"pk autoincr 'user_id'"`
Login string `xorm:"UNIQUE 'user_login'"`
OrgID int64 `xorm:"user_org_id"`
}
type orgs struct {
ID int64 `xorm:"pk autoincr 'id'"`
Name string `xorm:"UNIQUE 'name'"`
IsUser bool `xorm:"is_user"`
}

if err := sess.Sync(new(users), new(orgs)); err != nil {
return fmt.Errorf("sync new models failed: %w", err)
}

// get all users
var users []*userV009
if err := sess.Find(&users); err != nil {
var us []*users
if err := sess.Find(&us); err != nil {
return fmt.Errorf("find all repos failed: %w", err)
}

for _, user := range users {
org := &model.Org{}
for _, user := range us {
org := &orgs{}
has, err := sess.Where("name = ?", user.Login).Get(org)
if err != nil {
return fmt.Errorf("getting org failed: %w", err)
} else if !has {
org = &model.Org{
org = &orgs{
Name: user.Login,
IsUser: true,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,19 @@ import (
"xorm.io/xorm"
)

type oldSecret004 struct {
ID int64 `json:"id" xorm:"pk autoincr 'secret_id'"`
PluginsOnly bool `json:"plugins_only" xorm:"secret_plugins_only"`
SkipVerify bool `json:"-" xorm:"secret_skip_verify"`
Conceal bool `json:"-" xorm:"secret_conceal"`
Images []string `json:"images" xorm:"json 'secret_images'"`
}

func (oldSecret004) TableName() string {
return "secrets"
}

var removePluginOnlyOptionFromSecretsTable = xormigrate.Migration{
ID: "remove-plugin-only-option-from-secrets-table",
MigrateSession: func(sess *xorm.Session) (err error) {
type secrets struct {
ID int64 `json:"id" xorm:"pk autoincr 'secret_id'"`
PluginsOnly bool `json:"plugins_only" xorm:"secret_plugins_only"`
SkipVerify bool `json:"-" xorm:"secret_skip_verify"`
Conceal bool `json:"-" xorm:"secret_conceal"`
Images []string `json:"images" xorm:"json 'secret_images'"`
}

// make sure plugin_only column exists
if err := sess.Sync(new(oldSecret004)); err != nil {
if err := sess.Sync(new(secrets)); err != nil {
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,35 @@ package migration
import (
"src.techknowlogick.com/xormigrate"
"xorm.io/xorm"

errorTypes "go.woodpecker-ci.org/woodpecker/v2/pipeline/errors/types"
)

// perPage005 set the size of the slice to read per page.
var perPage005 = 100

type pipeline005 struct {
ID int64 `json:"id" xorm:"pk autoincr 'pipeline_id'"`
Error string `json:"error" xorm:"LONGTEXT 'pipeline_error'"` // old error format
Errors []*errorTypes.PipelineError `json:"errors" xorm:"json 'pipeline_errors'"` // new error format
}

func (pipeline005) TableName() string {
return "pipelines"
}

type PipelineError005 struct {
Type string `json:"type"`
Message string `json:"message"`
IsWarning bool `json:"is_warning"`
Data any `json:"data"`
}

var convertToNewPipelineErrorFormat = xormigrate.Migration{
ID: "convert-to-new-pipeline-error-format",
Long: true,
MigrateSession: func(sess *xorm.Session) (err error) {
type PipelineError struct {
Type string `json:"type"`
Message string `json:"message"`
IsWarning bool `json:"is_warning"`
Data any `json:"data"`
}

type pipelines struct {
ID int64 `json:"id" xorm:"pk autoincr 'pipeline_id'"`
Error string `json:"error" xorm:"LONGTEXT 'pipeline_error'"` // old error format
Errors []*PipelineError `json:"errors" xorm:"json 'pipeline_errors'"` // new error format
}

// make sure pipeline_error column exists
if err := sess.Sync(new(pipeline005)); err != nil {
if err := sess.Sync(new(pipelines)); err != nil {
return err
}

page := 0
oldPipelines := make([]*pipeline005, 0, perPage005)
oldPipelines := make([]*pipelines, 0, perPage005)

for {
oldPipelines = oldPipelines[:0]
Expand All @@ -62,9 +56,9 @@ var convertToNewPipelineErrorFormat = xormigrate.Migration{
}

for _, oldPipeline := range oldPipelines {
var newPipeline pipeline005
var newPipeline pipelines
newPipeline.ID = oldPipeline.ID
newPipeline.Errors = []*errorTypes.PipelineError{{
newPipeline.Errors = []*PipelineError{{
Type: "generic",
Message: oldPipeline.Error,
}}
Expand Down
38 changes: 15 additions & 23 deletions server/store/datastore/migration/007_clean_registry_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,24 @@ import (
"xorm.io/xorm"
)

type oldRegistry007 struct {
ID int64 `json:"id" xorm:"pk autoincr 'registry_id'"`
Token string `json:"token" xorm:"TEXT 'registry_token'"`
Email string `json:"email" xorm:"varchar(500) 'registry_email'"`
}

func (oldRegistry007) TableName() string {
return "registry"
}

type oldPipeline007 struct {
ID int64 `json:"id" xorm:"pk autoincr 'pipeline_id'"`
ConfigID int64 `json:"-" xorm:"pipeline_config_id"`
Enqueued int64 `json:"enqueued_at" xorm:"pipeline_enqueued"`
CloneURL string `json:"clone_url" xorm:"pipeline_clone_url"`
}

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

var cleanRegistryPipeline = xormigrate.Migration{
ID: "clean-registry-pipeline",
MigrateSession: func(sess *xorm.Session) (err error) {
if err := sess.Sync(new(oldRegistry007), new(oldPipeline007)); err != nil {
type registry struct {
ID int64 `json:"id" xorm:"pk autoincr 'registry_id'"`
Token string `json:"token" xorm:"TEXT 'registry_token'"`
Email string `json:"email" xorm:"varchar(500) 'registry_email'"`
}

type pipelines struct {
ID int64 `json:"id" xorm:"pk autoincr 'pipeline_id'"`
ConfigID int64 `json:"-" xorm:"pipeline_config_id"`
Enqueued int64 `json:"enqueued_at" xorm:"pipeline_enqueued"`
CloneURL string `json:"clone_url" xorm:"pipeline_clone_url"`
}

// ensure columns to drop exist
if err := sess.Sync(new(registry), new(pipelines)); err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions server/store/datastore/migration/008_set_default_forge_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (repoV008) TableName() string {
return "repos"
}

type forgeV008 struct {
type forge struct {
ID int64 `xorm:"pk autoincr 'id'"`
Type model.ForgeType `xorm:"VARCHAR(250) 'type'"`
URL string `xorm:"VARCHAR(500) 'url'"`
Expand All @@ -88,14 +88,14 @@ type forgeV008 struct {
AdditionalOptions map[string]any `xorm:"json 'additional_options'"`
}

func (forgeV008) TableName() string {
func (forge) TableName() string {
return "forge"
}

var setForgeID = xormigrate.Migration{
ID: "set-forge-id",
MigrateSession: func(sess *xorm.Session) (err error) {
if err := sess.Sync(new(userV008), new(repoV008), new(forgeV008), new(model.Org)); err != nil {
if err := sess.Sync(new(userV008), new(repoV008), new(forge), new(model.Org)); err != nil {
return fmt.Errorf("sync new models failed: %w", err)
}

Expand Down
Loading