Skip to content

Commit bed3b32

Browse files
authored
[Sqldef Pluging] refactor(sqldef) refactor sqldef toolRegistry download binary (#36)
* refactor(sqldef) refactor sqldef toolRegistry download binary Signed-off-by: kadai0308 <kadai0308@gmail.com> * refactor(sqldef) remove binary path cache from stage_plan Signed-off-by: kadai0308 <kadai0308@gmail.com> * refactor(sqldef) fix deployment/stage_plan_test.go Signed-off-by: kadai0308 <kadai0308@gmail.com> * refactor(sqldef) remove useless comment and add TODO to sqldef registry_test Signed-off-by: kadai0308 <kadai0308@gmail.com> --------- Signed-off-by: kadai0308 <kadai0308@gmail.com>
1 parent dfb96e6 commit bed3b32

4 files changed

Lines changed: 88 additions & 36 deletions

File tree

plugins/sqldef/deployment/stage_plan.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package deployment
22

33
import (
44
"context"
5-
"fmt"
65
"github.com/pipe-cd/community-plugins/plugins/sqldef/config"
76
toolRegistryPkg "github.com/pipe-cd/community-plugins/plugins/sqldef/toolregistry"
87

@@ -16,9 +15,6 @@ func (p *Plugin) executePlanStage(ctx context.Context, dts []*sdk.DeployTarget[c
1615
// 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.
1716
toolRegistry := toolRegistryPkg.NewRegistry(input.Client.ToolRegistry())
1817

19-
// map for prevent repeatedly download sqldef
20-
downloadedSqldefPaths := make(map[config.DBType]string)
21-
2218
for _, dt := range dts {
2319
lp.Infof("Deploy Target [%s]: host=%s, port=%s, db=%s, schemaFile=%s",
2420
dt.Name,
@@ -27,26 +23,10 @@ func (p *Plugin) executePlanStage(ctx context.Context, dts []*sdk.DeployTarget[c
2723
dt.Config.DBName,
2824
)
2925

30-
// check db_type from dt.config to choose which sqldef binary to download
31-
// Now we only support mysql temporarily
32-
var sqlDefPath string
33-
var existed bool
34-
if sqlDefPath, existed = downloadedSqldefPaths[config.DBTypeMySQL]; !existed {
35-
var binPath string
36-
var err error
37-
switch dt.Config.DbType {
38-
case config.DBTypeMySQL:
39-
binPath, err = toolRegistry.Mysqldef(ctx, "")
40-
default:
41-
lp.Errorf(fmt.Sprintf("Unsupported database type: %s, currently only support: mysql", dt.Name))
42-
return sdk.StageStatusFailure
43-
}
44-
if err != nil {
45-
lp.Errorf("Failed while getting Sqldef tool (%v)", err)
46-
return sdk.StageStatusFailure
47-
}
48-
downloadedSqldefPaths[dt.Config.DbType] = binPath
49-
lp.Info(fmt.Sprintf("Sqldef binary downloadeded: %s", sqlDefPath))
26+
sqlDefPath, err := toolRegistry.DownloadBinary(ctx, dt.Config.DbType, "")
27+
if err != nil {
28+
lp.Errorf("Failed while getting Sqldef tool (%v)", err)
29+
return sdk.StageStatusFailure
5030
}
5131

5232
appDir := input.Request.RunningDeploymentSource.ApplicationDirectory

plugins/sqldef/deployment/stage_plan_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func TestPlugin_executePlanStage_EmptySchemaFileName(t *testing.T) {
171171
"3306",
172172
"testdb",
173173
"testdata/schema.sql",
174-
"",
174+
mock.AnythingOfType("string"), // execPath from tool registry
175175
).Return(nil)
176176

177177
mockSqldef.On("Execute", mock.Anything, true).Return(errors.New("execute failed."))

plugins/sqldef/toolregistry/registry.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ package toolregistry
1919
import (
2020
"cmp"
2121
"context"
22+
"fmt"
23+
24+
"github.com/pipe-cd/community-plugins/plugins/sqldef/config"
2225
)
2326

2427
const (
@@ -40,8 +43,13 @@ type Registry struct {
4043
client client
4144
}
4245

43-
// Mysqldef installs the mysqldef tool with the given version and return the path to the installed binary.
44-
// If the version is empty, the default version will be used.
45-
func (r *Registry) Mysqldef(ctx context.Context, version string) (string, error) {
46-
return r.client.InstallTool(ctx, "mysqldef", cmp.Or(version, defaultSqldefVersion), MysqldefInstallScript)
46+
// DownloadBinary downloads the appropriate sqldef binary based on the database type.
47+
// Currently only supports MySQL, but can be extended for other database types.
48+
func (r *Registry) DownloadBinary(ctx context.Context, dbType config.DBType, version string) (string, error) {
49+
switch dbType {
50+
case config.DBTypeMySQL:
51+
return r.client.InstallTool(ctx, "mysqldef", cmp.Or(version, defaultSqldefVersion), MysqldefInstallScript)
52+
default:
53+
return "", fmt.Errorf("unsupported database type: %s, currently only support: mysql", dbType)
54+
}
4755
}

plugins/sqldef/toolregistry/registry_test.go

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,86 @@ import (
1818
"context"
1919
"testing"
2020

21+
"github.com/pipe-cd/community-plugins/plugins/sqldef/config"
2122
"github.com/pipe-cd/piped-plugin-sdk-go/toolregistry/toolregistrytest"
2223
"github.com/stretchr/testify/assert"
2324
"github.com/stretchr/testify/require"
2425
)
2526

26-
func TestRegistry_Sqldef(t *testing.T) {
27+
func TestRegistry_DownloadBinary(t *testing.T) {
2728
t.Parallel()
2829

29-
c := toolregistrytest.NewTestToolRegistry(t)
30+
tests := []struct {
31+
name string
32+
dbType config.DBType
33+
version string
34+
expectError bool
35+
errorMsg string
36+
}{
37+
{
38+
name: "MySQL_with_specific_version",
39+
dbType: config.DBTypeMySQL,
40+
version: "2.0.4",
41+
expectError: false,
42+
},
43+
{
44+
name: "MySQL_with_empty_version_should_use_default",
45+
dbType: config.DBTypeMySQL,
46+
version: "",
47+
expectError: false,
48+
},
49+
{
50+
name: "PostgreSQL_unsupported",
51+
dbType: config.DBTypePostgres,
52+
version: "2.0.4",
53+
expectError: true,
54+
errorMsg: "unsupported database type: psql, currently only support: mysql",
55+
},
56+
{
57+
name: "SQLite_unsupported",
58+
dbType: config.DBTypeSQLite,
59+
version: "2.0.4",
60+
expectError: true,
61+
errorMsg: "unsupported database type: sqlite, currently only support: mysql",
62+
},
63+
{
64+
name: "MSSQL_unsupported",
65+
dbType: config.DBTypeMSSQL,
66+
version: "2.0.4",
67+
expectError: true,
68+
errorMsg: "unsupported database type: mssql, currently only support: mysql",
69+
},
70+
}
3071

31-
r := NewRegistry(c)
72+
for _, tt := range tests {
73+
t.Run(tt.name, func(t *testing.T) {
74+
c := toolregistrytest.NewTestToolRegistry(t)
75+
r := NewRegistry(c)
3276

33-
path, err := r.Mysqldef(context.Background(), "2.0.4")
77+
path, err := r.DownloadBinary(context.Background(), tt.dbType, tt.version)
3478

35-
assert.NoError(t, err)
36-
require.NotEmpty(t, path)
79+
if tt.expectError {
80+
assert.Error(t, err)
81+
assert.Equal(t, tt.errorMsg, err.Error())
82+
assert.Empty(t, path)
83+
} else {
84+
assert.NoError(t, err)
85+
require.NotEmpty(t, path)
3786

38-
assert.Contains(t, path, "mysqldef-2.0.4", "Expected file title not found in path")
87+
// For MySQL, verify the path contains the expected binary name
88+
if tt.dbType == config.DBTypeMySQL {
89+
assert.Contains(t, path, "mysqldef", "Expected mysqldef binary name in path")
90+
91+
// If version was provided, check it's in the path
92+
if tt.version != "" {
93+
assert.Contains(t, path, tt.version, "Expected version in path")
94+
} else {
95+
// If no version provided, should use default
96+
assert.Contains(t, path, defaultSqldefVersion, "Expected default version in path")
97+
}
98+
}
99+
// TODO: Add more dbType cases when supported
100+
}
101+
})
102+
}
39103
}

0 commit comments

Comments
 (0)