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

pkg/spanner - breaking - remove migration tableName parameter from API #51

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
9 changes: 5 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ const (

func newSpannerClient(ctx context.Context, c *cobra.Command) (*spanner.Client, error) {
config := &spanner.Config{
Project: c.Flag(flagNameProject).Value.String(),
Instance: c.Flag(flagNameInstance).Value.String(),
Database: c.Flag(flagNameDatabase).Value.String(),
CredentialsFile: c.Flag(flagCredentialsFile).Value.String(),
Project: c.Flag(flagNameProject).Value.String(),
Instance: c.Flag(flagNameInstance).Value.String(),
Database: c.Flag(flagNameDatabase).Value.String(),
CredentialsFile: c.Flag(flagCredentialsFile).Value.String(),
MigrationTableName: "", // use pkg.spanner default
}

client, err := spanner.NewClient(ctx, config)
Expand Down
13 changes: 6 additions & 7 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (

const (
migrationsDirName = "migrations"
migrationTableName = "SchemaMigrations"
)

// migrateCmd represents the migrate command
Expand Down Expand Up @@ -126,7 +125,7 @@ func migrateUp(c *cobra.Command, args []string) error {
}
defer client.Close()

if err = client.EnsureMigrationTable(ctx, migrationTableName); err != nil {
if err = client.EnsureMigrationTable(ctx); err != nil {
return &Error{
cmd: c,
err: err,
Expand All @@ -142,7 +141,7 @@ func migrateUp(c *cobra.Command, args []string) error {
}
}

return client.ExecuteMigrations(ctx, migrations, limit, migrationTableName)
return client.ExecuteMigrations(ctx, migrations, limit)
}

func migrateVersion(c *cobra.Command, args []string) error {
Expand All @@ -154,14 +153,14 @@ func migrateVersion(c *cobra.Command, args []string) error {
}
defer client.Close()

if err = client.EnsureMigrationTable(ctx, migrationTableName); err != nil {
if err = client.EnsureMigrationTable(ctx); err != nil {
return &Error{
cmd: c,
err: err,
}
}

v, _, err := client.GetSchemaMigrationVersion(ctx, migrationTableName)
v, _, err := client.GetSchemaMigrationVersion(ctx)
if err != nil {
var se *spanner.Error
if errors.As(err, &se) && se.Code == spanner.ErrorCodeNoMigration {
Expand Down Expand Up @@ -202,14 +201,14 @@ func migrateSet(c *cobra.Command, args []string) error {
}
defer client.Close()

if err = client.EnsureMigrationTable(ctx, migrationTableName); err != nil {
if err = client.EnsureMigrationTable(ctx); err != nil {
return &Error{
cmd: c,
err: err,
}
}

if err := client.SetSchemaMigrationVersion(ctx, uint(version), false, migrationTableName); err != nil {
if err := client.SetSchemaMigrationVersion(ctx, uint(version), false); err != nil {
return &Error{
cmd: c,
err: err,
Expand Down
27 changes: 19 additions & 8 deletions pkg/spanner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (

const (
ddlStatementsSeparator = ";"
defaultMigrationTableName = "SchemaMigrations"
)

type table struct {
Expand Down Expand Up @@ -78,6 +79,13 @@ func NewClient(ctx context.Context, config *Config) (*Client, error) {
}, nil
}

func (c *Client) migrationTableName() string {
if c.config == nil || c.config.MigrationTableName == "" {
return defaultMigrationTableName
}
return c.config.MigrationTableName
}

func (c *Client) CreateDatabase(ctx context.Context, ddl []byte) error {
statements := toStatements(ddl)

Expand Down Expand Up @@ -131,7 +139,7 @@ func (c *Client) TruncateAllTables(ctx context.Context) error {
return err
}

if t.TableName == "SchemaMigrations" {
if t.TableName == c.migrationTableName() {
return nil
}

Expand Down Expand Up @@ -295,10 +303,10 @@ func (c *Client) ApplyPartitionedDML(ctx context.Context, statements []string, p
return numAffectedRows, nil
}

func (c *Client) ExecuteMigrations(ctx context.Context, migrations Migrations, limit int, tableName string) error {
func (c *Client) ExecuteMigrations(ctx context.Context, migrations Migrations, limit int) error {
sort.Sort(migrations)

version, dirty, err := c.GetSchemaMigrationVersion(ctx, tableName)
version, dirty, err := c.GetSchemaMigrationVersion(ctx)
if err != nil {
var se *Error
if !errors.As(err, &se) || se.Code != ErrorCodeNoMigration {
Expand Down Expand Up @@ -326,7 +334,7 @@ func (c *Client) ExecuteMigrations(ctx context.Context, migrations Migrations, l
continue
}

if err := c.SetSchemaMigrationVersion(ctx, m.Version, true, tableName); err != nil {
if err := c.SetSchemaMigrationVersion(ctx, m.Version, true); err != nil {
return &Error{
Code: ErrorCodeExecuteMigrations,
err: err,
Expand Down Expand Up @@ -361,7 +369,7 @@ func (c *Client) ExecuteMigrations(ctx context.Context, migrations Migrations, l
fmt.Printf("%d/up\n", m.Version)
}

if err := c.SetSchemaMigrationVersion(ctx, m.Version, false, tableName); err != nil {
if err := c.SetSchemaMigrationVersion(ctx, m.Version, false); err != nil {
return &Error{
Code: ErrorCodeExecuteMigrations,
err: err,
Expand All @@ -381,7 +389,8 @@ func (c *Client) ExecuteMigrations(ctx context.Context, migrations Migrations, l
return nil
}

func (c *Client) GetSchemaMigrationVersion(ctx context.Context, tableName string) (uint, bool, error) {
func (c *Client) GetSchemaMigrationVersion(ctx context.Context) (uint, bool, error) {
tableName := c.migrationTableName()
stmt := spanner.Statement{
SQL: `SELECT Version, Dirty FROM ` + tableName + ` LIMIT 1`,
}
Expand Down Expand Up @@ -416,7 +425,8 @@ func (c *Client) GetSchemaMigrationVersion(ctx context.Context, tableName string
return uint(v), dirty, nil
}

func (c *Client) SetSchemaMigrationVersion(ctx context.Context, version uint, dirty bool, tableName string) error {
func (c *Client) SetSchemaMigrationVersion(ctx context.Context, version uint, dirty bool) error {
tableName := c.migrationTableName()
_, err := c.spannerClient.ReadWriteTransaction(ctx, func(_ context.Context, tx *spanner.ReadWriteTransaction) error {
m := []*spanner.Mutation{
spanner.Delete(tableName, spanner.AllKeys()),
Expand All @@ -438,7 +448,8 @@ func (c *Client) SetSchemaMigrationVersion(ctx context.Context, version uint, di
return nil
}

func (c *Client) EnsureMigrationTable(ctx context.Context, tableName string) error {
func (c *Client) EnsureMigrationTable(ctx context.Context) error {
tableName := c.migrationTableName()
iter := c.spannerClient.Single().Read(ctx, tableName, spanner.AllKeys(), []string{"Version"})
err := iter.Do(func(r *spanner.Row) error {
return nil
Expand Down
38 changes: 29 additions & 9 deletions pkg/spanner/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,15 @@ func TestExecuteMigrations(t *testing.T) {
}

// only apply 000002.sql by specifying limit 1.
if err := client.ExecuteMigrations(ctx, migrations, 1, migrationTable); err != nil {
if err := client.ExecuteMigrations(ctx, migrations, 1); err != nil {
t.Fatalf("failed to execute migration: %v", err)
}

// ensure that only 000002.sql has been applied.
ensureMigrationColumn(t, ctx, client, "LastName", "STRING(MAX)", "YES")
ensureMigrationVersionRecord(t, ctx, client, 2, false)

if err := client.ExecuteMigrations(ctx, migrations, len(migrations), migrationTable); err != nil {
if err := client.ExecuteMigrations(ctx, migrations, len(migrations)); err != nil {
t.Fatalf("failed to execute migration: %v", err)
}

Expand Down Expand Up @@ -314,7 +314,7 @@ func TestGetSchemaMigrationVersion(t *testing.T) {
t.Fatalf("failed to apply mutation: %v", err)
}

v, d, err := client.GetSchemaMigrationVersion(ctx, migrationTable)
v, d, err := client.GetSchemaMigrationVersion(ctx)
if err != nil {
t.Fatalf("failed to get version: %v", err)
}
Expand Down Expand Up @@ -350,7 +350,7 @@ func TestSetSchemaMigrationVersion(t *testing.T) {
nextVersion := 2
nextDirty := true

if err := client.SetSchemaMigrationVersion(ctx, uint(nextVersion), nextDirty, migrationTable); err != nil {
if err := client.SetSchemaMigrationVersion(ctx, uint(nextVersion), nextDirty); err != nil {
t.Fatalf("failed to set version: %v", err)
}

Expand All @@ -360,8 +360,6 @@ func TestSetSchemaMigrationVersion(t *testing.T) {
func TestEnsureMigrationTable(t *testing.T) {
ctx := context.Background()

client, done := testClientWithDatabase(t, ctx)
defer done()

tests := map[string]struct {
table string
Expand All @@ -372,7 +370,12 @@ func TestEnsureMigrationTable(t *testing.T) {

for name, test := range tests {
t.Run(name, func(t *testing.T) {
if err := client.EnsureMigrationTable(ctx, test.table); err != nil {
cfg := &Config{
MigrationTableName: test.table,
}
client, done := testConfiguredClientWithDatabase(t, ctx, cfg)
defer done()
if err := client.EnsureMigrationTable(ctx); err != nil {
t.Fatalf("failed to ensure migration table: %v", err)
}

Expand Down Expand Up @@ -434,7 +437,12 @@ func TestPriorityPBOf(t *testing.T) {

}


func testClientWithDatabase(t *testing.T, ctx context.Context) (*Client, func()) {
return testConfiguredClientWithDatabase(t, ctx, &Config{})
}

func testConfiguredClientWithDatabase(t *testing.T, ctx context.Context, config *Config) (*Client, func()) {
t.Helper()

project := os.Getenv(envSpannerProjectID)
Expand All @@ -454,13 +462,25 @@ func testClientWithDatabase(t *testing.T, ctx context.Context) (*Client, func())
database = fmt.Sprintf("wrench-test-%s", id.String()[:8])
}

config := &Config{
mergedConfig := &Config{
Project: project,
Instance: instance,
Database: database,
}
if config != nil && config.Project != "" {
mergedConfig.Project = config.Project
}
if config != nil && config.Instance != "" {
mergedConfig.Instance = config.Instance
}
if config != nil && config.Database != "" {
mergedConfig.Database = config.Database
}
if config != nil && config.MigrationTableName != "" {
mergedConfig.MigrationTableName = config.MigrationTableName
}

client, err := NewClient(ctx, config)
client, err := NewClient(ctx, mergedConfig)
if err != nil {
t.Fatalf("failed to create spanner client: %v", err)
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/spanner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ package spanner
import "fmt"

type Config struct {
Project string
Instance string
Database string
CredentialsFile string
Project string
Instance string
Database string
CredentialsFile string
MigrationTableName string
}

func (c *Config) URL() string {
Expand Down