Skip to content

Commit

Permalink
feat: add starrocks dialect (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
barloc authored Sep 18, 2024
1 parent b831b82 commit cf53a22
Show file tree
Hide file tree
Showing 16 changed files with 302 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ DB_MYSQL_PORT ?= 3307
DB_CLICKHOUSE_PORT ?= 9001
DB_YDB_PORT ?= 2136
DB_TURSO_PORT ?= 8080
DB_STARROCKS_PORT ?= 9030

list-build-tags:
@echo "Available build tags:"
Expand Down Expand Up @@ -86,6 +87,9 @@ test-vertica: add-gowork
test-ydb: add-gowork
go test $(GO_TEST_FLAGS) ./internal/testing/integration -run='TestYDB'

test-starrocks: add-gowork
go test $(GO_TEST_FLAGS) ./internal/testing/integration -run='TestStarrocks'

test-integration: add-gowork
go test $(GO_TEST_FLAGS) ./internal/testing/integration/...

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Drivers:
clickhouse
vertica
ydb
starrocks
Examples:
goose sqlite3 ./foo.db status
Expand All @@ -95,6 +96,7 @@ Examples:
goose clickhouse "tcp://127.0.0.1:9000" status
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_DRIVER=sqlite3 GOOSE_DBSTRING=./foo.db goose status
GOOSE_DRIVER=sqlite3 GOOSE_DBSTRING=./foo.db goose create init sql
Expand Down
2 changes: 2 additions & 0 deletions database/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
DialectTurso Dialect = "turso"
DialectVertica Dialect = "vertica"
DialectYdB Dialect = "ydb"
DialectStarrocks Dialect = "starrocks"
)

// NewStore returns a new [Store] implementation for the given dialect.
Expand All @@ -44,6 +45,7 @@ func NewStore(dialect Dialect, tablename string) (Store, error) {
DialectVertica: &dialectquery.Vertica{},
DialectYdB: &dialectquery.Ydb{},
DialectTurso: &dialectquery.Turso{},
DialectStarrocks: &dialectquery.Starrocks{},
}
querier, ok := lookup[dialect]
if !ok {
Expand Down
4 changes: 3 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ func OpenDBWithDriver(driver string, dbstring string) (*sql.DB, error) {
driver = "sqlite"
case "postgres", "redshift":
driver = "pgx"
case "starrocks":
driver = "mysql"
}

switch driver {
case "postgres", "pgx", "sqlite3", "sqlite", "mysql", "sqlserver", "clickhouse", "vertica", "azuresql", "ydb", "libsql":
case "postgres", "pgx", "sqlite3", "sqlite", "mysql", "sqlserver", "clickhouse", "vertica", "azuresql", "ydb", "libsql", "starrocks":
return sql.Open(driver, dbstring)
default:
return nil, fmt.Errorf("unsupported driver %s", driver)
Expand Down
3 changes: 3 additions & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
DialectTiDB Dialect = database.DialectTiDB
DialectVertica Dialect = database.DialectVertica
DialectYdB Dialect = database.DialectYdB
DialectStarrocks Dialect = database.DialectStarrocks
)

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

import "fmt"

type Starrocks struct{}

var _ Querier = (*Starrocks)(nil)

func (m *Starrocks) CreateTable(tableName string) string {
q := `CREATE TABLE IF NOT EXISTS %s (
id bigint NOT NULL AUTO_INCREMENT,
version_id bigint NOT NULL,
is_applied boolean NOT NULL,
tstamp datetime NULL default CURRENT_TIMESTAMP
)
PRIMARY KEY (id)
DISTRIBUTED BY HASH (id)
ORDER BY (id,version_id)`
return fmt.Sprintf(q, tableName)
}

func (m *Starrocks) InsertVersion(tableName string) string {
q := `INSERT INTO %s (version_id, is_applied) VALUES (?, ?)`
return fmt.Sprintf(q, tableName)
}

func (m *Starrocks) DeleteVersion(tableName string) string {
q := `DELETE FROM %s WHERE version_id=?`
return fmt.Sprintf(q, tableName)
}

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

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

func (m *Starrocks) 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 @@ -14,4 +14,5 @@ const (
Vertica Dialect = "vertica"
Ydb Dialect = "ydb"
Turso Dialect = "turso"
Starrocks Dialect = "starrocks"
)
2 changes: 2 additions & 0 deletions internal/dialect/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func NewStore(d Dialect) (Store, error) {
querier = &dialectquery.Ydb{}
case Turso:
querier = &dialectquery.Turso{}
case Starrocks:
querier = &dialectquery.Starrocks{}
default:
return nil, fmt.Errorf("unknown querier dialect: %v", d)
}
Expand Down
20 changes: 20 additions & 0 deletions internal/testing/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@ module github.com/pressly/goose/v3/internal/testing
go 1.22.1

require (
github.com/ClickHouse/clickhouse-go/v2 v2.28.3
github.com/ClickHouse/clickhouse-go/v2 v2.28.3
github.com/go-sql-driver/mysql v1.8.1
github.com/jackc/pgx/v5 v5.7.1
github.com/jackc/pgx/v5 v5.7.1
github.com/ory/dockertest/v3 v3.11.0
github.com/pressly/goose/v3 v3.22.0
github.com/pressly/goose/v3 v3.22.0
github.com/sethvargo/go-retry v0.3.0
github.com/stretchr/testify v1.9.0
github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d
github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d
github.com/vertica/vertica-sql-go v1.3.3
github.com/ydb-platform/ydb-go-sdk/v3 v3.80.2
github.com/ydb-platform/ydb-go-sdk/v3 v3.80.2
golang.org/x/sync v0.8.0
)

require (
dario.cat/mergo v1.0.1 // indirect
dario.cat/mergo v1.0.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
Expand All @@ -31,6 +37,8 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v27.2.1+incompatible // indirect
github.com/docker/docker v27.2.1+incompatible // indirect
github.com/docker/cli v27.2.1+incompatible // indirect
github.com/docker/docker v27.2.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/elastic/go-sysinfo v1.14.1 // indirect
Expand All @@ -45,6 +53,7 @@ require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kr/text v0.2.0 // indirect
Expand All @@ -54,6 +63,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/opencontainers/runc v1.1.14 // indirect
github.com/opencontainers/runc v1.1.14 // indirect
github.com/paulmach/orb v0.11.1 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand All @@ -68,6 +78,9 @@ require (
github.com/ydb-platform/ydb-go-genproto v0.0.0-20240821162910-6cb364b2ccc8 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
github.com/ydb-platform/ydb-go-genproto v0.0.0-20240821162910-6cb364b2ccc8 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
Expand All @@ -76,6 +89,13 @@ require (
golang.org/x/text v0.18.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/grpc v1.66.2 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/grpc v1.66.2 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit cf53a22

Please sign in to comment.