From 2d8fce32d39fd9bb0854b91bcb5b792c68f5cfdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxi=20Quo=C3=9F?= Date: Fri, 17 Jan 2025 00:53:39 +0100 Subject: [PATCH] feat: allow non http/https links for ports and devices https://github.com/seriousm4x/UpSnap/discussions/556 https://github.com/seriousm4x/UpSnap/discussions/662 https://github.com/seriousm4x/UpSnap/discussions/926 --- .../1737069556_url_to_string_devices.go | 106 ++++++++++++++++++ .../1737071155_url_to_string_ports.go | 106 ++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 backend/migrations/1737069556_url_to_string_devices.go create mode 100644 backend/migrations/1737071155_url_to_string_ports.go diff --git a/backend/migrations/1737069556_url_to_string_devices.go b/backend/migrations/1737069556_url_to_string_devices.go new file mode 100644 index 00000000..b98a7c0a --- /dev/null +++ b/backend/migrations/1737069556_url_to_string_devices.go @@ -0,0 +1,106 @@ +package migrations + +import ( + "github.com/pocketbase/pocketbase/core" + m "github.com/pocketbase/pocketbase/migrations" +) + +func init() { + m.Register(func(app core.App) error { + // Up migration: Change `url` field type from URL to string + collectionName := "devices" + + return app.RunInTransaction(func(txApp core.App) error { + // Step 1: Fetch the collection + collection, err := txApp.FindCollectionByNameOrId(collectionName) + if err != nil { + return err + } + + // Step 2: Add a new text field + newField := &core.TextField{ + Name: "new_field", + Required: false, + } + collection.Fields.AddAt(6, newField) + + if err := txApp.Save(collection); err != nil { + return err + } + + // Step 3: Migrate data from `url` to `new_field` + records, err := txApp.FindAllRecords(collectionName) + if err != nil { + return err + } + + for _, record := range records { + url := record.GetString("link") + record.Set("new_field", url) + if err := txApp.Save(record); err != nil { + return err + } + } + + // Step 4: Remove the old `url` field + collection.Fields.RemoveByName("link") + if err := txApp.Save(collection); err != nil { + return err + } + + // Step 5: Rename `new_field` to `url` + renamedField := collection.Fields.GetByName("new_field") + renamedField.SetName("link") + + return txApp.Save(collection) + }) + }, func(app core.App) error { + // Down migration: Restore the `url` field + collectionName := "devices" + + return app.RunInTransaction(func(txApp core.App) error { + // Step 1: Fetch the collection + collection, err := txApp.FindCollectionByNameOrId(collectionName) + if err != nil { + return err + } + + // Step 2: Add a new `url` field + newField := &core.URLField{ + Name: "new_field", + Required: false, + } + collection.Fields.AddAt(6, newField) + + if err := txApp.Save(collection); err != nil { + return err + } + + // Step 3: Migrate data from `url` to `new_field` + records, err := txApp.FindAllRecords(collectionName) + if err != nil { + return err + } + + for _, record := range records { + text := record.GetString("link") + record.Set("new_field", text) + if err := txApp.Save(record); err != nil { + return err + } + } + + // Step 4: Remove the old `url` field + collection.Fields.RemoveByName("link") + if err := txApp.Save(collection); err != nil { + return err + } + + // Step 5: Rename `new_field` to `url` + renamedField := collection.Fields.GetByName("new_field") + renamedField.SetName("link") + + return txApp.Save(collection) + }) + }) +} diff --git a/backend/migrations/1737071155_url_to_string_ports.go b/backend/migrations/1737071155_url_to_string_ports.go new file mode 100644 index 00000000..e959c3fe --- /dev/null +++ b/backend/migrations/1737071155_url_to_string_ports.go @@ -0,0 +1,106 @@ +package migrations + +import ( + "github.com/pocketbase/pocketbase/core" + m "github.com/pocketbase/pocketbase/migrations" +) + +func init() { + m.Register(func(app core.App) error { + // Up migration: Change `url` field type from URL to string + collectionName := "ports" + + return app.RunInTransaction(func(txApp core.App) error { + // Step 1: Fetch the collection + collection, err := txApp.FindCollectionByNameOrId(collectionName) + if err != nil { + return err + } + + // Step 2: Add a new text field + newField := &core.TextField{ + Name: "new_field", + Required: false, + } + collection.Fields.AddAt(5, newField) + + if err := txApp.Save(collection); err != nil { + return err + } + + // Step 3: Migrate data from `url` to `new_field` + records, err := txApp.FindAllRecords(collectionName) + if err != nil { + return err + } + + for _, record := range records { + url := record.GetString("link") + record.Set("new_field", url) + if err := txApp.Save(record); err != nil { + return err + } + } + + // Step 4: Remove the old `url` field + collection.Fields.RemoveByName("link") + if err := txApp.Save(collection); err != nil { + return err + } + + // Step 5: Rename `new_field` to `url` + renamedField := collection.Fields.GetByName("new_field") + renamedField.SetName("link") + + return txApp.Save(collection) + }) + }, func(app core.App) error { + // Down migration: Restore the `url` field + collectionName := "ports" + + return app.RunInTransaction(func(txApp core.App) error { + // Step 1: Fetch the collection + collection, err := txApp.FindCollectionByNameOrId(collectionName) + if err != nil { + return err + } + + // Step 2: Add a new `url` field + newField := &core.URLField{ + Name: "new_field", + Required: false, + } + collection.Fields.AddAt(5, newField) + + if err := txApp.Save(collection); err != nil { + return err + } + + // Step 3: Migrate data from `url` to `new_field` + records, err := txApp.FindAllRecords(collectionName) + if err != nil { + return err + } + + for _, record := range records { + text := record.GetString("link") + record.Set("new_field", text) + if err := txApp.Save(record); err != nil { + return err + } + } + + // Step 4: Remove the old `url` field + collection.Fields.RemoveByName("link") + if err := txApp.Save(collection); err != nil { + return err + } + + // Step 5: Rename `new_field` to `url` + renamedField := collection.Fields.GetByName("new_field") + renamedField.SetName("link") + + return txApp.Save(collection) + }) + }) +}