From 4d503d51abd8abe8c37da44397b6cb3323b04937 Mon Sep 17 00:00:00 2001 From: KentaTakahashi Date: Mon, 8 Jul 2024 17:50:33 +0900 Subject: [PATCH] make the logic of inspectStatementsKind simpler --- pkg/spanner/migration.go | 41 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/pkg/spanner/migration.go b/pkg/spanner/migration.go index 997439b..2cfc3dc 100644 --- a/pkg/spanner/migration.go +++ b/pkg/spanner/migration.go @@ -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 {