Skip to content

Commit 17e4e77

Browse files
committed
fix: import with lock sql file
Signed-off-by: thxCode <[email protected]>
1 parent 82c6fe9 commit 17e4e77

28 files changed

+643
-185
lines changed

byteset/provider_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"html/template"
66
"io"
7-
"os"
8-
"path/filepath"
97
"strings"
108
"testing"
119

@@ -60,15 +58,6 @@ func renderConfigTemplate(ct string, keyValuePairs ...any) string {
6058
return s.String()
6159
}
6260

63-
func testdataPath() string {
64-
dir, err := os.Getwd()
65-
if err != nil {
66-
panic(err)
67-
}
68-
69-
return filepath.Join(dir, "testdata")
70-
}
71-
7261
type dockerContainer struct {
7362
Name string
7463
Image string

byteset/resource_pipeline.go

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55
"strings"
66
"time"
77

8+
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
89
"github.com/hashicorp/terraform-plugin-framework/path"
910
"github.com/hashicorp/terraform-plugin-framework/resource"
1011
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1112
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
13+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1214
"github.com/hashicorp/terraform-plugin-framework/types"
1315
"github.com/hashicorp/terraform-plugin-log/tflog"
1416

@@ -41,7 +43,6 @@ type ResourcePipelineDestination struct {
4143
Address types.String `tfsdk:"address"`
4244
ConnMaxOpen types.Int64 `tfsdk:"conn_max_open"`
4345
ConnMaxIdle types.Int64 `tfsdk:"conn_max_idle"`
44-
ConnMaxLife types.Int64 `tfsdk:"conn_max_life"`
4546
Salt types.String `tfsdk:"salt"`
4647
}
4748

@@ -51,9 +52,6 @@ func (r ResourcePipelineDestination) Reflect(ctx context.Context) (pipeline.Dest
5152
r.Address.ValueString(),
5253
pipeline.WithConnMaxOpen(int(r.ConnMaxOpen.ValueInt64())),
5354
pipeline.WithConnMaxIdle(int(r.ConnMaxIdle.ValueInt64())),
54-
pipeline.WithConnMaxLife(
55-
time.Duration(r.ConnMaxLife.ValueInt64())*time.Second,
56-
),
5755
)
5856
}
5957

