Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ gomock_reflect_*/
.cache

# other
.DS_Store
.DS_Store
.idea
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete

3 changes: 3 additions & 0 deletions plugins/sqldef/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ include ../../Makefile.common
# .PHONY: common/test/go
# common/test/go:
# XXX=YYY go test -failfast -race ./...

build:
go build
Comment on lines +9 to +11
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use make build/go to keep the consistency unless special reasons.

.PHONY: build/go

16 changes: 16 additions & 0 deletions plugins/sqldef/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package config

type SqldefDeployTargetConfig struct {
DbType string `json:"db_type"`
Username string `json:"username"`
Password string `json:"password"`
Host string `json:"host"`
Port string `json:"port"`
DBName string `json:"db_name"`
SchemaFilePath string `json:"schema_file_path"`
DryRun bool `json:"dry_run"`
EnableDrop bool `json:"enable_drop"`
}

type SqldefApplicationSpec struct {
}
41 changes: 41 additions & 0 deletions plugins/sqldef/deployment/func_sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package deployment

import (
"context"
"fmt"
"github.com/pipe-cd/community-plugins/plugins/sqldef/config"
"github.com/pipe-cd/community-plugins/plugins/sqldef/provider"
toolRegistryPkg "github.com/pipe-cd/community-plugins/plugins/sqldef/toolregistry"

sdk "github.com/pipe-cd/piped-plugin-sdk-go"
)

func (p *Plugin) executeSqldefSyncStage(ctx context.Context, dts []*sdk.DeployTarget[config.SqldefDeployTargetConfig], input *sdk.ExecuteStageInput[config.SqldefApplicationSpec]) sdk.StageStatus {
lp := input.Client.LogPersister()
lp.Info("Start syncing the deployment")
// Currently, we create them every time the stage is executed beucause we can't pass input.Client.toolRegistry to the plugin when starting the plugin.
toolRegistry := toolRegistryPkg.NewRegistry(input.Client.ToolRegistry())

for _, dt := range dts {
// TODO: check db_type from dt.config to choose which sqldef binary to download
// Now we temporarily hard-coded as mysql
sqlDefPath, err := toolRegistry.Mysqldef(ctx, "")

lp.Info(fmt.Sprintf("Sqldef binary downloaded: %s", sqlDefPath))
if err != nil {
lp.Errorf("Failed while getting Sqldef tool (%v)", err)
return sdk.StageStatusFailure
}

lp.Info(fmt.Sprintf("dt: %+v\n", dt))

sqldef := provider.NewSqldef(lp, dt.Config.Username, dt.Config.Password, dt.Config.Host, dt.Config.Port, dt.Config.DBName, dt.Config.SchemaFilePath, sqlDefPath)

err = sqldef.Execute(ctx, dt.Config.DryRun, dt.Config.EnableDrop)
if err != nil {
lp.Errorf("Failed while syncing the deployment (%v)", err)
}
}

return sdk.StageStatusSuccess
}
69 changes: 69 additions & 0 deletions plugins/sqldef/deployment/pipeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package deployment

import (
sdk "github.com/pipe-cd/piped-plugin-sdk-go"
)

const (
SqldefFuncSync string = "SQLDEF_FUNCTION_SYNC"
SqldefFuncRollback string = "SQLDEF_FUNCTION_ROLLBACK"
)

var allStages = []string{
SqldefFuncSync,
SqldefFuncRollback,
}

// buildPipelineStages builds the pipeline stages with the given SDK stages.
func buildPipelineStages(stages []sdk.StageConfig, autoRollback bool) []sdk.PipelineStage {
// MOCK first, TODO
out := make([]sdk.PipelineStage, 0, len(stages)+1)

for _, s := range stages {
out = append(out, sdk.PipelineStage{
Name: s.Name,
Index: s.Index,
Rollback: false,
Metadata: make(map[string]string, 0),
AvailableOperation: sdk.ManualOperationNone,
})
}

//if autoRollback {
// // we set the index of the rollback stage to the minimum index of all stages.
// minIndex := slices.MinFunc(stages, func(a, b sdk.StageConfig) int {
// return a.Index - b.Index
// }).Index
//
// out = append(out, sdk.PipelineStage{
// Name: SqldefFuncRollback,
// Index: minIndex,
// Rollback: true,
// Metadata: make(map[string]string, 0),
// AvailableOperation: sdk.ManualOperationNone,
// })
//}

return out
}

