Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dialect for spanner using pg adapter #900

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ go build -tags='no_postgres no_mysql no_sqlite3 no_ydb' -o goose ./cmd/goose
# Available build tags:
# no_clickhouse no_libsql no_mssql no_mysql
# no_postgres no_sqlite3 no_vertica no_ydb
# no_spannerpg
```

For macOS users `goose` is available as a [Homebrew
Expand Down Expand Up @@ -97,6 +98,7 @@ Examples:
goose vertica "vertica://user:password@localhost:5433/dbname?connection_load_balance=1" status
goose ydb "grpcs://localhost:2135/local?go_query_mode=scripting&go_fake_tx=scripting&go_query_bind=declare,numeric" status
goose starrocks "user:password@/dbname?parseTime=true&interpolateParams=true" status
goose spannerpg "user=postgres dbname=postgres sslmode=disable" status
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assume the PGAdapter is running locally on the default postgres port.


GOOSE_DRIVER=sqlite3 GOOSE_DBSTRING=./foo.db goose status
GOOSE_DRIVER=sqlite3 GOOSE_DBSTRING=./foo.db goose create init sql
Expand Down
9 changes: 9 additions & 0 deletions cmd/goose/driver_spannerpg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !no_spannerpg
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure about this file, since Spanner PG would use the regular Postgres PGX driver, so should we just use "no_postgres" for both postgres and spanner?

// +build !no_spannerpg

package main

//Spanner using PG (postgres) adapter. Using pgx as a driver.
import (
_ "github.com/jackc/pgx/v5/stdlib"
)
2 changes: 2 additions & 0 deletions database/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
DialectVertica Dialect = "vertica"
DialectYdB Dialect = "ydb"
DialectStarrocks Dialect = "starrocks"
DialectSpannerPG Dialect = "spannerpg"
)

// NewStore returns a new [Store] implementation for the given dialect.
Expand All @@ -46,6 +47,7 @@ func NewStore(dialect Dialect, tablename string) (Store, error) {
DialectYdB: &dialectquery.Ydb{},
DialectTurso: &dialectquery.Turso{},
DialectStarrocks: &dialectquery.Starrocks{},
DialectSpannerPG: &dialectquery.SpannerPG{},
}
querier, ok := lookup[dialect]
if !ok {
Expand Down
3 changes: 3 additions & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
DialectVertica Dialect = database.DialectVertica
DialectYdB Dialect = database.DialectYdB
DialectStarrocks Dialect = database.DialectStarrocks
DialectSpannerPG Dialect = database.DialectSpannerPG
)

func init() {
Expand Down Expand Up @@ -55,6 +56,8 @@ func SetDialect(s string) error {
d = dialect.Turso
case "starrocks":
d = dialect.Starrocks
case "spannerpg":
d = dialect.SpannerPG
default:
return fmt.Errorf("%q: unknown dialect", s)
}
Expand Down
44 changes: 44 additions & 0 deletions internal/dialect/dialectquery/spannerpg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package dialectquery
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spanner via PGAdapter only has partial support for current_schema, and we can only query it directly via select current_schema but not in a where clause. For this reason, I removed the Exists to use the fallback method like the mssql dialect does.


import (
"fmt"
)

type SpannerPG struct{}

var _ Querier = (*SpannerPG)(nil)

func (p *SpannerPG) CreateTable(tableName string) string {
q := `CREATE TABLE %s (
id bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (BIT_REVERSED_POSITIVE),
version_id bigint NOT NULL,
is_applied boolean NOT NULL,
tstamp timestamptz NOT NULL DEFAULT now()
)`
return fmt.Sprintf(q, tableName)
}

func (p *SpannerPG) InsertVersion(tableName string) string {
q := `INSERT INTO %s (version_id, is_applied) VALUES ($1, $2)`
return fmt.Sprintf(q, tableName)
}

func (p *SpannerPG) DeleteVersion(tableName string) string {
q := `DELETE FROM %s WHERE version_id=$1`
return fmt.Sprintf(q, tableName)
}

func (p *SpannerPG) GetMigrationByVersion(tableName string) string {
q := `SELECT tstamp, is_applied FROM %s WHERE version_id=$1 ORDER BY tstamp DESC LIMIT 1`
return fmt.Sprintf(q, tableName)
}

func (p *SpannerPG) ListMigrations(tableName string) string {
q := `SELECT version_id, is_applied from %s ORDER BY id DESC`
return fmt.Sprintf(q, tableName)
}

func (p *SpannerPG) GetLatestVersion(tableName string) string {
q := `SELECT max(version_id) FROM %s`
return fmt.Sprintf(q, tableName)
}
1 change: 1 addition & 0 deletions internal/dialect/dialects.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ const (
Ydb Dialect = "ydb"
Turso Dialect = "turso"
Starrocks Dialect = "starrocks"
SpannerPG Dialect = "spannerpg"
)
2 changes: 2 additions & 0 deletions internal/dialect/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func NewStore(d Dialect) (Store, error) {
querier = &dialectquery.Turso{}
case Starrocks:
querier = &dialectquery.Starrocks{}
case SpannerPG:
querier = &dialectquery.SpannerPG{}
default:
return nil, fmt.Errorf("unknown querier dialect: %v", d)
}
Expand Down