Skip to content

Commit 7cd5c5c

Browse files
committed
gcp/changestreams: refactor dialect to use databasepb.DatabaseDialect and add test
Fixes CON-65
1 parent f74f9aa commit 7cd5c5c

File tree

2 files changed

+69
-24
lines changed

2 files changed

+69
-24
lines changed

internal/impl/gcp/enterprise/changestreams/dialect.go

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

2626
import (
2727
"context"
28-
"fmt"
2928

3029
"cloud.google.com/go/spanner"
30+
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
3131
)
3232

33-
type dialect int
33+
type dialect = databasepb.DatabaseDialect
3434

35-
const (
36-
dialectUnknown dialect = iota
37-
dialectGoogleSQL
38-
dialectPostgreSQL
35+
var (
36+
dialectGoogleSQL = databasepb.DatabaseDialect_GOOGLE_STANDARD_SQL
37+
dialectPostgreSQL = databasepb.DatabaseDialect_POSTGRESQL
3938
)
4039

41-
func (d dialect) String() string {
42-
switch d {
43-
case dialectGoogleSQL:
44-
return "GoogleSQL"
45-
case dialectPostgreSQL:
46-
return "PostgreSQL"
47-
default:
48-
return ""
49-
}
50-
}
51-
5240
func detectDialect(ctx context.Context, client *spanner.Client) (dialect, error) {
53-
var value string
54-
stmt := spanner.NewStatement("SELECT option_value FROM information_schema.database_options WHERE option_name = 'database_dialect'")
55-
if err := client.Single().Query(ctx, stmt).Do(func(r *spanner.Row) error {
56-
return r.ColumnByName("option_value", &value)
41+
const stmt = "SELECT option_value FROM information_schema.database_options WHERE option_name = 'database_dialect'"
42+
var v string
43+
if err := client.Single().Query(ctx, spanner.NewStatement(stmt)).Do(func(r *spanner.Row) error {
44+
return r.ColumnByName("option_value", &v)
5745
}); err != nil {
58-
return dialectUnknown, err
46+
return databasepb.DatabaseDialect_DATABASE_DIALECT_UNSPECIFIED, err
5947
}
6048

61-
switch value {
49+
switch v {
6250
case "GOOGLE_STANDARD_SQL", "":
6351
return dialectGoogleSQL, nil
6452
case "POSTGRESQL":
6553
return dialectPostgreSQL, nil
6654
default:
67-
return dialectUnknown, fmt.Errorf("invalid dialect: %q", value)
55+
return databasepb.DatabaseDialect_DATABASE_DIALECT_UNSPECIFIED, nil
6856
}
6957
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2025 Redpanda Data, Inc.
2+
//
3+
// Licensed as a Redpanda Enterprise file under the Redpanda Community
4+
// License (the "License"); you may not use this file except in compliance with
5+
// the License. You may obtain a copy of the License at
6+
//
7+
// https://github.com/redpanda-data/connect/blob/main/licenses/rcl.md
8+
9+
package changestreams
10+
11+
import (
12+
"fmt"
13+
"testing"
14+
15+
adminpb "cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
16+
)
17+
18+
func TestIntegrationDetectDialect(t *testing.T) {
19+
if testing.Short() {
20+
t.Skip("Skipping test in short mode")
21+
}
22+
23+
adm := newEmulatorHelper(t)
24+
25+
testCases := []struct {
26+
dialect dialect
27+
fn func(*adminpb.CreateDatabaseRequest)
28+
}{
29+
{
30+
dialect: dialectGoogleSQL,
31+
},
32+
{
33+
dialect: dialectPostgreSQL,
34+
fn: func(req *adminpb.CreateDatabaseRequest) {
35+
req.DatabaseDialect = dialectPostgreSQL
36+
},
37+
},
38+
}
39+
40+
for i, tc := range testCases {
41+
t.Run(tc.dialect.String(), func(t *testing.T) {
42+
dbName := fmt.Sprintf("dialect%d", i)
43+
44+
var opts []func(*adminpb.CreateDatabaseRequest)
45+
if tc.fn != nil {
46+
opts = append(opts, tc.fn)
47+
}
48+
dd, err := detectDialect(t.Context(), adm.CreateTestDatabase(dbName, opts...))
49+
if err != nil {
50+
t.Fatalf("failed to detect dialect: %v", err)
51+
}
52+
if dd != tc.dialect {
53+
t.Fatalf("expected dialect %s, got %s", tc.dialect, dd)
54+
}
55+
})
56+
}
57+
}

0 commit comments

Comments
 (0)