Skip to content

Commit

Permalink
make the logic of inspectStatementsKind simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
knwoop committed Jul 8, 2024
1 parent 519be57 commit 4d503d5
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions pkg/spanner/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,41 +172,32 @@ func dmlToStatements(filename string, data []byte) ([]string, error) {
}

func inspectStatementsKind(statements []string) (statementKind, error) {
kindMap := map[statementKind]uint64{
statementKindDDL: 0,
statementKindDML: 0,
statementKindPartitionedDML: 0,
if len(statements) == 0 { // Treat empty files as DDL.
return statementKindDDL, nil
}

var hasDDL, hasDML, hasPartitionedDML bool
for _, s := range statements {
if isDML(s) {
kindMap[statementKindDML]++
} else if isPartitionedDML(s) {
kindMap[statementKindPartitionedDML]++
} else {
kindMap[statementKindDDL]++
switch {
case isDML(s):
hasDML = true
case isPartitionedDML(s):
hasPartitionedDML = true
default:
hasDDL = true
}
}

if kindMap[statementKindDDL] > 0 {
if kindMap[statementKindDML] > 0 || kindMap[statementKindPartitionedDML] > 0 {
return "", errors.New("cannot specify DDL with DML or partitioned DML in the same migration file")
}
switch {
case hasDDL && !hasDML && !hasPartitionedDML:
return statementKindDDL, nil
}

if kindMap[statementKindDML] > 0 {
if kindMap[statementKindPartitionedDML] > 0 {
return "", errors.New("cannot specify DML and partitioned DML in the same migration file")
}
case !hasDDL && hasDML && !hasPartitionedDML:
return statementKindDML, nil
}

if kindMap[statementKindPartitionedDML] > 0 {
case !hasDDL && !hasDML && hasPartitionedDML:
return statementKindPartitionedDML, nil
default:
return "", errors.New("DDL, DML (INSERT), and partitioned DML (UPDATE or DELETE) must not be combined in the same migration file")
}

return statementKindDDL, nil
}

func isDML(statement string) bool {
Expand Down

0 comments on commit 4d503d5

Please sign in to comment.