diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-16 15:06:30 -0400 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-16 15:06:30 -0400 |
| commit | 65ded7c0133ae011a30546fb4fd1e00fc3179874 (patch) | |
| tree | 305c9655c581ba5c959d56cf801a96bf80683dd7 /internal | |
| parent | Update README (diff) | |
| download | cli-65ded7c0133ae011a30546fb4fd1e00fc3179874.zip | |
Update migration file format
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/bolttest/mock_migration_fs_repo.go | 17 | ||||
| -rw-r--r-- | internal/configloader/configloader.go | 2 | ||||
| -rw-r--r-- | internal/configloader/configloader_test.go | 2 | ||||
| -rw-r--r-- | internal/models/migration.go | 13 | ||||
| -rw-r--r-- | internal/models/migration_test.go | 32 | ||||
| -rw-r--r-- | internal/repositories/migration_db_repo.go | 2 | ||||
| -rw-r--r-- | internal/repositories/migration_fs_repo.go | 73 | ||||
| -rw-r--r-- | internal/repositories/migration_fs_repo_test.go | 41 | ||||
| -rw-r--r-- | internal/services/migration.go | 34 | ||||
| -rw-r--r-- | internal/services/migration_test.go | 37 | ||||
| -rw-r--r-- | internal/sqlparse/sqlparse.go | 78 | ||||
| -rw-r--r-- | internal/sqlparse/sqlparse_test.go | 175 |
12 files changed, 354 insertions, 152 deletions
diff --git a/internal/bolttest/mock_migration_fs_repo.go b/internal/bolttest/mock_migration_fs_repo.go index d392732..b2507a0 100644 --- a/internal/bolttest/mock_migration_fs_repo.go +++ b/internal/bolttest/mock_migration_fs_repo.go @@ -1,6 +1,9 @@ package bolttest -import "github.com/eugenetriguba/bolt/internal/models" +import ( + "github.com/eugenetriguba/bolt/internal/models" + "github.com/eugenetriguba/bolt/internal/sqlparse" +) type MockMigrationFsRepo struct { CreateReturnValue CreateReturnValue @@ -18,8 +21,8 @@ type CreateReturnValue struct { } type ReadUpgradeScriptReturnValue struct { - ScriptContents string - Err error + Script sqlparse.MigrationScript + Err error } type ReadDowngradeScriptReturnValue = ReadUpgradeScriptReturnValue @@ -36,14 +39,14 @@ func (repo *MockMigrationFsRepo) List() (map[string]*models.Migration, error) { func (repo *MockMigrationFsRepo) ReadUpgradeScript( migration *models.Migration, -) (string, error) { +) (sqlparse.MigrationScript, error) { repo.ReadUpgradeScriptCallCount += 1 - return repo.ReadUpgradeScriptReturnValue.ScriptContents, repo.ReadUpgradeScriptReturnValue.Err + return repo.ReadUpgradeScriptReturnValue.Script, repo.ReadUpgradeScriptReturnValue.Err } func (repo *MockMigrationFsRepo) ReadDowngradeScript( migration *models.Migration, -) (string, error) { +) (sqlparse.MigrationScript, error) { repo.ReadDowngradeScriptCallCount += 1 - return repo.ReadDowngradeScriptReturnValue.ScriptContents, repo.ReadDowngradeScriptReturnValue.Err + return repo.ReadDowngradeScriptReturnValue.Script, repo.ReadDowngradeScriptReturnValue.Err } diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go index eed150f..c13977a 100644 --- a/internal/configloader/configloader.go +++ b/internal/configloader/configloader.go @@ -71,7 +71,7 @@ func NewConfig() (*Config, error) { cfg := Config{ Migrations: MigrationsConfig{ DirectoryPath: "migrations", - VersionStyle: VersionStyleSequential, + VersionStyle: VersionStyleTimestamp, }, } if !errors.Is(err, ErrConfigFileNotFound) { diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go index 7684436..89c4368 100644 --- a/internal/configloader/configloader_test.go +++ b/internal/configloader/configloader_test.go @@ -18,7 +18,7 @@ func TestNewConfigDefaults(t *testing.T) { assert.Nil(t, err) check.Equal(t, cfg.Migrations.DirectoryPath, "migrations") - check.Equal(t, cfg.Migrations.VersionStyle, configloader.VersionStyleSequential) + check.Equal(t, cfg.Migrations.VersionStyle, configloader.VersionStyleTimestamp) } func TestNewConfigWithInvalidVersionStyle(t *testing.T) { diff --git a/internal/models/migration.go b/internal/models/migration.go index b63fec9..ed48073 100644 --- a/internal/models/migration.go +++ b/internal/models/migration.go @@ -2,6 +2,7 @@ package models import ( "fmt" + "strings" "time" ) @@ -31,6 +32,16 @@ func NewSequentialMigration(version uint64, message string) *Migration { } } +// NormalizedMessage gives back the Message in lowercase, +// leading and trailing whitespace removed, and spaces +// replaced with underscores. +func (m Migration) NormalizedMessage() string { + message := strings.ToLower(m.Message) + message = strings.TrimSpace(message) + message = strings.ReplaceAll(message, " ", "_") + return message +} + func (m *Migration) Name() string { - return fmt.Sprintf("%s_%s", m.Version, m.Message) + return fmt.Sprintf("%s_%s", m.Version, m.NormalizedMessage()) } diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go index b6d3205..809887f 100644 --- a/internal/models/migration_test.go +++ b/internal/models/migration_test.go @@ -1,7 +1,6 @@ package models_test import ( - "fmt" "testing" "time" @@ -34,6 +33,33 @@ func TestNewSequentialMigration(t *testing.T) { } func TestName(t *testing.T) { - m := models.NewSequentialMigration(1, "test message") - check.Equal(t, m.Name(), fmt.Sprintf("%s_%s", m.Version, m.Message)) + type test struct { + migration *models.Migration + expectedName string + } + + testCases := []test{ + // Underscores are used to separate version and message. + { + migration: models.NewSequentialMigration(1, "test_message"), + expectedName: "001_test_message", + }, + { + migration: models.NewTimestampMigration(time.Date(1234, 1, 2, 3, 4, 5, 6, time.UTC), "test_message"), + expectedName: "12340102030405_test_message", + }, + // Leading/Trailing spaces trimmed, lowercased, and spaces converted to underscores. + { + migration: models.NewSequentialMigration(1, " teST mesSAGE "), + expectedName: "001_test_message", + }, + { + migration: models.NewTimestampMigration(time.Date(1234, 1, 2, 3, 4, 5, 6, time.UTC), " teST mesSAGE "), + expectedName: "12340102030405_test_message", + }, + } + + for _, tc := range testCases { + check.Equal(t, tc.migration.Name(), tc.expectedName) + } } diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go index e97349a..26cc2b3 100644 --- a/internal/repositories/migration_db_repo.go +++ b/internal/repositories/migration_db_repo.go @@ -39,7 +39,7 @@ func NewMigrationDBRepo(db storage.DB) (MigrationDBRepo, error) { if !migrationTableExists { _, err := db.Exec(` CREATE TABLE bolt_migrations( - version CHARACTER(14) PRIMARY KEY NOT NULL + version VARCHAR(255) PRIMARY KEY NOT NULL ); `) if err != nil { diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go index e535013..0ded9d0 100644 --- a/internal/repositories/migration_fs_repo.go +++ b/internal/repositories/migration_fs_repo.go @@ -10,6 +10,7 @@ import ( "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/models" + "github.com/eugenetriguba/bolt/internal/sqlparse" ) type ErrIsNotDir struct { @@ -26,8 +27,8 @@ func (e *ErrIsNotDir) Error() string { type MigrationFsRepo interface { Create(migration *models.Migration) error List() (map[string]*models.Migration, error) - ReadUpgradeScript(migration *models.Migration) (string, error) - ReadDowngradeScript(migration *models.Migration) (string, error) + ReadUpgradeScript(migration *models.Migration) (sqlparse.MigrationScript, error) + ReadDowngradeScript(migration *models.Migration) (sqlparse.MigrationScript, error) } type migrationFsRepo struct { @@ -61,23 +62,15 @@ func NewMigrationFsRepo( } func (mr migrationFsRepo) Create(migration *models.Migration) error { - newMigrationDir := filepath.Join(mr.migrationsDirPath, mr.migrationDirname(migration)) - - err := os.Mkdir(newMigrationDir, 0755) + newMigrationPath := filepath.Join(mr.migrationsDirPath, migration.Name()+".sql") + file, err := os.Create(newMigrationPath) if err != nil { - return err + return fmt.Errorf("unable to create file at %s: %w", newMigrationPath, err) } - - _, err = os.Create(filepath.Join(newMigrationDir, "upgrade.sql")) + _, err = file.WriteString("-- migrate:up\n\n-- migrate:down\n") if err != nil { - return err + return fmt.Errorf("unable to write template to new migration script: %w", err) } - - _, err = os.Create(filepath.Join(newMigrationDir, "downgrade.sql")) - if err != nil { - return err - } - return nil } @@ -108,31 +101,42 @@ func dirEntryToMigration(entry fs.DirEntry) (*models.Migration, error) { entry.Name(), ) } + version := parts[0] + message := strings.TrimSuffix(parts[1], filepath.Ext(parts[1])) // Remove .sql return &models.Migration{ - Version: parts[0], - Message: parts[1], + Version: version, + Message: message, Applied: false, }, nil } -func (mr migrationFsRepo) ReadUpgradeScript(migration *models.Migration) (string, error) { - upgradeScriptPath := filepath.Join( +func (mr migrationFsRepo) ReadUpgradeScript(migration *models.Migration) (sqlparse.MigrationScript, error) { + scriptPath := filepath.Join( mr.migrationsDirPath, - mr.migrationDirname(migration), - "upgrade.sql", + migration.Name()+".sql", ) - return mr.readScriptContents(upgradeScriptPath) + upgradeScript, _, err := mr.getMigrationScripts(scriptPath) + return upgradeScript, err } func (mr migrationFsRepo) ReadDowngradeScript( migration *models.Migration, -) (string, error) { - downgradeScriptPath := filepath.Join( +) (sqlparse.MigrationScript, error) { + scriptPath := filepath.Join( mr.migrationsDirPath, - mr.migrationDirname(migration), - "downgrade.sql", + migration.Name()+".sql", ) - return mr.readScriptContents(downgradeScriptPath) + _, downgradeScript, err := mr.getMigrationScripts(scriptPath) + return downgradeScript, err +} + +func (mr migrationFsRepo) getMigrationScripts(scriptPath string) (sqlparse.MigrationScript, sqlparse.MigrationScript, error) { + scriptContents, err := mr.readScriptContents(scriptPath) + if err != nil { + return sqlparse.MigrationScript{}, sqlparse.MigrationScript{}, fmt.Errorf("unable to read %s script: %w", scriptPath, err) + } + sqlParser := sqlparse.NewSqlParser() + return sqlParser.Parse(strings.NewReader(scriptContents)) } func (mr migrationFsRepo) readScriptContents(scriptPath string) (string, error) { @@ -143,18 +147,3 @@ func (mr migrationFsRepo) readScriptContents(scriptPath string) (string, error) return string(contents), nil } - -// migrationDirname creates the directory name that -// should be used for this migration. -func (mr migrationFsRepo) migrationDirname(migration *models.Migration) string { - return fmt.Sprintf("%s_%s", migration.Version, mr.normalizeMessage(migration)) -} - -// normalizeMessage normalizes the Message of a migration -// to be filesystem friendly. -func (mr migrationFsRepo) normalizeMessage(migration *models.Migration) string { - message := strings.ToLower(migration.Message) - message = strings.TrimSpace(message) - message = strings.ReplaceAll(message, " ", "_") - return message -} diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go index 32de193..96d48a0 100644 --- a/internal/repositories/migration_fs_repo_test.go +++ b/internal/repositories/migration_fs_repo_test.go @@ -59,26 +59,23 @@ func TestCreate_SuccessfullyCreated(t *testing.T) { err = repo.Create(migration) assert.Nil(t, err) - migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version) - assertFileExists(t, filepath.Join(tempDir, migrationDirName)) - assertFileExists(t, filepath.Join(tempDir, migrationDirName, "upgrade.sql")) - assertFileExists(t, filepath.Join(tempDir, migrationDirName, "downgrade.sql")) + assertFileExists(t, filepath.Join(tempDir, fmt.Sprintf("%s.sql", migration.Name()))) } -func TestCreate_FailsToCreateDir(t *testing.T) { +func TestCreate_FailsToCreateMigration(t *testing.T) { tempDir := t.TempDir() - migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir} - repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) + repo, err := repositories.NewMigrationFsRepo( + &configloader.MigrationsConfig{DirectoryPath: tempDir}, + ) assert.Nil(t, err) migration := models.NewTimestampMigration(time.Now(), "add users table") - migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version) - _, err = os.Create(filepath.Join(tempDir, migrationDirName)) + _, err = os.OpenFile(filepath.Join(tempDir, fmt.Sprintf("%s.sql", migration.Name())), os.O_CREATE, 0000) assert.Nil(t, err) err = repo.Create(migration) assert.NotNil(t, err) - assert.ErrorContains(t, err, "file exists") + assert.ErrorContains(t, err, "unable to create file") } func TestReadUpgradeScript_SuccessfullyRead(t *testing.T) { @@ -91,17 +88,17 @@ func TestReadUpgradeScript_SuccessfullyRead(t *testing.T) { err = repo.Create(migration) assert.Nil(t, err) - migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version) - expectedUpgradeScriptContents := []byte("CREATE TABLE users(id int PRIMARY KEY);") + migrationName := fmt.Sprintf("%s.sql", migration.Name()) + expectedUpgradeScriptContents := "CREATE TABLE users(id int PRIMARY KEY);\n" os.WriteFile( - filepath.Join(tempDir, migrationDirName, "upgrade.sql"), - expectedUpgradeScriptContents, + filepath.Join(tempDir, migrationName), + []byte("-- migrate:up\n"+expectedUpgradeScriptContents), 0755, ) - upgradeScriptContents, err := repo.ReadUpgradeScript(migration) + upgradeScript, err := repo.ReadUpgradeScript(migration) assert.Nil(t, err) - assert.Equal(t, upgradeScriptContents, string(expectedUpgradeScriptContents)) + assert.Equal(t, upgradeScript.Contents, string(expectedUpgradeScriptContents)) } func TestReadUpgradeScript_FileDoesNotExist(t *testing.T) { @@ -126,17 +123,17 @@ func TestReadDowngradeScript_SuccessfullyRead(t *testing.T) { err = repo.Create(migration) assert.Nil(t, err) - migrationDirName := fmt.Sprintf("%s_add_users_table", migration.Version) - expectedDowngradeScriptContents := []byte("DROP TABLE users;") + migrationName := fmt.Sprintf("%s.sql", migration.Name()) + expectedDowngradeScriptContents := "DROP TABLE users;\n" os.WriteFile( - filepath.Join(tempDir, migrationDirName, "downgrade.sql"), - expectedDowngradeScriptContents, + filepath.Join(tempDir, migrationName), + []byte("-- migrate:down\n"+expectedDowngradeScriptContents), 0755, ) - downgradeScriptContents, err := repo.ReadDowngradeScript(migration) + downgradeScript, err := repo.ReadDowngradeScript(migration) assert.Nil(t, err) - assert.Equal(t, downgradeScriptContents, string(expectedDowngradeScriptContents)) + assert.Equal(t, downgradeScript.Contents, string(expectedDowngradeScriptContents)) } func TestReadDowngradeScript_FileDoesNotExist(t *testing.T) { diff --git a/internal/services/migration.go b/internal/services/migration.go index 10cc1b2..3b0f297 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -4,14 +4,12 @@ import ( "fmt" "sort" "strconv" - "strings" "time" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/models" "github.com/eugenetriguba/bolt/internal/output" "github.com/eugenetriguba/bolt/internal/repositories" - "github.com/eugenetriguba/bolt/internal/sqlparse" ) type MigrationService struct { @@ -118,21 +116,15 @@ func (ms MigrationService) ApplyMigration(migration *models.Migration) error { ), ) - scriptContents, err := ms.fsRepo.ReadUpgradeScript(migration) + upgradeScript, err := ms.fsRepo.ReadUpgradeScript(migration) if err != nil { - return err - } - - sqlParser := sqlparse.NewSqlParser() - execOptions, err := sqlParser.Parse(strings.NewReader(scriptContents)) - if err != nil { - return fmt.Errorf("unable to parse sql file for upgrade script: %w", err) + return fmt.Errorf("unable to read upgrade script: %w", err) } - if execOptions.UseTransaction { - err = ms.dbRepo.ApplyWithTx(scriptContents, migration) + if upgradeScript.Options.UseTransaction { + err = ms.dbRepo.ApplyWithTx(upgradeScript.Contents, migration) } else { - err = ms.dbRepo.Apply(scriptContents, migration) + err = ms.dbRepo.Apply(upgradeScript.Contents, migration) } if err != nil { @@ -225,21 +217,15 @@ func (ms MigrationService) RevertMigration(migration *models.Migration) error { migration.Message, ), ) - scriptContents, err := ms.fsRepo.ReadDowngradeScript(migration) - if err != nil { - return err - } - - sqlParser := sqlparse.NewSqlParser() - execOptions, err := sqlParser.Parse(strings.NewReader(scriptContents)) + downgradeScript, err := ms.fsRepo.ReadDowngradeScript(migration) if err != nil { - return fmt.Errorf("unable to parse sql file for downgrade script: %w", err) + return fmt.Errorf("unable to read downgrade script: %w", err) } - if execOptions.UseTransaction { - err = ms.dbRepo.RevertWithTx(scriptContents, migration) + if downgradeScript.Options.UseTransaction { + err = ms.dbRepo.RevertWithTx(downgradeScript.Contents, migration) } else { - err = ms.dbRepo.Revert(scriptContents, migration) + err = ms.dbRepo.Revert(downgradeScript.Contents, migration) } if err != nil { diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go index db9448f..6828d28 100644 --- a/internal/services/migration_test.go +++ b/internal/services/migration_test.go @@ -8,6 +8,7 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/models" + "github.com/eugenetriguba/bolt/internal/sqlparse" "github.com/eugenetriguba/checkmate/assert" "github.com/eugenetriguba/checkmate/check" ) @@ -582,8 +583,11 @@ func TestCreateMigration_SequentialVersionIncrementsParsingErr(t *testing.T) { func TestApplyMigration_WithTransaction(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ - ScriptContents: "CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);", - Err: nil, + Script: sqlparse.MigrationScript{ + Contents: "CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);", + Options: sqlparse.ExecutionOptions{UseTransaction: true}, + }, + Err: nil, }, } @@ -609,8 +613,11 @@ func TestApplyMigration_WithTransaction(t *testing.T) { func TestApplyMigration_WithoutTransaction(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ - ScriptContents: "-- bolt: no-transaction\nCREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);", - Err: nil, + Script: sqlparse.MigrationScript{ + Contents: "CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);", + Options: sqlparse.ExecutionOptions{UseTransaction: false}, + }, + Err: nil, }, } @@ -637,8 +644,8 @@ func TestApplyMigration_ReadUpgradeScriptErr(t *testing.T) { expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ - ScriptContents: "", - Err: expectedErr, + Script: sqlparse.MigrationScript{}, + Err: expectedErr, }, } @@ -860,8 +867,11 @@ func TestApplyUpToVersion_AppliesMigrations(t *testing.T) { func TestRevertMigration_WithTransaction(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ - ScriptContents: "DROP TABLE tmp;", - Err: nil, + Script: sqlparse.MigrationScript{ + Contents: "DROP TABLE tmp;", + Options: sqlparse.ExecutionOptions{UseTransaction: true}, + }, + Err: nil, }, } @@ -887,8 +897,11 @@ func TestRevertMigration_WithTransaction(t *testing.T) { func TestRevertMigration_WithoutTransaction(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ - ScriptContents: "-- bolt: no-transaction\nDROP TABLE tmp;", - Err: nil, + Script: sqlparse.MigrationScript{ + Contents: "DROP TABLE tmp;", + Options: sqlparse.ExecutionOptions{UseTransaction: false}, + }, + Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ @@ -914,8 +927,8 @@ func TestRevertMigration_ReadUpgradeScriptErr(t *testing.T) { expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ - ScriptContents: "", - Err: expectedErr, + Script: sqlparse.MigrationScript{}, + Err: expectedErr, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{} diff --git a/internal/sqlparse/sqlparse.go b/internal/sqlparse/sqlparse.go index e986068..fa9fa96 100644 --- a/internal/sqlparse/sqlparse.go +++ b/internal/sqlparse/sqlparse.go @@ -8,40 +8,84 @@ import ( ) type SqlParser interface { - Parse(reader io.Reader) (ExecutionOptions, error) + Parse(reader io.Reader) (MigrationScript, MigrationScript, error) +} + +type MigrationScript struct { + Contents string + Options ExecutionOptions } type ExecutionOptions struct { UseTransaction bool } +const ( + unknownSection = iota + upgradeScriptSection + downgradeScriptSection +) + +const ( + upgradeScriptDeliminator = "-- migrate:up" + downgradeScriptDeliminator = "-- migrate:down" + transactionOptionName = "transaction" +) + type sqlParser struct{} +// NewSqlParser creates a new SqlParser which can +// parse out a migration script. func NewSqlParser() SqlParser { - return sqlParser{} + return &sqlParser{} } -func (sp sqlParser) Parse(reader io.Reader) (ExecutionOptions, error) { - options := ExecutionOptions{UseTransaction: true} +// Parse parses out the migration's upgrade and downgrade scripts +// and any custom execution options with those scripts. It returns +// the upgrade script, downgrade script, and any error if one occurred. +func (sp *sqlParser) Parse(reader io.Reader) (MigrationScript, MigrationScript, error) { + upgradeScript := MigrationScript{} + downgradeScript := MigrationScript{} scanner := bufio.NewScanner(reader) - if scanner.Scan() { - firstLine := strings.ToLower(strings.TrimSpace(scanner.Text())) - if strings.HasPrefix(firstLine, "-- bolt:") { - parts := strings.Split(firstLine, " ") - // Skip the first two parts ("--" and "bolt:") - for _, part := range parts[2:] { - if part == "no-transaction" { - options.UseTransaction = false - break - } - } + currentSection := unknownSection + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + loweredLine := strings.ToLower(line) + + if strings.HasPrefix(loweredLine, upgradeScriptDeliminator) { + currentSection = upgradeScriptSection + upgradeScript.Options = parseExecutionOptions(line) + } else if strings.HasPrefix(loweredLine, downgradeScriptDeliminator) { + currentSection = downgradeScriptSection + downgradeScript.Options = parseExecutionOptions(line) + } else if currentSection == upgradeScriptSection { + upgradeScript.Contents += line + "\n" + } else if currentSection == downgradeScriptSection { + downgradeScript.Contents += line + "\n" } } if err := scanner.Err(); err != nil { - return options, fmt.Errorf("parsing sql file encountered an error: %w", err) + return MigrationScript{}, MigrationScript{}, fmt.Errorf("parsing sql file encountered an error: %w", err) + } + + return upgradeScript, downgradeScript, nil +} + +// parseExecutionOptions extracts the execution options for a +// upgrade or downgrade migration script. +func parseExecutionOptions(line string) ExecutionOptions { + options := ExecutionOptions{UseTransaction: true} + + parts := strings.Split(line, " ") + for _, part := range parts { + part = strings.ToLower(part) + if strings.Contains(part, "transaction:") { + option := strings.Split(part, ":")[1] + options.UseTransaction = option != "false" + } } - return options, nil + return options } diff --git a/internal/sqlparse/sqlparse_test.go b/internal/sqlparse/sqlparse_test.go index 12a435d..b5d2e93 100644 --- a/internal/sqlparse/sqlparse_test.go +++ b/internal/sqlparse/sqlparse_test.go @@ -9,50 +9,183 @@ import ( "github.com/eugenetriguba/bolt/internal/sqlparse" "github.com/eugenetriguba/checkmate/assert" + "github.com/eugenetriguba/checkmate/check" ) func TestSqlParser_Parse(t *testing.T) { testCases := []struct { - buffer string - expectedExecutionOptions sqlparse.ExecutionOptions + migration string + expectedUpgradeScript sqlparse.MigrationScript + expectedDowngradeScript sqlparse.MigrationScript }{ { - buffer: "-- bolt: no-transaction", - expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: false}, + migration: "", + expectedUpgradeScript: sqlparse.MigrationScript{ + Contents: "", + Options: sqlparse.ExecutionOptions{}, + }, + expectedDowngradeScript: sqlparse.MigrationScript{ + Contents: "", + Options: sqlparse.ExecutionOptions{}, + }, }, { - buffer: "-- BOLT: no-transaction", - expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: false}, + migration: ` + -- migrate:up + CREATE TABLE users(id int PRIMARY KEY);`, + expectedUpgradeScript: sqlparse.MigrationScript{ + Contents: "CREATE TABLE users(id int PRIMARY KEY);\n", + Options: sqlparse.ExecutionOptions{UseTransaction: true}, + }, + expectedDowngradeScript: sqlparse.MigrationScript{ + Contents: "", + Options: sqlparse.ExecutionOptions{}, + }, }, { - buffer: "\n-- bolt: no-transaction", - expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true}, + migration: ` + -- migrate:up transaction:false + CREATE TABLE users(id int PRIMARY KEY);`, + expectedUpgradeScript: sqlparse.MigrationScript{ + Contents: "CREATE TABLE users(id int PRIMARY KEY);\n", + Options: sqlparse.ExecutionOptions{UseTransaction: false}, + }, + expectedDowngradeScript: sqlparse.MigrationScript{ + Contents: "", + Options: sqlparse.ExecutionOptions{}, + }, }, { - buffer: "", - expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true}, + migration: ` + -- migrate:up transaction:true + CREATE TABLE users(id int PRIMARY KEY);`, + expectedUpgradeScript: sqlparse.MigrationScript{ + Contents: "CREATE TABLE users(id int PRIMARY KEY);\n", + Options: sqlparse.ExecutionOptions{UseTransaction: true}, + }, + expectedDowngradeScript: sqlparse.MigrationScript{ + Contents: "", + Options: sqlparse.ExecutionOptions{}, + }, }, { - buffer: "--bolt: no-transaction", - expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true}, + migration: ` + -- transaction:true migrate:up + CREATE TABLE users(id int PRIMARY KEY);`, + expectedUpgradeScript: sqlparse.MigrationScript{ + Contents: "", + Options: sqlparse.ExecutionOptions{}, + }, + expectedDowngradeScript: sqlparse.MigrationScript{ + Contents: "", + Options: sqlparse.ExecutionOptions{}, + }, }, { - buffer: "-- bolt : no-transaction", - expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: true}, + migration: ` + -- migrate:down + DROP TABLE users;`, + expectedUpgradeScript: sqlparse.MigrationScript{ + Contents: "", + Options: sqlparse.ExecutionOptions{}, + }, + expectedDowngradeScript: sqlparse.MigrationScript{ + Contents: "DROP TABLE users;\n", + Options: sqlparse.ExecutionOptions{UseTransaction: true}, + }, }, { - buffer: "-- bolt: no-transaction no-transaction", - expectedExecutionOptions: sqlparse.ExecutionOptions{UseTransaction: false}, + migration: ` + -- migrate:down transaction:false + DROP TABLE users;`, + expectedUpgradeScript: sqlparse.MigrationScript{ + Contents: "", + Options: sqlparse.ExecutionOptions{}, + }, + expectedDowngradeScript: sqlparse.MigrationScript{ + Contents: "DROP TABLE users;\n", + Options: sqlparse.ExecutionOptions{UseTransaction: false}, + }, + }, + { + migration: ` + -- migrate:down transaction:true + DROP TABLE users;`, + expectedUpgradeScript: sqlparse.MigrationScript{ + Contents: "", + Options: sqlparse.ExecutionOptions{}, + }, + expectedDowngradeScript: sqlparse.MigrationScript{ + Contents: "DROP TABLE users;\n", + Options: sqlparse.ExecutionOptions{UseTransaction: true}, + }, + }, + { + migration: ` + -- transaction:true migrate:down + DROP TABLE users;`, + expectedUpgradeScript: sqlparse.MigrationScript{ + Contents: "", + Options: sqlparse.ExecutionOptions{}, + }, + expectedDowngradeScript: sqlparse.MigrationScript{ + Contents: "", + Options: sqlparse.ExecutionOptions{}, + }, + }, + { + migration: ` + -- migrate:up + CREATE TABLE users(id int PRIMARY KEY); + -- migrate:down + DROP TABLE users;`, + expectedUpgradeScript: sqlparse.MigrationScript{ + Contents: "CREATE TABLE users(id int PRIMARY KEY);\n", + Options: sqlparse.ExecutionOptions{UseTransaction: true}, + }, + expectedDowngradeScript: sqlparse.MigrationScript{ + Contents: "DROP TABLE users;\n", + Options: sqlparse.ExecutionOptions{UseTransaction: true}, + }, + }, + { + migration: ` + -- migrate:up transaction:false + CREATE TABLE users(id int PRIMARY KEY); + -- migrate:down transaction:false + DROP TABLE users;`, + expectedUpgradeScript: sqlparse.MigrationScript{ + Contents: "CREATE TABLE users(id int PRIMARY KEY);\n", + Options: sqlparse.ExecutionOptions{UseTransaction: false}, + }, + expectedDowngradeScript: sqlparse.MigrationScript{ + Contents: "DROP TABLE users;\n", + Options: sqlparse.ExecutionOptions{UseTransaction: false}, + }, + }, + { + migration: ` + -- MIGRATE:UP TRANSACTION:FALSE + CREATE TABLE users(id int PRIMARY KEY); + -- MIGRATE:DOWN TRANSACTION:FALSE + DROP TABLE users;`, + expectedUpgradeScript: sqlparse.MigrationScript{ + Contents: "CREATE TABLE users(id int PRIMARY KEY);\n", + Options: sqlparse.ExecutionOptions{UseTransaction: false}, + }, + expectedDowngradeScript: sqlparse.MigrationScript{ + Contents: "DROP TABLE users;\n", + Options: sqlparse.ExecutionOptions{UseTransaction: false}, + }, }, } for _, tc := range testCases { sqlParser := sqlparse.NewSqlParser() - - execOptions, err := sqlParser.Parse(strings.NewReader(tc.buffer)) + upgradeScript, downgradeScript, err := sqlParser.Parse(strings.NewReader(tc.migration)) assert.Nil(t, err) - - assert.DeepEqual(t, execOptions, tc.expectedExecutionOptions) + check.DeepEqual(t, upgradeScript, tc.expectedUpgradeScript) + check.DeepEqual(t, downgradeScript, tc.expectedDowngradeScript) } } @@ -80,7 +213,7 @@ func TestSqlParserParseError(t *testing.T) { ErrCond: " ", } sqlParser := sqlparse.NewSqlParser() - _, err := sqlParser.Parse(reader) + _, _, err := sqlParser.Parse(reader) assert.NotNil(t, err) assert.ErrorContains(t, err, "unwanted input encountered") } |