Skip to content

Commit 52c4f78

Browse files
committed
gcp/changestreams: refactor dialect to use databasepb.DatabaseDialect and add test
Fixes CON-65
1 parent 58ca2fe commit 52c4f78

File tree

2 files changed

+75
-24
lines changed

2 files changed

+75
-24
lines changed

internal/impl/gcp/changestreams/dialect.go

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,33 @@ package changestreams
3131

3232
import (
3333
"context"
34-
"fmt"
3534

3635
"cloud.google.com/go/spanner"
36+
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
3737
)
3838

39-
type dialect int
39+
type dialect = databasepb.DatabaseDialect
4040

41-
const (
42-
dialectUnknown dialect = iota
43-
dialectGoogleSQL
44-
dialectPostgreSQL
41+
var (
42+
dialectGoogleSQL = databasepb.DatabaseDialect_GOOGLE_STANDARD_SQL
43+
dialectPostgreSQL = databasepb.DatabaseDialect_POSTGRESQL
4544
)
4645

47-
func (d dialect) String() string {
48-
switch d {
49-
case dialectGoogleSQL:
50-
return "GoogleSQL"
51-
case dialectPostgreSQL:
52-
return "PostgreSQL"
53-
default:
54-
return ""
55-
}
56-
}
57-
5846
func detectDialect(ctx context.Context, client *spanner.Client) (dialect, error) {
59-
var value string
60-
stmt := spanner.NewStatement("SELECT option_value FROM information_schema.database_options WHERE option_name = 'database_dialect'")
61-
if err := client.Single().Query(ctx, stmt).Do(func(r *spanner.Row) error {
62-
return r.ColumnByName("option_value", &value)
47+
const stmt = "SELECT option_value FROM information_schema.database_options WHERE option_name = 'database_dialect'"
48+
var v string
49+
if err := client.Single().Query(ctx, spanner.NewStatement(stmt)).Do(func(r *spanner.Row) error {
50+
return r.ColumnByName("option_value", &v)
6351
}); err != nil {
64-
return dialectUnknown, err
52+
return databasepb.DatabaseDialect_DATABASE_DIALECT_UNSPECIFIED, err
6553
}
6654

67-
switch value {
55+
switch v {
6856
case "GOOGLE_STANDARD_SQL", "":
6957
return dialectGoogleSQL, nil
7058
case "POSTGRESQL":
7159
return dialectPostgreSQL, nil
7260
default:
73-
return dialectUnknown, fmt.Errorf("invalid dialect: %q", value)
61+
return databasepb.DatabaseDialect_DATABASE_DIALECT_UNSPECIFIED, nil
7462
}
7563
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2025 Redpanda Data, Inc.
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 changestreams
16+
17+
import (
18+
"fmt"
19+
"testing"
20+
21+
adminpb "cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
22+
)
23+
24+
func TestIntegrationDetectDialect(t *testing.T) {
25+
if testing.Short() {
26+
t.Skip("Skipping test in short mode")
27+
}
28+
29+
adm := newEmulatorHelper(t)
30+
31+
testCases := []struct {
32+
dialect dialect
33+
fn func(*adminpb.CreateDatabaseRequest)
34+
}{
35+
{
36+
dialect: dialectGoogleSQL,
37+
},
38+
{
39+
dialect: dialectPostgreSQL,
40+
fn: func(req *adminpb.CreateDatabaseRequest) {
41+
req.DatabaseDialect = dialectPostgreSQL
42+
},
43+
},
44+
}
45+
46+
for i, tc := range testCases {
47+
t.Run(tc.dialect.String(), func(t *testing.T) {
48+
dbName := fmt.Sprintf("dialect%d", i)
49+
50+
var opts []func(*adminpb.CreateDatabaseRequest)
51+
if tc.fn != nil {
52+
opts = append(opts, tc.fn)
53+
}
54+
dd, err := detectDialect(t.Context(), adm.CreateTestDatabase(dbName, opts...))
55+
if err != nil {
56+
t.Fatalf("failed to detect dialect: %v", err)
57+
}
58+
if dd != tc.dialect {
59+
t.Fatalf("expected dialect %s, got %s", tc.dialect, dd)
60+
}
61+
})
62+
}
63+
}

0 commit comments

Comments
 (0)