From 26c3479f8a405f1731b97cf1dbb994270e62665b Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Wed, 10 Jul 2024 00:26:12 +0500 Subject: [PATCH] feat(apps): add migration '' -> 'ansible' --- db/Migration.go | 1 + db/bolt/migration.go | 2 + db/bolt/migration_2_10_16.go | 38 ++++++++++ db/bolt/migration_2_10_16_test.go | 88 +++++++++++++++++++++++ db/sql/migrations/v2.10.16.sql | 1 + web/src/components/EditTemplateDialog.vue | 80 ++++++++++++++------- web/src/components/TemplateForm.vue | 29 +++++--- 7 files changed, 204 insertions(+), 35 deletions(-) create mode 100644 db/bolt/migration_2_10_16.go create mode 100644 db/bolt/migration_2_10_16_test.go create mode 100644 db/sql/migrations/v2.10.16.sql diff --git a/db/Migration.go b/db/Migration.go index 8d5f78c0..463dee59 100644 --- a/db/Migration.go +++ b/db/Migration.go @@ -70,6 +70,7 @@ func GetMigrations() []Migration { {Version: "2.9.100"}, {Version: "2.10.12"}, {Version: "2.10.15"}, + {Version: "2.10.16"}, } } diff --git a/db/bolt/migration.go b/db/bolt/migration.go index b07a6e3b..b3090f0b 100644 --- a/db/bolt/migration.go +++ b/db/bolt/migration.go @@ -43,6 +43,8 @@ func (d *BoltDb) ApplyMigration(m db.Migration) (err error) { err = migration_2_8_91{migration{d.db}}.Apply() case "2.10.12": err = migration_2_10_12{migration{d.db}}.Apply() + case "2.10.16": + err = migration_2_10_16{migration{d.db}}.Apply() } if err != nil { diff --git a/db/bolt/migration_2_10_16.go b/db/bolt/migration_2_10_16.go new file mode 100644 index 00000000..cfcb8d00 --- /dev/null +++ b/db/bolt/migration_2_10_16.go @@ -0,0 +1,38 @@ +package bolt + +type migration_2_10_16 struct { + migration +} + +func (d migration_2_10_16) Apply() (err error) { + projectIDs, err := d.getProjectIDs() + + if err != nil { + return + } + + templates := make(map[string]map[string]map[string]interface{}) + + for _, projectID := range projectIDs { + var err2 error + templates[projectID], err2 = d.getObjects(projectID, "template") + if err2 != nil { + return err2 + } + } + + for projectID, projectTemplates := range templates { + for repoID, tpl := range projectTemplates { + if tpl["app"] != nil && tpl["app"] != "" { + continue + } + tpl["app"] = "ansible" + err = d.setObject(projectID, "template", repoID, tpl) + if err != nil { + return err + } + } + } + + return +} diff --git a/db/bolt/migration_2_10_16_test.go b/db/bolt/migration_2_10_16_test.go new file mode 100644 index 00000000..c5efee23 --- /dev/null +++ b/db/bolt/migration_2_10_16_test.go @@ -0,0 +1,88 @@ +package bolt + +import ( + "encoding/json" + "go.etcd.io/bbolt" + "testing" +) + +func TestMigration_2_10_16_Apply(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("{}")) + if err != nil { + return err + } + + r, err := tx.CreateBucketIfNotExists([]byte("project__template_0000000001")) + if err != nil { + return err + } + + err = r.Put([]byte("0000000001"), + []byte("{\"id\":\"1\",\"project_id\":\"1\"}")) + + return err + }) + + if err != nil { + t.Fatal(err) + } + + err = migration_2_10_16{migration{store.db}}.Apply() + if err != nil { + t.Fatal(err) + } + + var repo map[string]interface{} + err = store.db.View(func(tx *bbolt.Tx) error { + b := tx.Bucket([]byte("project__template_0000000001")) + str := string(b.Get([]byte("0000000001"))) + return json.Unmarshal([]byte(str), &repo) + }) + if err != nil { + t.Fatal(err) + } + + if repo["app"] == nil { + t.Fatal("app must be set") + } + + if repo["app"].(string) != "ansible" { + t.Fatal("invalid app: " + repo["app"].(string)) + } + + if repo["alias"] != nil { + t.Fatal("alias must be deleted") + } +} + +func TestMigration_2_10_16_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 = migration_2_10_16{migration{store.db}}.Apply() + if err != nil { + t.Fatal(err) + } +} diff --git a/db/sql/migrations/v2.10.16.sql b/db/sql/migrations/v2.10.16.sql new file mode 100644 index 00000000..bef03d17 --- /dev/null +++ b/db/sql/migrations/v2.10.16.sql @@ -0,0 +1 @@ +update `project__template` set `app` = 'ansible' where `app` = ''; diff --git a/web/src/components/EditTemplateDialog.vue b/web/src/components/EditTemplateDialog.vue index 26b334c0..9430ce24 100644 --- a/web/src/components/EditTemplateDialog.vue +++ b/web/src/components/EditTemplateDialog.vue @@ -11,8 +11,7 @@ @save="onSave" > @@ -53,16 +32,57 @@