func buildQuickSync(autoRollback bool) []sdk.QuickSyncStage {
// MOCK first, TODO
out := make([]sdk.QuickSyncStage, 0, 2)
out = append(out, sdk.QuickSyncStage{
Name: string(SqldefFuncSync),
Description: "", //TODO: add description
Metadata: map[string]string{},
AvailableOperation: sdk.ManualOperationNone,
})
if autoRollback {
out = append(out, sdk.QuickSyncStage{
Name: string(SqldefFuncRollback),
Description: "", // TODO
Metadata: map[string]string{},
AvailableOperation: sdk.ManualOperationNone,
Rollback: true,
})
}
return out
}
68 changes: 68 additions & 0 deletions plugins/sqldef/deployment/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package deployment

import (
"context"
"github.com/pipe-cd/community-plugins/plugins/sqldef/config"
sdk "github.com/pipe-cd/piped-plugin-sdk-go"
)

type Plugin struct{}

type toolRegistry interface {
Mysqldef(ctx context.Context, version string) (string, error)
}

// FetchDefinedStages returns the defined stages for this plugin.
func (p *Plugin) FetchDefinedStages() []string {
return allStages
}

// BuildPipelineSyncStages returns the stages for the pipeline sync strategy.
func (p *Plugin) BuildPipelineSyncStages(ctx context.Context, _ *sdk.ConfigNone, input *sdk.BuildPipelineSyncStagesInput) (*sdk.BuildPipelineSyncStagesResponse, error) {
// MOCK first, TODO
return &sdk.BuildPipelineSyncStagesResponse{
Stages: buildPipelineStages(input.Request.Stages, input.Request.Rollback),
}, nil
}

// ExecuteStage executes the stage.
func (p *Plugin) ExecuteStage(ctx context.Context, _ *sdk.ConfigNone, dts []*sdk.DeployTarget[config.SqldefDeployTargetConfig], input *sdk.ExecuteStageInput[config.SqldefApplicationSpec]) (*sdk.ExecuteStageResponse, error) {
switch input.Request.StageName {
case SqldefFuncSync:
return &sdk.ExecuteStageResponse{
Status: p.executeSqldefSyncStage(ctx, dts, input),
}, nil
default:
panic("unimplemented stage: " + input.Request.StageName)
}
}

// DetermineVersions determines the versions of the application.
func (p *Plugin) DetermineVersions(ctx context.Context, _ *sdk.ConfigNone, input *sdk.DetermineVersionsInput[config.SqldefApplicationSpec]) (*sdk.DetermineVersionsResponse, error) {
// MOCK first, TODO
return &sdk.DetermineVersionsResponse{
Versions: []sdk.ArtifactVersion{
{
Name: "DetermineVersionsResponse",
Version: "DetermineVersionsResponse",
URL: "DetermineVersionsResponse",
},
},
}, nil
}

// DetermineStrategy determines the strategy for the deployment.
func (p *Plugin) DetermineStrategy(ctx context.Context, _ *sdk.ConfigNone, input *sdk.DetermineStrategyInput[config.SqldefApplicationSpec]) (*sdk.DetermineStrategyResponse, error) {
// MOCK first, TODO
return &sdk.DetermineStrategyResponse{
Strategy: sdk.SyncStrategyPipelineSync,
}, nil
}

// BuildQuickSyncStages returns the stages for the quick sync strategy.
func (p *Plugin) BuildQuickSyncStages(ctx context.Context, _ *sdk.ConfigNone, input *sdk.BuildQuickSyncStagesInput) (*sdk.BuildQuickSyncStagesResponse, error) {
// MOCK first, TODO
return &sdk.BuildQuickSyncStagesResponse{
Stages: buildQuickSync(input.Request.Rollback),
}, nil
}
59 changes: 59 additions & 0 deletions plugins/sqldef/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module github.com/pipe-cd/community-plugins/plugins/sqldef

go 1.24.3

require github.com/pipe-cd/piped-plugin-sdk-go v0.0.0-20250707080244-4bc5fa28769a

require (
cloud.google.com/go v0.112.1 // indirect
cloud.google.com/go/compute/metadata v0.3.0 // indirect
cloud.google.com/go/profiler v0.3.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coreos/go-oidc/v3 v3.11.0 // indirect
github.com/creasty/defaults v1.6.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.4 // indirect
github.com/go-jose/go-jose/v4 v4.0.5 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/pprof v0.0.0-20221103000818-d260c55eee4c // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/pipe-cd/pipecd v0.52.1-0.20250704040938-472e02fa6fa1 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/spf13/cobra v1.9.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.19.1 // indirect
go.yaml.in/yaml/v2 v2.4.2 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/net v0.38.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/api v0.169.0 // indirect
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
sigs.k8s.io/yaml v1.5.0 // indirect
)
3 changes: 0 additions & 3 deletions plugins/sqldef/go.mod.tmp

This file was deleted.

Loading
Loading