Skip to content

Commit b1acbf1

Browse files
authored
[Sqldef Plugin] feat(sqldef) implement rollback stage (#40)
* feat(sqldef) implement rollback stage Signed-off-by: kadai0308 <[email protected]> * feat(sqldef) add header Signed-off-by: kadai0308 <[email protected]> --------- Signed-off-by: kadai0308 <[email protected]>
1 parent 3c28262 commit b1acbf1

6 files changed

Lines changed: 111 additions & 13 deletions

File tree

plugins/sqldef/deployment/plugin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ func (p *Plugin) ExecuteStage(
8787
return &sdk.ExecuteStageResponse{
8888
Status: p.executeApplyStage(ctx, dts, input),
8989
}, nil
90+
case sqldefStageRollback:
91+
return &sdk.ExecuteStageResponse{
92+
Status: p.executeRollbackStage(ctx, dts, input),
93+
}, nil
9094
default:
9195
panic("unimplemented stage")
9296
}

plugins/sqldef/deployment/stage_apply.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2025 The PipeCD Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package deployment
216

317
import (

plugins/sqldef/deployment/stage_plan.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2025 The PipeCD Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package deployment
216

317
import (
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2025 The PipeCD Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package deployment
16+
17+
import (
18+
"context"
19+
"github.com/pipe-cd/community-plugins/plugins/sqldef/config"
20+
toolRegistryPkg "github.com/pipe-cd/community-plugins/plugins/sqldef/toolregistry"
21+
22+
sdk "github.com/pipe-cd/piped-plugin-sdk-go"
23+
)
24+
25+
func (p *Plugin) executeRollbackStage(ctx context.Context, dts []*sdk.DeployTarget[config.DeployTargetConfig], input *sdk.ExecuteStageInput[config.ApplicationConfigSpec]) sdk.StageStatus {
26+
lp := input.Client.LogPersister()
27+
lp.Info("Start rollback the schema deployment")
28+
29+
// Currently, we create them every time the stage is executed because we can't pass input.Client.toolRegistry to the plugin when starting the plugin.
30+
toolRegistry := toolRegistryPkg.NewRegistry(input.Client.ToolRegistry())
31+
32+
for _, dt := range dts {
33+
lp.Infof("Deploy Target [%s]: host=%s, port=%s, db=%s",
34+
dt.Name,
35+
dt.Config.Host,
36+
dt.Config.Port,
37+
dt.Config.DBName,
38+
)
39+
40+
sqlDefPath, err := toolRegistry.DownloadBinary(ctx, dt.Config.DbType, "")
41+
if err != nil {
42+
lp.Errorf("Failed while getting Sqldef tool (%v)", err)
43+
return sdk.StageStatusFailure
44+
}
45+
46+
appDir := input.Request.RunningDeploymentSource.ApplicationDirectory
47+
schemaPath, err := findFirstSQLFile(appDir)
48+
if err != nil {
49+
lp.Errorf("Failed while finding schema file (%v)", err)
50+
return sdk.StageStatusFailure
51+
}
52+
53+
p.Sqldef.Init(lp, dt.Config.Username, dt.Config.Password, dt.Config.Host, dt.Config.Port, dt.Config.DBName, schemaPath, sqlDefPath)
54+
55+
if err := p.Sqldef.Execute(ctx, false); err != nil {
56+
lp.Errorf("Failed while applying the deployment (%v)", err)
57+
return sdk.StageStatusFailure
58+
}
59+
}
60+
61+
return sdk.StageStatusSuccess
62+
}

plugins/sqldef/go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/pipe-cd/community-plugins/plugins/sqldef
33
go 1.24.3
44

55
require (
6-
github.com/pipe-cd/piped-plugin-sdk-go v0.0.0-20250619080234-1ee9423d23c1
6+
github.com/pipe-cd/piped-plugin-sdk-go v0.1.0
77
github.com/stretchr/testify v1.10.0
88
)
99

@@ -30,7 +30,7 @@ require (
3030
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
3131
github.com/inconshreveable/mousetrap v1.1.0 // indirect
3232
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
33-
github.com/pipe-cd/pipecd v0.52.0 // indirect
33+
github.com/pipe-cd/pipecd v0.52.1-0.20250731104149-f611ce3501c5 // indirect
3434
github.com/pmezard/go-difflib v1.0.0 // indirect
3535
github.com/prometheus/client_golang v1.12.1 // indirect
3636
github.com/prometheus/client_model v0.5.0 // indirect
@@ -47,9 +47,10 @@ require (
4747
go.uber.org/atomic v1.11.0 // indirect
4848
go.uber.org/multierr v1.6.0 // indirect
4949
go.uber.org/zap v1.19.1 // indirect
50+
go.yaml.in/yaml/v2 v2.4.2 // indirect
5051
golang.org/x/crypto v0.36.0 // indirect
5152
golang.org/x/net v0.38.0 // indirect
52-
golang.org/x/oauth2 v0.21.0 // indirect
53+
golang.org/x/oauth2 v0.27.0 // indirect
5354
golang.org/x/sync v0.12.0 // indirect
5455
golang.org/x/sys v0.31.0 // indirect
5556
golang.org/x/text v0.23.0 // indirect
@@ -60,7 +61,6 @@ require (
6061
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
6162
google.golang.org/grpc v1.64.1 // indirect
6263
google.golang.org/protobuf v1.34.2 // indirect
63-
gopkg.in/yaml.v2 v2.4.0 // indirect
6464
gopkg.in/yaml.v3 v3.0.1 // indirect
65-
sigs.k8s.io/yaml v1.3.0 // indirect
65+
sigs.k8s.io/yaml v1.5.0 // indirect
6666
)

plugins/sqldef/go.sum

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb
207207
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
208208
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
209209
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
210-
github.com/pipe-cd/pipecd v0.52.0 h1:/WRzHs4hqeYRJBvu0ask6UAO7qBlvPgN1ulBdA1VjgE=
211-
github.com/pipe-cd/pipecd v0.52.0/go.mod h1:Hi4d3mndTeY+hPB4YbN9aIgvP00EBV0CM+NQgyEwn98=
212-
github.com/pipe-cd/piped-plugin-sdk-go v0.0.0-20250619080234-1ee9423d23c1 h1:XkFY49HEXBRYbTUOGxK0IoaEeG1Kx03FdxQjRuCJXBE=
213-
github.com/pipe-cd/piped-plugin-sdk-go v0.0.0-20250619080234-1ee9423d23c1/go.mod h1:WpVRto2ZLgFRJ4VOk8gtTChHNCrGa4UjRhGN81TCl2E=
210+
github.com/pipe-cd/pipecd v0.52.1-0.20250731104149-f611ce3501c5 h1:1VM6ZkE2YfXqROq3lU8xrOV21MdJ257p19VX71E/nsU=
211+
github.com/pipe-cd/pipecd v0.52.1-0.20250731104149-f611ce3501c5/go.mod h1:5H0ydj0eUpGnJOesA2GPU3mTVlZEZDb8cNP7/lvNPTU=
212+
github.com/pipe-cd/piped-plugin-sdk-go v0.1.0 h1:kvtWTGRBY9wE9skhWF3cZUHdqcY1W6f6pblQG+qq8X0=
213+
github.com/pipe-cd/piped-plugin-sdk-go v0.1.0/go.mod h1:awF6mNgk8B0uP1HQ4vQKkso6qUdlUJIYUFg2fYqlvuA=
214214
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
215215
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
216216
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -299,6 +299,10 @@ go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
299299
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
300300
go.uber.org/zap v1.19.1 h1:ue41HOKd1vGURxrmeKIgELGb3jPW9DMUDGtsinblHwI=
301301
go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI=
302+
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
303+
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
304+
go.yaml.in/yaml/v3 v3.0.3 h1:bXOww4E/J3f66rav3pX3m8w6jDE4knZjGOw8b5Y6iNE=
305+
go.yaml.in/yaml/v3 v3.0.3/go.mod h1:tBHosrYAkRZjRAOREWbDnBXUf08JOwYq++0QNwQiWzI=
302306
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
303307
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
304308
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
@@ -377,8 +381,8 @@ golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4Iltr
377381
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
378382
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
379383
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
380-
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
381-
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
384+
golang.org/x/oauth2 v0.27.0 h1:da9Vo7/tDv5RH/7nZDz1eMGS/q1Vv1N/7FCrBhI9I3M=
385+
golang.org/x/oauth2 v0.27.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8=
382386
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
383387
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
384388
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -608,5 +612,5 @@ honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
608612
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
609613
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
610614
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
611-
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
612-
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
615+
sigs.k8s.io/yaml v1.5.0 h1:M10b2U7aEUY6hRtU870n2VTPgR5RZiL/I6Lcc2F4NUQ=
616+
sigs.k8s.io/yaml v1.5.0/go.mod h1:wZs27Rbxoai4C0f8/9urLZtZtF3avA3gKvGyPdDqTO4=

0 commit comments

Comments
 (0)