diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-14 21:14:10 -0400 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-14 21:14:10 -0400 |
| commit | de8d64d88dbad28cbd92c39f69d6adb5826a79d3 (patch) | |
| tree | 9fc5324a51a4a41b6120a95e7e445c3abcbf6d0e | |
| parent | Fix db test (diff) | |
| download | cli-de8d64d88dbad28cbd92c39f69d6adb5826a79d3.zip | |
Fix bug with bolt new
| -rw-r--r-- | CHANGELOG.md | 4 | ||||
| -rw-r--r-- | internal/services/migration.go | 17 | ||||
| -rw-r--r-- | internal/services/migration_test.go | 25 |
3 files changed, 44 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 6720741..bd84066 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Renamed the `[connection]` section of `bolt.toml` to `[database]`. +### Fixed + +- Panic that occurs when `bolt new` is run without a remote database connection setup. + ## [0.6.0] - 2024-03-11 ### Added diff --git a/internal/services/migration.go b/internal/services/migration.go index 4be10cc..10cc1b2 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -283,7 +283,12 @@ func (ms MigrationService) CreateMigration(message string) (*models.Migration, e func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) { var currentVersion uint64 = 0 - migrations, err := ms.ListMigrations(SortOrderDesc) + localMigrations, err := ms.fsRepo.List() + if err != nil { + return 0, fmt.Errorf("unable to list out local filesystem migrations: %w", err) + } + + migrations, err := ms.combineMigrations(localMigrations, map[string]*models.Migration{}, SortOrderDesc) if err != nil { return 0, err } @@ -319,6 +324,14 @@ func (ms MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, ) } + return ms.combineMigrations(localMigrations, appliedMigrations, order) +} + +func (ms MigrationService) combineMigrations( + localMigrations map[string]*models.Migration, + appliedMigrations map[string]*models.Migration, + order sortOrder, +) ([]*models.Migration, error) { migrations := make([]*models.Migration, 0) for _, localMigration := range localMigrations { _, ok := appliedMigrations[localMigration.Version] @@ -328,7 +341,7 @@ func (ms MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, migrations = append(migrations, localMigration) } - err = ms.sortMigrations(migrations, order) + err := ms.sortMigrations(migrations, order) if err != nil { return nil, err } diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go index b2b480b..db9448f 100644 --- a/internal/services/migration_test.go +++ b/internal/services/migration_test.go @@ -412,6 +412,31 @@ func TestCreateMigration_VersionStyleSequential(t *testing.T) { check.Equal(t, migration.Applied, false) } +func TestCreateMigration_NilDbRepo(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + CreateReturnValue: bolttest.CreateReturnValue{ + Err: nil, + }, + } + svc := NewMigrationService( + nil, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + migration, err := svc.CreateMigration("new migration") + + assert.Nil(t, err) + check.Equal(t, migration.Version, "001") + check.Equal(t, migration.Message, "new migration") + check.Equal(t, migration.Applied, false) +} + func TestCreateMigration_CreateErr(t *testing.T) { expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ |