Skip to content

Commit

Permalink
export Kind and StatementKind
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryQ committed Oct 6, 2024
1 parent 80abbf4 commit 56c2cc0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
8 changes: 4 additions & 4 deletions pkg/spanner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,15 +654,15 @@ func (c *Client) ExecuteMigrations(ctx context.Context, migrations Migrations, l
}
}

switch m.kind {
case statementKindDDL:
switch m.Kind {
case StatementKindDDL:
if err := c.ApplyDDL(ctx, m.Statements); err != nil {
return nil, &Error{
Code: ErrorCodeExecuteMigrations,
err: err,
}
}
case statementKindDML:
case StatementKindDML:
rowsAffected, err := c.ApplyDML(ctx, m.Statements)
if err != nil {
return nil, &Error{
Expand All @@ -674,7 +674,7 @@ func (c *Client) ExecuteMigrations(ctx context.Context, migrations Migrations, l
migrationsOutput[m.FileName] = migrationInfo{
RowsAffected: rowsAffected,
}
case statementKindPartitionedDML:
case StatementKindPartitionedDML:
rowsAffected, err := c.ApplyPartitionedDML(ctx, m.Statements, partitionedConcurrency)
if err != nil {
return nil, &Error{
Expand Down
48 changes: 24 additions & 24 deletions pkg/spanner/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ var (
)

const (
statementKindDDL statementKind = "DDL"
statementKindDML statementKind = "DML"
statementKindPartitionedDML statementKind = "PartitionedDML"
StatementKindDDL StatementKind = "DDL"
StatementKindDML StatementKind = "DML"
StatementKindPartitionedDML StatementKind = "PartitionedDML"
)

type (
Expand All @@ -77,12 +77,12 @@ type (
// Statements is the migration statements
Statements []string

kind statementKind
Kind StatementKind
}

Migrations []*Migration

statementKind string
StatementKind string
)

func (ms Migrations) Len() int {
Expand Down Expand Up @@ -144,7 +144,7 @@ func LoadMigrations(dir string, toSkipSlice []uint, detectPartitionedDML bool) (
Name: matches[2],
FileName: f.Name(),
Statements: statements,
kind: kind,
Kind: kind,
})
}

Expand Down Expand Up @@ -185,38 +185,38 @@ func toStatements(file []byte) []string {
return statements
}

func inspectStatementsKind(statements []string, detectPartitionedDML bool) (statementKind, error) {
kindMap := map[statementKind]uint64{
statementKindDDL: 0,
statementKindDML: 0,
statementKindPartitionedDML: 0,
func inspectStatementsKind(statements []string, detectPartitionedDML bool) (StatementKind, error) {
kindMap := map[StatementKind]uint64{
StatementKindDDL: 0,
StatementKindDML: 0,
StatementKindPartitionedDML: 0,
}

for _, s := range statements {
kindMap[getStatementKind(s)]++
}

if distinctKind(kindMap, statementKindDDL) {
return statementKindDDL, nil
if distinctKind(kindMap, StatementKindDDL) {
return StatementKindDDL, nil
}

// skip further DML type inspection unless detectPartitionedDML is true
if !detectPartitionedDML && distinctKind(kindMap, statementKindDML, statementKindPartitionedDML) {
return statementKindDML, nil
if !detectPartitionedDML && distinctKind(kindMap, StatementKindDML, StatementKindPartitionedDML) {
return StatementKindDML, nil
}

if detectPartitionedDML && distinctKind(kindMap, statementKindDML) {
return statementKindDML, nil
if detectPartitionedDML && distinctKind(kindMap, StatementKindDML) {
return StatementKindDML, nil
}

if detectPartitionedDML && distinctKind(kindMap, statementKindPartitionedDML) {
return statementKindPartitionedDML, nil
if detectPartitionedDML && distinctKind(kindMap, StatementKindPartitionedDML) {
return StatementKindPartitionedDML, nil
}

return "", errors.New("Cannot specify DDL and DML in the same migration file")
}

func distinctKind(kindMap map[statementKind]uint64, kinds ...statementKind) bool {
func distinctKind(kindMap map[StatementKind]uint64, kinds ...StatementKind) bool {
// sum the target statement kinds
var target uint64
for _, k := range kinds {
Expand All @@ -232,16 +232,16 @@ func distinctKind(kindMap map[statementKind]uint64, kinds ...statementKind) bool
return target == total
}

func getStatementKind(statement string) statementKind {
func getStatementKind(statement string) StatementKind {
if isPartitionedDMLOnly(statement) {
return statementKindPartitionedDML
return StatementKindPartitionedDML
}

if isDMLAny(statement) {
return statementKindDML
return StatementKindDML
}

return statementKindDDL
return StatementKindDDL
}

func isPartitionedDMLOnly(statement string) bool {
Expand Down
24 changes: 12 additions & 12 deletions pkg/spanner/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,27 @@ func Test_getStatementKind(t *testing.T) {
tests := []struct {
name string
statement string
want statementKind
want StatementKind
}{
{
"ALTER statement is DDL",
TestStmtDDL,
statementKindDDL,
StatementKindDDL,
},
{
"UPDATE statement is PartitionedDML",
TestStmtPartitionedDML,
statementKindPartitionedDML,
StatementKindPartitionedDML,
},
{
"INSERT statement is DML",
TestStmtDML,
statementKindDML,
StatementKindDML,
},
{
"lowercase insert statement is DML",
TestStmtDML,
statementKindDML,
StatementKindDML,
},
}
for _, tt := range tests {
Expand All @@ -136,31 +136,31 @@ func Test_inspectStatementsKind(t *testing.T) {
tests := []struct {
name string
statements []string
want statementKind
want StatementKind
detectPartitionedDML bool
wantErr bool
}{
{
name: "Only DDL returns DDL",
statements: []string{TestStmtDDL, TestStmtDDL},
want: statementKindDDL,
want: StatementKindDDL,
},
{
name: "Only PartitionedDML returns PartitionedDML",
statements: []string{TestStmtPartitionedDML, TestStmtPartitionedDML},
want: statementKindPartitionedDML,
want: StatementKindPartitionedDML,
detectPartitionedDML: true,
},
{
name: "No PartitionedDML detection returns DML",
statements: []string{TestStmtPartitionedDML, TestStmtPartitionedDML},
want: statementKindDML,
want: StatementKindDML,
detectPartitionedDML: false,
},
{
name: "Only DML returns DML",
statements: []string{TestStmtDML, TestStmtDML},
want: statementKindDML,
want: StatementKindDML,
},
{
name: "DML and DDL returns error",
Expand All @@ -170,7 +170,7 @@ func Test_inspectStatementsKind(t *testing.T) {
{
name: "DML and undetected PartitionedDML returns DML",
statements: []string{TestStmtDML, TestStmtPartitionedDML},
want: statementKindDML,
want: StatementKindDML,
},
{
name: "DML and detected PartitionedDML returns error",
Expand All @@ -192,7 +192,7 @@ func Test_inspectStatementsKind(t *testing.T) {
{
name: "no statements defaults to DDL as before",
statements: []string{},
want: statementKindDDL,
want: StatementKindDDL,
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 56c2cc0

Please sign in to comment.