package sqlparse import ( "bufio" "fmt" "io" "strings" ) type SqlParser interface { Parse(reader io.Reader) (Migration, error) } type Migration struct { UpgradeScript MigrationScript DowngradeScript MigrationScript Dependencies []string } type MigrationScript struct { Contents string Options ExecutionOptions } type ExecutionOptions struct { UseTransaction bool } const ( unknownSection = iota upgradeScriptSection downgradeScriptSection ) const ( upgradeScriptDeliminator = "-- migrate:up" downgradeScriptDeliminator = "-- migrate:down" dependsDirective = "-- depends:" transactionOptionName = "transaction" ) type sqlParser struct{} // NewSqlParser creates a new SqlParser which can // parse out a migration script. func NewSqlParser() SqlParser { return &sqlParser{} } // Parse parses out the migration's upgrade and downgrade scripts, // any custom execution options with those scripts, and migration dependencies. // It returns the upgrade script, downgrade script, dependencies, and any error if one occurred. func (sp *sqlParser) Parse(reader io.Reader) (Migration, error) { migration := Migration{Dependencies: []string{}} scanner := bufio.NewScanner(reader) currentSection := unknownSection for scanner.Scan() { line := strings.TrimSpace(scanner.Text()) loweredLine := strings.ToLower(line) if strings.HasPrefix(loweredLine, upgradeScriptDeliminator) { currentSection = upgradeScriptSection migration.UpgradeScript.Options = parseExecutionOptions(line) } else if strings.HasPrefix(loweredLine, downgradeScriptDeliminator) { currentSection = downgradeScriptSection migration.DowngradeScript.Options = parseExecutionOptions(line) } else if currentSection == upgradeScriptSection { migration.UpgradeScript.Contents += line + "\n" } else if currentSection == downgradeScriptSection { migration.DowngradeScript.Contents += line + "\n" } if strings.HasPrefix(loweredLine, dependsDirective) { migration.Dependencies = append(migration.Dependencies, parseDependencies(line)...) } } if err := scanner.Err(); err != nil { return Migration{}, fmt.Errorf( "parsing sql file encountered an error: %w", err, ) } return migration, nil } // parseExecutionOptions extracts the execution options for a // upgrade or downgrade migration script. func parseExecutionOptions(line string) ExecutionOptions { options := ExecutionOptions{UseTransaction: true} parts := strings.SplitSeq(line, " ") for part := range parts { part = strings.ToLower(part) if strings.Contains(part, fmt.Sprintf("%s:", transactionOptionName)) { option := strings.Split(part, ":")[1] options.UseTransaction = option != "false" } } return options } // parseDependencies extracts migration dependencies from a depends directive. // Expected format: -- depends: 001, 002, 003 func parseDependencies(line string) []string { dependencies := []string{} loweredLine := strings.ToLower(line) prefixIdx := strings.Index(loweredLine, dependsDirective) if prefixIdx == -1 { return dependencies } depsStr := line[prefixIdx+len(dependsDirective):] depsStr = strings.TrimSpace(depsStr) if depsStr == "" { return dependencies } deps := strings.SplitSeq(depsStr, ",") for dep := range deps { dep = strings.TrimSpace(dep) if dep != "" { dependencies = append(dependencies, dep) } } return dependencies }