-
-
Notifications
You must be signed in to change notification settings - Fork 979
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(be): implement migrations for BoltDB
- Loading branch information
Showing
11 changed files
with
261 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package db | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// Migration represents sql schema version | ||
type Migration struct { | ||
Version string `db:"version" json:"version"` | ||
UpgradedDate *time.Time `db:"upgraded_date" json:"upgraded_date"` | ||
Notes *string `db:"notes" json:"notes"` | ||
} | ||
|
||
// HumanoidVersion adds a v to the VersionString | ||
func (m Migration) HumanoidVersion() string { | ||
return "v" + m.Version | ||
} | ||
|
||
func GetMigrations() []Migration { | ||
return []Migration{ | ||
{}, | ||
{Version: "1.0.0"}, | ||
{Version: "1.2.0"}, | ||
{Version: "1.3.0"}, | ||
{Version: "1.4.0"}, | ||
{Version: "1.5.0"}, | ||
{Version: "1.6.0"}, | ||
{Version: "1.7.0"}, | ||
{Version: "1.8.0"}, | ||
{Version: "1.9.0"}, | ||
{Version: "2.2.1"}, | ||
{Version: "2.3.0"}, | ||
{Version: "2.3.1"}, | ||
{Version: "2.3.2"}, | ||
{Version: "2.4.0"}, | ||
{Version: "2.5.0"}, | ||
{Version: "2.5.2"}, | ||
{Version: "2.7.1"}, | ||
{Version: "2.7.4"}, | ||
{Version: "2.7.6"}, | ||
{Version: "2.7.8"}, | ||
{Version: "2.7.9"}, | ||
{Version: "2.7.10"}, | ||
{Version: "2.7.12"}, | ||
{Version: "2.7.13"}, | ||
{Version: "2.8.0"}, | ||
{Version: "2.8.1"}, | ||
{Version: "2.8.7"}, | ||
{Version: "2.8.8"}, | ||
{Version: "2.8.20"}, | ||
{Version: "2.8.25"}, | ||
{Version: "2.8.26"}, | ||
} | ||
} | ||
|
||
func Migrate(d Store) error { | ||
fmt.Println("Checking db migrations") | ||
didRun := false | ||
|
||
for _, version := range GetMigrations() { | ||
if exists, err := d.IsMigrationApplied(version); err != nil || exists { | ||
if exists { | ||
continue | ||
} | ||
|
||
return err | ||
} | ||
|
||
didRun = true | ||
fmt.Printf("Executing migration %s (at %v)...\n", version.HumanoidVersion(), time.Now()) | ||
if err := d.ApplyMigration(version); err != nil { | ||
fmt.Printf("Rolling back %s (time: %v)...\n", version.HumanoidVersion(), time.Now()) | ||
d.TryRollbackMigration(version) | ||
return err | ||
} | ||
} | ||
|
||
if didRun { | ||
fmt.Println("Migrations Finished") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package bolt | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/ansible-semaphore/semaphore/db" | ||
"github.com/ansible-semaphore/semaphore/db/bolt/migrations" | ||
"go.etcd.io/bbolt" | ||
) | ||
|
||
func (d *BoltDb) IsMigrationApplied(migration db.Migration) (bool, error) { | ||
err := d.db.View(func(tx *bbolt.Tx) error { | ||
b := tx.Bucket([]byte("versions")) | ||
if b == nil { | ||
return db.ErrNotFound | ||
} | ||
|
||
d := b.Get([]byte(migration.Version)) | ||
|
||
if d == nil { | ||
return db.ErrNotFound | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
if err == nil { | ||
return true, nil | ||
} | ||
|
||
if err == db.ErrNotFound { | ||
return false, nil | ||
} | ||
|
||
return false, err | ||
} | ||
|
||
func (d *BoltDb) ApplyMigration(migration db.Migration) (err error) { | ||
switch migration.Version { | ||
case "2.8.26": | ||
err = migrations.Migration_2_8_28{DB: d.db}.Apply() | ||
} | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
return d.db.Update(func(tx *bbolt.Tx) error { | ||
b, err := tx.CreateBucketIfNotExists([]byte("versions")) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
j, err := json.Marshal(migration) | ||
|
||
return b.Put([]byte(migration.Version), j) | ||
}) | ||
} | ||
|
||
func (d *BoltDb) TryRollbackMigration(migration db.Migration) { | ||
switch migration.Version { | ||
case "2.8.26": | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package migrations | ||
|
||
import ( | ||
"encoding/json" | ||
"go.etcd.io/bbolt" | ||
"strings" | ||
) | ||
|
||
type Migration_2_8_28 struct { | ||
DB *bbolt.DB | ||
} | ||
|
||
func (d Migration_2_8_28) getProjectRepositories_2_8_26(projectID string) (repos map[string]map[string]interface{}, err error) { | ||
err = d.DB.View(func(tx *bbolt.Tx) error { | ||
b := tx.Bucket([]byte("project__repository_" + projectID)) | ||
return b.ForEach(func(id, body []byte) error { | ||
r := make(map[string]interface{}) | ||
repos[string(id)] = r | ||
return json.Unmarshal(body, &r) | ||
}) | ||
}) | ||
return | ||
} | ||
|
||
func (d Migration_2_8_28) setProjectRepository_2_8_26(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)) | ||
j, err := json.Marshal(repo) | ||
if err != nil { | ||
return err | ||
} | ||
return b.Put([]byte(repoID), j) | ||
}) | ||
} | ||
|
||
func (d Migration_2_8_28) Apply() (err error) { | ||
var projectIDs []string | ||
|
||
err = d.DB.View(func(tx *bbolt.Tx) error { | ||
return tx.Bucket([]byte("projects")).ForEach(func(id, _ []byte) error { | ||
projectIDs = append(projectIDs, string(id)) | ||
return nil | ||
}) | ||
}) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
projectsRepositories := make(map[string]map[string]map[string]interface{}) | ||
|
||
for _, projectID := range projectIDs { | ||
var err2 error | ||
projectsRepositories[projectID], err2 = d.getProjectRepositories_2_8_26(projectID) | ||
if err2 != nil { | ||
return err2 | ||
} | ||
} | ||
|
||
for projectID, repositories := range projectsRepositories { | ||
for repoID, repo := range repositories { | ||
branch := "master" | ||
url := repo["git_url"].(string) | ||
parts := strings.Split(url, "#") | ||
if len(parts) > 1 { | ||
url, branch = parts[0], parts[1] | ||
} | ||
repo["git_url"] = url | ||
repo["git_branch"] = branch | ||
err = d.setProjectRepository_2_8_26(projectID, repoID, repo) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.