Repository: lf-edge/ekuiper
Pull request #3021 (2024-07-18 03:39:39)

feat: let url props compatibility

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/lf-edge/ekuiper/pull/3021?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lf-edge) Report
Attention: Patch coverage is `81.81818%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 67.98%. Comparing base [(`88fd5da`)](https://app.codecov.io/gh/lf-edge/ekuiper/commit/88fd5da26daaa8b607b01cb013f2d77a39818e40?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lf-edge) to head [(`6a88e40`)](https://app.codecov.io/gh/lf-edge/ekuiper/commit/6a88e40857da9d7bd3585bf73b77af9daf2aa7dc?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lf-edge).
> Report is 1 commits behind head on master-v2.

| [Files](https://app.codecov.io/gh/lf-edge/ekuiper/pull/3021?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lf-edge) | Patch % | Lines |
|---|---|---|
| [extensions/impl/sql/sink.go](https://app.codecov.io/gh/lf-edge/ekuiper/pull/3021?src=pr&el=tree&filepath=extensions%2Fimpl%2Fsql%2Fsink.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lf-edge#diff-ZXh0ZW5zaW9ucy9pbXBsL3NxbC9zaW5rLmdv) | 0.00% | [2 Missing :warning: ](https://app.codecov.io/gh/lf-edge/ekuiper/pull/3021?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lf-edge) |

<details><summary>Additional details and impacted files</summary>


```diff
@@              Coverage Diff              @@
##           master-v2    #3021      +/-   ##
=============================================
+ Coverage      67.97%   67.98%   +0.01%     
=============================================
  Files            353      353              
  Lines          34242    34419     +177     
=============================================
+ Hits           23273    23397     +124     
- Misses          8898     8940      +42     
- Partials        2071     2082      +11     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/lf-edge/ekuiper/pull/3021?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lf-edge).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lf-edge).


Patch:
diff --git a/extensions/impl/sql/sink.go b/extensions/impl/sql/sink.go
index 230d7947..cd94b1e6 100644
--- a/extensions/impl/sql/sink.go
+++ b/extensions/impl/sql/sink.go
@@ -34,7 +34,7 @@ type SQLSinkConnector struct {
 }
 
 type sqlSinkConfig struct {
-	DBUrl        string   `json:"dburl"`
+	*SQLConf
 	Table        string   `json:"table"`
 	Fields       []string `json:"fields"`
 	RowKindField string   `json:"rowKindField"`
@@ -90,8 +90,8 @@ func (s *SQLSinkConnector) Provision(ctx api.StreamContext, configs map[string]a
 	if err != nil {
 		return err
 	}
-	if c.DBUrl == "" {
-		return fmt.Errorf("property dburl is required")
+	if err := c.resolveDBURL(); err != nil {
+		return err
 	}
 	if c.Table == "" {
 		return fmt.Errorf("property Table is required")
diff --git a/extensions/impl/sql/source.go b/extensions/impl/sql/source.go
index 98585fa0..fb2ab8b6 100644
--- a/extensions/impl/sql/source.go
+++ b/extensions/impl/sql/source.go
@@ -42,6 +42,7 @@ type SQLSourceConnector struct {
 type SQLConf struct {
 	Interval cast.DurationConf `json:"interval"`
 	DBUrl    string            `json:"dburl"`
+	URL      string            `json:"url,omitempty"`
 }
 
 func (s *SQLSourceConnector) Provision(ctx api.StreamContext, props map[string]any) error {
@@ -56,8 +57,8 @@ func (s *SQLSourceConnector) Provision(ctx api.StreamContext, props map[string]a
 	if time.Duration(cfg.Interval) < 1 {
 		return fmt.Errorf("interval should be defined")
 	}
-	if len(cfg.DBUrl) < 1 {
-		return fmt.Errorf("dburl should be defined")
+	if err := cfg.resolveDBURL(); err != nil {
+		return err
 	}
 	s.conf = cfg
 	s.props = props
@@ -219,3 +220,14 @@ func GetSource() api.Source {
 }
 
 var _ api.PullTupleSource = &SQLSourceConnector{}
+
+func (sc *SQLConf) resolveDBURL() error {
+	if len(sc.DBUrl) < 1 && len(sc.URL) < 1 {
+		return fmt.Errorf("dburl should be defined")
+	}
+	if len(sc.DBUrl) < 1 {
+		sc.DBUrl = sc.URL
+	}
+	sc.URL = ""
+	return nil
+}


Test patch:
diff --git a/extensions/impl/sql/source_test.go b/extensions/impl/sql/source_test.go
index caf2df3c..f1a23b2a 100644
--- a/extensions/impl/sql/source_test.go
+++ b/extensions/impl/sql/source_test.go
@@ -252,3 +252,32 @@ func TestSQLReconnect(t *testing.T) {
 	sqlConnector.queryData(ctx, time.Now(), func(ctx api.StreamContext, data any, meta map[string]any, ts time.Time) {}, func(ctx api.StreamContext, err error) {})
 	require.False(t, sqlConnector.needReconnect)
 }
+
+func TestSQLConfURL(t *testing.T) {
+	testcases := []struct {
+		got *SQLConf
+		exp *SQLConf
+	}{
+		{
+			got: &SQLConf{
+				DBUrl: "123",
+				URL:   "321",
+			},
+			exp: &SQLConf{
+				DBUrl: "123",
+			},
+		},
+		{
+			got: &SQLConf{
+				URL: "321",
+			},
+			exp: &SQLConf{
+				DBUrl: "321",
+			},
+		},
+	}
+	for _, tc := range testcases {
+		require.NoError(t, tc.got.resolveDBURL())
+		require.Equal(t, tc.exp, tc.got)
+	}
+}