fix(be): boltdb migration

This commit is contained in:
Denis Gukov 2022-01-28 15:45:31 +05:00
parent 84471a4539
commit 727b6f7326
2 changed files with 31 additions and 4 deletions

View File

@ -28,9 +28,6 @@ func TestMigration_2_8_28_Apply(t *testing.T) {
err = r.Put([]byte("0000000001"),
[]byte("{\"id\":\"1\",\"project_id\":\"1\",\"git_url\": \"git@github.com/test/test#main\"}"))
if err != nil {
return err
}
return err
})
@ -62,3 +59,27 @@ func TestMigration_2_8_28_Apply(t *testing.T) {
t.Fatal("invalid branch")
}
}
func TestMigration_2_8_28_Apply2(t *testing.T) {
store := CreateTestStore()
err := store.db.Update(func(tx *bbolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("project"))
if err != nil {
return err
}
err = b.Put([]byte("0000000001"), []byte("{}"))
return err
})
if err != nil {
t.Fatal(err)
}
err = migrations.Migration_2_8_28{DB: store.db}.Apply()
if err != nil {
t.Fatal(err)
}
}

View File

@ -14,6 +14,9 @@ func (d Migration_2_8_28) getProjectRepositories(projectID string) (map[string]m
repos := make(map[string]map[string]interface{})
err := d.DB.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte("project__repository_" + projectID))
if b == nil {
return nil
}
return b.ForEach(func(id, body []byte) error {
r := make(map[string]interface{})
repos[string(id)] = r
@ -25,7 +28,10 @@ func (d Migration_2_8_28) getProjectRepositories(projectID string) (map[string]m
func (d Migration_2_8_28) setProjectRepository(projectID string, repoID string, repo map[string]interface{}) error {
return d.DB.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte("project__repository_" + projectID))
b, err := tx.CreateBucketIfNotExists([]byte("project__repository_" + projectID))
if err != nil {
return err
}
j, err := json.Marshal(repo)
if err != nil {
return err