Skip to content

Commit 8b5e9e7

Browse files
committed
feat(sqldef): implement sqldef plugin for executing sql
Signed-off-by: kadai0308 <kadai0308@gmail.com>
1 parent a0cb33f commit 8b5e9e7

File tree

14 files changed

+1114
-4
lines changed

14 files changed

+1114
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ gomock_reflect_*/
2222
.cache
2323

2424
# other
25-
.DS_Store
25+
.DS_Store
26+
.idea

plugins/sqldef/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ include ../../Makefile.common
66
# .PHONY: common/test/go
77
# common/test/go:
88
# XXX=YYY go test -failfast -race ./...
9+
10+
build:
11+
go build

plugins/sqldef/config/config.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package config
2+
3+
type SqldefDeployTargetConfig struct {
4+
DbType string `json:"db_type"`
5+
Username string `json:"username"`
6+
Password string `json:"password"`
7+
Host string `json:"host"`
8+
Port string `json:"port"`
9+
DBName string `json:"db_name"`
10+
SchemaFilePath string `json:"schema_file_path"`
11+
DryRun bool `json:"dry_run"`
12+
EnableDrop bool `json:"enable_drop"`
13+
}
14+
15+
type SqldefApplicationSpec struct {
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package deployment
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/pipe-cd/community-plugins/plugins/sqldef/config"
7+
"github.com/pipe-cd/community-plugins/plugins/sqldef/provider"
8+
toolRegistryPkg "github.com/pipe-cd/community-plugins/plugins/sqldef/toolregistry"
9+
10+
sdk "github.com/pipe-cd/piped-plugin-sdk-go"
11+
)
12+
13+
func (p *Plugin) executeSqldefSyncStage(ctx context.Context, dts []*sdk.DeployTarget[config.SqldefDeployTargetConfig], input *sdk.ExecuteStageInput[config.SqldefApplicationSpec]) sdk.StageStatus {
14+
lp := input.Client.LogPersister()
15+
lp.Info("Start syncing the deployment")
16+
// 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.
17+
toolRegistry := toolRegistryPkg.NewRegistry(input.Client.ToolRegistry())
18+
19+
for _, dt := range dts {
20+
// TODO: check db_type from dt.config to choose which sqldef binary to download
21+
// Now we temporarily hard-coded as mysql
22+
sqlDefPath, err := toolRegistry.Mysqldef(ctx, "")
23+
24+
lp.Info(fmt.Sprintf("Sqldef binary downloaded: %s", sqlDefPath))
25+
if err != nil {
26+
lp.Errorf("Failed while getting Sqldef tool (%v)", err)
27+
return sdk.StageStatusFailure
28+
}
29+
30+
lp.Info(fmt.Sprintf("dt: %+v\n", dt))
31+
32+
sqldef := provider.NewSqldef(lp, dt.Config.Username, dt.Config.Password, dt.Config.Host, dt.Config.Port, dt.Config.DBName, dt.Config.SchemaFilePath, sqlDefPath)
33+
34+
err = sqldef.Execute(ctx, dt.Config.DryRun, dt.Config.EnableDrop)
35+
if err != nil {
36+
lp.Errorf("Failed while syncing the deployment (%v)", err)
37+
}
38+
}
39+
40+
return sdk.StageStatusSuccess
41+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package deployment
2+
3+
import (
4+
sdk "github.com/pipe-cd/piped-plugin-sdk-go"
5+
)
6+
7+
const (
8+
SqldefFuncSync string = "SQLDEF_FUNCTION_SYNC"
9+
SqldefFuncRollback string = "SQLDEF_FUNCTION_ROLLBACK"
10+
)
11+
12+
var allStages = []string{
13+
SqldefFuncSync,
14+
SqldefFuncRollback,
15+
}
16+
17+
// buildPipelineStages builds the pipeline stages with the given SDK stages.
18+
func buildPipelineStages(stages []sdk.StageConfig, autoRollback bool) []sdk.PipelineStage {
19+
// MOCK first, TODO
20+
out := make([]sdk.PipelineStage, 0, len(stages)+1)
21+
22+
for _, s := range stages {
23+
out = append(out, sdk.PipelineStage{
24+
Name: s.Name,
25+
Index: s.Index,
26+
Rollback: false,
27+
Metadata: make(map[string]string, 0),
28+
AvailableOperation: sdk.ManualOperationNone,
29+
})
30+
}
31+
32+
//if autoRollback {
33+
// // we set the index of the rollback stage to the minimum index of all stages.
34+
// minIndex := slices.MinFunc(stages, func(a, b sdk.StageConfig) int {
35+
// return a.Index - b.Index
36+
// }).Index
37+
//
38+
// out = append(out, sdk.PipelineStage{
39+
// Name: SqldefFuncRollback,
40+
// Index: minIndex,
41+
// Rollback: true,
42+
// Metadata: make(map[string]string, 0),
43+
// AvailableOperation: sdk.ManualOperationNone,
44+
// })
45+
//}
46+
47+
return out
48+
}
49+
50+
func buildQuickSync(autoRollback bool) []sdk.QuickSyncStage {
51+
// MOCK first, TODO
52+
out := make([]sdk.QuickSyncStage, 0, 2)
53+
out = append(out, sdk.QuickSyncStage{
54+
Name: string(SqldefFuncSync),
55+
Description: "", //TODO: add description
56+
Metadata: map[string]string{},
57+
AvailableOperation: sdk.ManualOperationNone,
58+
})
59+
if autoRollback {
60+
out = append(out, sdk.QuickSyncStage{
61+
Name: string(SqldefFuncRollback),
62+
Description: "", // TODO
63+
Metadata: map[string]string{},
64+
AvailableOperation: sdk.ManualOperationNone,
65+
Rollback: true,
66+
})
67+
}
68+
return out
69+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package deployment
2+
3+
import (
4+
"context"
5+
"github.com/pipe-cd/community-plugins/plugins/sqldef/config"
6+
sdk "github.com/pipe-cd/piped-plugin-sdk-go"
7+
)
8+
9+
type Plugin struct{}
10+
11+
type toolRegistry interface {
12+
Mysqldef(ctx context.Context, version string) (string, error)
13+
}
14+
15+
// FetchDefinedStages returns the defined stages for this plugin.
16+
func (p *Plugin) FetchDefinedStages() []string {
17+
return allStages
18+
}
19+
20+
// BuildPipelineSyncStages returns the stages for the pipeline sync strategy.
21+
func (p *Plugin) BuildPipelineSyncStages(ctx context.Context, _ *sdk.ConfigNone, input *sdk.BuildPipelineSyncStagesInput) (*sdk.BuildPipelineSyncStagesResponse, error) {
22+
// MOCK first, TODO
23+
return &sdk.BuildPipelineSyncStagesResponse{
24+
Stages: buildPipelineStages(input.Request.Stages, input.Request.Rollback),
25+
}, nil
26+
}
27+
28+
// ExecuteStage executes the stage.
29+
func (p *Plugin) ExecuteStage(ctx context.Context, _ *sdk.ConfigNone, dts []*sdk.DeployTarget[config.SqldefDeployTargetConfig], input *sdk.ExecuteStageInput[config.SqldefApplicationSpec]) (*sdk.ExecuteStageResponse, error) {
30+
switch input.Request.StageName {
31+
case SqldefFuncSync:
32+
return &sdk.ExecuteStageResponse{
33+
Status: p.executeSqldefSyncStage(ctx, dts, input),
34+
}, nil
35+
default:
36+
panic("unimplemented stage: " + input.Request.StageName)
37+
}
38+
}
39+
40+
// DetermineVersions determines the versions of the application.
41+
func (p *Plugin) DetermineVersions(ctx context.Context, _ *sdk.ConfigNone, input *sdk.DetermineVersionsInput[config.SqldefApplicationSpec]) (*sdk.DetermineVersionsResponse, error) {
42+
// MOCK first, TODO
43+
return &sdk.DetermineVersionsResponse{
44+
Versions: []sdk.ArtifactVersion{
45+
{
46+
Name: "DetermineVersionsResponse",
47+
Version: "DetermineVersionsResponse",
48+
URL: "DetermineVersionsResponse",
49+
},
50+
},
51+
}, nil
52+
}
53+
54+
// DetermineStrategy determines the strategy for the deployment.
55+
func (p *Plugin) DetermineStrategy(ctx context.Context, _ *sdk.ConfigNone, input *sdk.DetermineStrategyInput[config.SqldefApplicationSpec]) (*sdk.DetermineStrategyResponse, error) {
56+
// MOCK first, TODO
57+
return &sdk.DetermineStrategyResponse{
58+
Strategy: sdk.SyncStrategyPipelineSync,
59+
}, nil
60+
}
61+
62+
// BuildQuickSyncStages returns the stages for the quick sync strategy.
63+
func (p *Plugin) BuildQuickSyncStages(ctx context.Context, _ *sdk.ConfigNone, input *sdk.BuildQuickSyncStagesInput) (*sdk.BuildQuickSyncStagesResponse, error) {
64+
// MOCK first, TODO
65+
return &sdk.BuildQuickSyncStagesResponse{
66+
Stages: buildQuickSync(input.Request.Rollback),
67+
}, nil
68+
}

plugins/sqldef/go.mod

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module github.com/pipe-cd/community-plugins/plugins/sqldef
2+
3+
go 1.24.3
4+
5+
require github.com/pipe-cd/piped-plugin-sdk-go v0.0.0-20250707080244-4bc5fa28769a
6+
7+
require (
8+
cloud.google.com/go v0.112.1 // indirect
9+
cloud.google.com/go/compute/metadata v0.3.0 // indirect
10+
cloud.google.com/go/profiler v0.3.1 // indirect
11+
github.com/beorn7/perks v1.0.1 // indirect
12+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
13+
github.com/coreos/go-oidc/v3 v3.11.0 // indirect
14+
github.com/creasty/defaults v1.6.0 // indirect
15+
github.com/envoyproxy/protoc-gen-validate v1.0.4 // indirect
16+
github.com/go-jose/go-jose/v4 v4.0.5 // indirect
17+
github.com/go-logr/logr v1.4.2 // indirect
18+
github.com/go-logr/stdr v1.2.2 // indirect
19+
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
20+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
21+
github.com/golang/protobuf v1.5.4 // indirect
22+
github.com/google/pprof v0.0.0-20221103000818-d260c55eee4c // indirect
23+
github.com/google/s2a-go v0.1.7 // indirect
24+
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
25+
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
26+
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
27+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
28+
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
29+
github.com/pipe-cd/pipecd v0.52.1-0.20250704040938-472e02fa6fa1 // indirect
30+
github.com/prometheus/client_golang v1.12.1 // indirect
31+
github.com/prometheus/client_model v0.5.0 // indirect
32+
github.com/prometheus/common v0.32.1 // indirect
33+
github.com/prometheus/procfs v0.7.3 // indirect
34+
github.com/spf13/cobra v1.9.1 // indirect
35+
github.com/spf13/pflag v1.0.6 // indirect
36+
go.opencensus.io v0.24.0 // indirect
37+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
38+
go.opentelemetry.io/otel v1.28.0 // indirect
39+
go.opentelemetry.io/otel/metric v1.28.0 // indirect
40+
go.opentelemetry.io/otel/trace v1.28.0 // indirect
41+
go.uber.org/atomic v1.11.0 // indirect
42+
go.uber.org/multierr v1.6.0 // indirect
43+
go.uber.org/zap v1.19.1 // indirect
44+
go.yaml.in/yaml/v2 v2.4.2 // indirect
45+
golang.org/x/crypto v0.36.0 // indirect
46+
golang.org/x/net v0.38.0 // indirect
47+
golang.org/x/oauth2 v0.21.0 // indirect
48+
golang.org/x/sync v0.12.0 // indirect
49+
golang.org/x/sys v0.31.0 // indirect
50+
golang.org/x/text v0.23.0 // indirect
51+
golang.org/x/time v0.5.0 // indirect
52+
google.golang.org/api v0.169.0 // indirect
53+
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect
54+
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
55+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
56+
google.golang.org/grpc v1.64.1 // indirect
57+
google.golang.org/protobuf v1.34.2 // indirect
58+
sigs.k8s.io/yaml v1.5.0 // indirect
59+
)

plugins/sqldef/go.mod.tmp

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)