@@ -118,21 +116,27 @@ choose from local/remote SQL file or database.
118116
"conn_max_open": schema.Int64Attribute{
119117
Optional: true,
120118
Computed: true,
121-
Default: int64default.StaticInt64(15),
119+
Default: int64default.StaticInt64(5),
122120
Description: `The maximum opening connectors of source database.`,
121+
Validators: []validator.Int64{
122+
int64validator.AtLeast(1),
123+
},
123124
},
124125
"conn_max_idle": schema.Int64Attribute{
125126
Optional: true,
126127
Computed: true,
127128
Default: int64default.StaticInt64(5),
128129
Description: `The maximum idling connections of source database.`,
130+
Validators: []validator.Int64{
131+
int64validator.AtLeast(1),
132+
int64validator.AtMostSumOf(
133+
path.MatchRelative().AtParent().AtName("conn_max_open")),
134+
},
129135
},
130136
"conn_max_life": schema.Int64Attribute{
131-
Optional: true,
132-
Computed: true,
133-
Default: int64default.StaticInt64(
134-
5 * 60,
135-
),
137+
Optional: true,
138+
Computed: true,
139+
Default: int64default.StaticInt64(5 * 60),
136140
Description: `The maximum lifetime in seconds of source database.`,
137141
},
138142
},
@@ -153,24 +157,28 @@ choose from local/remote SQL file or database.
153157
- mssql://[username:[password]@][address][:port][/instance][?database=dbname&param1=value1&...]`,
154158
},
155159
"conn_max_open": schema.Int64Attribute{
156-
Optional: true,
157-
Computed: true,
158-
Default: int64default.StaticInt64(15),
159-
Description: `The maximum opening connectors of destination database.`,
160+
Optional: true,
161+
Computed: true,
162+
Default: int64default.StaticInt64(5),
163+
Description: `The maximum opening connectors of destination database,
164+
if the given SQL file is using single transaction, should turn down the "conn_max_open" to 1.
165+
`,
166+
Validators: []validator.Int64{
167+
int64validator.AtLeast(1),
168+
},
160169
},
161170
"conn_max_idle": schema.Int64Attribute{
162-
Optional: true,
163-
Computed: true,
164-
Default: int64default.StaticInt64(5),
165-
Description: `The maximum idling connections of destination database.`,
166-
},
167-
"conn_max_life": schema.Int64Attribute{
168171
Optional: true,
169172
Computed: true,
170-
Default: int64default.StaticInt64(
171-
5 * 60,
172-
),
173-
Description: `The maximum lifetime in seconds of destination database.`,
173+
Default: int64default.StaticInt64(5),
174+
Description: `The maximum idling connections of destination database,
175+
if the given SQL file is using single transaction, should turn down the "conn_max_idle" to 1.
176+
`,
177+
Validators: []validator.Int64{
178+
int64validator.AtLeast(1),
179+
int64validator.AtMostSumOf(
180+
path.MatchRelative().AtParent().AtName("conn_max_open")),
181+
},
174182
},
175183
"salt": schema.StringAttribute{
176184
Optional: true,

byteset/resource_pipeline_test.go

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ import (
88
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
99

1010
"github.com/seal-io/terraform-provider-byteset/utils/strx"
11+
"github.com/seal-io/terraform-provider-byteset/utils/testx"
1112
)
1213

13-
func TestAccResourcePipeline_sqlite(t *testing.T) {
14+
func TestAccResourcePipeline_file_to_sqlite(t *testing.T) {
1415
// Test pipeline.
1516
var (
17+
testdataPath = testx.AbsolutePath("testdata")
18+
1619
resourceName = "byteset_pipeline.test"
1720

18-
basicSrc = fmt.Sprintf("file://%s/sqlite.sql", testdataPath())
21+
basicSrc = fmt.Sprintf("file://%s/sqlite.sql", testdataPath)
1922
basicDst = "sqlite:///tmp/sqlite.db"
2023

21-
fkSrc = fmt.Sprintf("file://%s/sqlite-fk.sql", testdataPath())
24+
fkSrc = fmt.Sprintf("file://%s/sqlite-fk.sql", testdataPath)
2225
fkDst = "sqlite:///tmp/sqlite.db?_pragma=foreign_keys(1)"
2326
)
2427

@@ -28,28 +31,29 @@ func TestAccResourcePipeline_sqlite(t *testing.T) {
2831
Steps: []resource.TestStep{
2932
// Basic.
3033
{
31-
Config: testConfig(basicSrc, basicDst),
34+
Config: testConfigOfSourceFile(basicSrc, basicDst, 1, 1),
3235
Check: resource.ComposeAggregateTestCheckFunc(
3336
resource.TestCheckResourceAttr(resourceName, "source.address", basicSrc),
3437
resource.TestCheckResourceAttr(resourceName, "destination.address", basicDst),
35-
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_open", "15"),
36-
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_idle", "5"),
37-
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_life", "300"),
38+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_open", "1"),
39+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_idle", "1"),
3840
),
3941
},
4042
// Foreign Key.
4143
{
42-
Config: testConfig(fkSrc, fkDst),
44+
Config: testConfigOfSourceFile(fkSrc, fkDst, 1, 1),
4345
Check: resource.ComposeAggregateTestCheckFunc(
4446
resource.TestCheckResourceAttr(resourceName, "source.address", fkSrc),
4547
resource.TestCheckResourceAttr(resourceName, "destination.address", fkDst),
48+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_open", "1"),
49+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_idle", "1"),
4650
),
4751
},
4852
},
4953
})
5054
}
5155

52-
func TestAccResourcePipeline_mysql(t *testing.T) {
56+
func TestAccResourcePipeline_file_to_mysql(t *testing.T) {
5357
// Start Database.
5458
var (
5559
database = "byteset"
@@ -78,12 +82,13 @@ func TestAccResourcePipeline_mysql(t *testing.T) {
7882

7983
// Test pipeline.
8084
var (
85+
testdataPath = testx.AbsolutePath("testdata")
8186
resourceName = "byteset_pipeline.test"
8287

83-
basicSrc = fmt.Sprintf("file://%s/mysql.sql", testdataPath())
88+
basicSrc = fmt.Sprintf("file://%s/mysql.sql", testdataPath)
8489
basicDst = fmt.Sprintf("mysql://root:%s@tcp(127.0.0.1:3306)/%s", password, database)
8590

86-
fkSrc = fmt.Sprintf("file://%s/mysql-fk.sql", testdataPath())
91+
fkSrc = fmt.Sprintf("file://%s/mysql-fk.sql", testdataPath)
8792
fkDst = fmt.Sprintf("mysql://root:%s@tcp(127.0.0.1)/%s", password, database)
8893

8994
largeSrc = "https://raw.githubusercontent.com/seal-io/terraform-provider-byteset/main/byteset/testdata/mysql-lg.sql"
@@ -95,31 +100,37 @@ func TestAccResourcePipeline_mysql(t *testing.T) {
95100
ProtoV6ProviderFactories: testAccProviderFactories,
96101
Steps: []resource.TestStep{
97102
{
98-
Config: testConfig(basicSrc, basicDst),
103+
Config: testConfigOfSourceFile(basicSrc, basicDst, 5, 5),
99104
Check: resource.ComposeAggregateTestCheckFunc(
100105
resource.TestCheckResourceAttr(resourceName, "source.address", basicSrc),
101106
resource.TestCheckResourceAttr(resourceName, "destination.address", basicDst),
107+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_open", "5"),
108+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_idle", "5"),
102109
),
103110
},
104111
{
105-
Config: testConfig(fkSrc, fkDst),
112+
Config: testConfigOfSourceFile(fkSrc, fkDst, 5, 5),
106113
Check: resource.ComposeAggregateTestCheckFunc(
107114
resource.TestCheckResourceAttr(resourceName, "source.address", fkSrc),
108115
resource.TestCheckResourceAttr(resourceName, "destination.address", fkDst),
116+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_open", "5"),
117+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_idle", "5"),
109118
),
110119
},
111120
{
112-
Config: testConfig(largeSrc, largeDst),
121+
Config: testConfigOfSourceFile(largeSrc, largeDst, 5, 5),
113122
Check: resource.ComposeAggregateTestCheckFunc(
114123
resource.TestCheckResourceAttr(resourceName, "source.address", largeSrc),
115124
resource.TestCheckResourceAttr(resourceName, "destination.address", largeDst),
125+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_open", "5"),
126+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_idle", "5"),
116127
),
117128
},
118129
},
119130
})
120131
}
121132

122-
func TestAccResourcePipeline_postgres(t *testing.T) {
133+
func TestAccResourcePipeline_file_to_postgres(t *testing.T) {
123134
// Start Database.
124135
var (
125136
database = "byteset"
@@ -149,12 +160,13 @@ func TestAccResourcePipeline_postgres(t *testing.T) {
149160

150161
// Test pipeline.
151162
var (
163+
testdataPath = testx.AbsolutePath("testdata")
152164
resourceName = "byteset_pipeline.test"
153165

154-
basicSrc = fmt.Sprintf("file://%s/postgres.sql", testdataPath())
166+
basicSrc = fmt.Sprintf("file://%s/postgres.sql", testdataPath)
155167
basicDst = fmt.Sprintf("postgres://root:%[email protected]:5432/%s?sslmode=disable", password, database)
156168

157-
fkSrc = fmt.Sprintf("file://%s/postgres-fk.sql", testdataPath())
169+
fkSrc = fmt.Sprintf("file://%s/postgres-fk.sql", testdataPath)
158170
fkDst = fmt.Sprintf("postgres://root:%[email protected]/%s?sslmode=disable", password, database)
159171

160172
largeSrc = "https://raw.githubusercontent.com/seal-io/terraform-provider-byteset/main/byteset/testdata/postgres-lg.sql"
@@ -166,40 +178,52 @@ func TestAccResourcePipeline_postgres(t *testing.T) {
166178
ProtoV6ProviderFactories: testAccProviderFactories,
167179
Steps: []resource.TestStep{
168180
{
169-
Config: testConfig(basicSrc, basicDst),
181+
Config: testConfigOfSourceFile(basicSrc, basicDst, 5, 5),
170182
Check: resource.ComposeAggregateTestCheckFunc(
171183
resource.TestCheckResourceAttr(resourceName, "source.address", basicSrc),
172184
resource.TestCheckResourceAttr(resourceName, "destination.address", basicDst),
185+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_open", "5"),
186+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_idle", "5"),
173187
),
174188
},
175189
{
176-
Config: testConfig(fkSrc, fkDst),
190+
Config: testConfigOfSourceFile(fkSrc, fkDst, 5, 5),
177191
Check: resource.ComposeAggregateTestCheckFunc(
178192
resource.TestCheckResourceAttr(resourceName, "source.address", fkSrc),
179193
resource.TestCheckResourceAttr(resourceName, "destination.address", fkDst),
194+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_open", "5"),
195+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_idle", "5"),
180196
),
181197
},
182198
{
183-
Config: testConfig(largeSrc, largeDst),
199+
Config: testConfigOfSourceFile(largeSrc, largeDst, 5, 5),
184200
Check: resource.ComposeAggregateTestCheckFunc(
185201
resource.TestCheckResourceAttr(resourceName, "source.address", largeSrc),
186202
resource.TestCheckResourceAttr(resourceName, "destination.address", largeDst),
203+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_open", "5"),
204+
resource.TestCheckResourceAttr(resourceName, "destination.conn_max_idle", "5"),
187205
),
188206
},
189207
},
190208
})
191209
}
192210

193-
func testConfig(src, dst string) string {
211+
func testConfigOfSourceFile(src, dst string, dstMaxOpen, dstMaxIdle int) string {
194212
const tmpl = `
195213
resource "byteset_pipeline" "test" {
196214
source = {
197215
address = "{{ .Src }}"
198216
}
199217
destination = {
200218
address = "{{ .Dst }}"
219+
conn_max_open = {{ .DstMaxOpen }}
220+
conn_max_idle = {{ .DstMaxIdle }}
201221
}
202222
}`
203223

204-
return renderConfigTemplate(tmpl, "Src", src, "Dst", dst)
224+
return renderConfigTemplate(tmpl,
225+
"Src", src,
226+
"Dst", dst,
227+
"DstMaxOpen", dstMaxOpen,
228+
"DstMaxIdle", dstMaxIdle)
205229
}

byteset/testdata/mysql-fk.sql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ CREATE TABLE members
2121
);
2222

2323
-- members data
24-
;;
25-
;;
2624
INSERT INTO members (id, last_name, first_name, team_id)
2725
VALUES (1, 'Lucy', 'Li', 1);
2826
INSERT INTO members (id, last_name, first_name, team_id)
@@ -33,8 +31,6 @@ INSERT INTO members (id, last_name, first_name, team_id)
3331
VALUES (4, 'Frank', NULL, 2);
3432

3533
-- teams data
36-
;;
37-
;;
3834
INSERT INTO teams (id, name)
3935
VALUES (1, 'Finance');
4036
INSERT INTO teams (id, name)

byteset/testdata/mysql.sql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ CREATE TABLE company
77
age INT NOT NULL,
88
address CHAR(50),
99
salary NUMERIC
10-
);;
10+
);
1111

1212

1313
-- company data
14-
;;
15-
;;
1614
INSERT INTO company (name, age, address, salary)
1715
VALUES ('Paul', 32, 'California', 20000.00);
1816
INSERT INTO company (name, age, address, salary)

byteset/testdata/postgres-fk.sql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ CREATE TABLE members
2222

2323

2424
-- members data
25-
;;
26-
;;
2725
INSERT INTO members (id, last_name, first_name, team_id)
2826
VALUES (1, 'Lucy', 'Li', 1);
2927
INSERT INTO members (id, last_name, first_name, team_id)
@@ -34,8 +32,6 @@ INSERT INTO members (id, last_name, first_name, team_id)
3432
VALUES (4, 'Frank', NULL, 2);
3533

3634
-- teams data
37-
;;
38-
;;
3935
INSERT INTO teams (id, name)
4036
VALUES (1, 'Finance');
4137
INSERT INTO teams (id, name)

byteset/testdata/postgres.sql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ CREATE TABLE company
77
age INT NOT NULL,
88
address CHAR(50),
99
salary REAL
10-
);;
10+
);
1111

1212

1313
-- company data
14-
;;
15-
;;
1614
INSERT INTO company (name, age, address, salary)
1715
VALUES ('Paul', 32, 'California', 20000.00);
1816
INSERT INTO company (name, age, address, salary)

0 commit comments

Comments
 (0)