feat(migration): tracer

This commit is contained in:
Fu Diwei 2025-05-26 21:49:37 +08:00
parent 3bb88d9f93
commit bf1d03a30e
15 changed files with 518 additions and 355 deletions

View File

@ -2,7 +2,6 @@ package migrations
import (
x509 "crypto/x509"
"log/slog"
"strings"
"github.com/pocketbase/pocketbase/core"
@ -12,7 +11,8 @@ import (
func init() {
m.Register(func(app core.App) error {
slog.Info("[CERTIMATE] migration: ready ...")
tracer := NewTracer("to v0.3")
tracer.Printf("go ...")
// backup collection records
collectionRecords := make([]*core.Record, 0)
@ -30,7 +30,7 @@ func init() {
}
collectionRecords = append(collectionRecords, records...)
slog.Info("[CERTIMATE] migration: collection '" + collection.Name + "' backed up")
tracer.Printf("collection '%s' backed up", collection.Name)
if collection.Name == "access" {
collection.Fields.RemoveByName("usage")
@ -107,7 +107,7 @@ func init() {
{
app.Delete(collection)
slog.Info("[CERTIMATE] migration: collection '" + collection.Name + "' truncated")
tracer.Printf("collection '%s' truncated", collection.Name)
}
}
}
@ -1729,7 +1729,7 @@ func init() {
return err
}
slog.Info("[CERTIMATE] migration: collections imported")
tracer.Printf("collections imported")
// restore records
for _, record := range collectionRecords {
@ -1795,12 +1795,11 @@ func init() {
return err
}
slog.Info("[CERTIMATE] migration: collection '" + record.Collection().Name + "' record #" + record.Id + " updated")
tracer.Printf("record #%s in collection '%s' updated", record.Id, record.Collection().Name)
}
}
slog.Info("[CERTIMATE] migration: done")
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil

View File

@ -7,14 +7,17 @@ import (
func init() {
m.Register(func(app core.App) error {
tracer := NewTracer("(v0.3)1740050400")
tracer.Printf("go ...")
// update collection `certificate`
{
certimateCollection, err := app.FindCollectionByNameOrId("4szxr9x43tpj6np")
collection, err := app.FindCollectionByNameOrId("4szxr9x43tpj6np")
if err != nil {
return err
}
if err := certimateCollection.Fields.AddMarshaledJSONAt(4, []byte(`{
if err := collection.Fields.AddMarshaledJSONAt(4, []byte(`{
"autogeneratePattern": "",
"hidden": false,
"id": "plmambpz",
@ -31,7 +34,7 @@ func init() {
return err
}
if err := certimateCollection.Fields.AddMarshaledJSONAt(5, []byte(`{
if err := collection.Fields.AddMarshaledJSONAt(5, []byte(`{
"autogeneratePattern": "",
"hidden": false,
"id": "49qvwxcg",
@ -48,7 +51,7 @@ func init() {
return err
}
if err := certimateCollection.Fields.AddMarshaledJSONAt(7, []byte(`{
if err := collection.Fields.AddMarshaledJSONAt(7, []byte(`{
"autogeneratePattern": "",
"hidden": false,
"id": "agt7n5bb",
@ -65,19 +68,21 @@ func init() {
return err
}
if err := app.Save(certimateCollection); err != nil {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
// update collection `workflow`
{
workflowCollection, err := app.FindCollectionByNameOrId("tovyif5ax6j62ur")
collection, err := app.FindCollectionByNameOrId("tovyif5ax6j62ur")
if err != nil {
return err
}
if err := workflowCollection.Fields.AddMarshaledJSONAt(6, []byte(`{
if err := collection.Fields.AddMarshaledJSONAt(6, []byte(`{
"hidden": false,
"id": "awlphkfe",
"maxSize": 5000000,
@ -90,7 +95,7 @@ func init() {
return err
}
if err := workflowCollection.Fields.AddMarshaledJSONAt(7, []byte(`{
if err := collection.Fields.AddMarshaledJSONAt(7, []byte(`{
"hidden": false,
"id": "g9ohkk5o",
"maxSize": 5000000,
@ -103,19 +108,21 @@ func init() {
return err
}
if err := app.Save(workflowCollection); err != nil {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
// update collection `workflow_output`
{
workflowOutputCollection, err := app.FindCollectionByNameOrId("bqnxb95f2cooowp")
collection, err := app.FindCollectionByNameOrId("bqnxb95f2cooowp")
if err != nil {
return err
}
if err := workflowOutputCollection.Fields.AddMarshaledJSONAt(4, []byte(`{
if err := collection.Fields.AddMarshaledJSONAt(4, []byte(`{
"hidden": false,
"id": "c2rm9omj",
"maxSize": 5000000,
@ -128,11 +135,14 @@ func init() {
return err
}
if err := app.Save(workflowOutputCollection); err != nil {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil

View File

@ -11,6 +11,9 @@ import (
func init() {
m.Register(func(app core.App) error {
tracer := NewTracer("(v0.3)1742209200")
tracer.Printf("go ...")
// create collection `workflow_logs`
{
jsonData := `{
@ -167,6 +170,8 @@ func init() {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' created", collection.Name)
}
// migrate data
@ -215,6 +220,8 @@ func init() {
if err := app.Save(record); err != nil {
return err
}
tracer.Printf("record #%s in collection '%s' updated", record.Id, collection.Name)
}
}
}
@ -243,6 +250,8 @@ func init() {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
// migrate data
@ -321,6 +330,8 @@ func init() {
if err := app.Save(workflowRun); err != nil {
return err
}
tracer.Printf("record #%s in collection '%s' updated", workflowRun.Id, workflowRun.Collection().Name)
}
}
@ -336,8 +347,11 @@ func init() {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil

View File

@ -7,12 +7,16 @@ import (
func init() {
m.Register(func(app core.App) error {
tracer := NewTracer("(v0.3)1742392800")
tracer.Printf("go ...")
// update collection `access`
{
collection, err := app.FindCollectionByNameOrId("4yzbv8urny5ja1e")
if err != nil {
return err
}
// update field
if err := collection.Fields.AddMarshaledJSONAt(2, []byte(`{
"hidden": false,
"id": "hwy7m03o",
@ -74,7 +78,15 @@ func init() {
return err
}
return app.Save(collection)
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil
})

View File

@ -7,6 +7,9 @@ import (
func init() {
m.Register(func(app core.App) error {
tracer := NewTracer("(v0.3)1742644800")
tracer.Printf("go ...")
// update collection `workflow_run`
{
collection, err := app.FindCollectionByNameOrId("qjp8lygssgwyqyz")
@ -35,6 +38,8 @@ func init() {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
// update collection `workflow_output`
@ -61,6 +66,8 @@ func init() {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
// update collection `workflow_logs`
@ -105,6 +112,8 @@ func init() {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
// update collection `access`
@ -182,8 +191,11 @@ func init() {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil

View File

@ -7,6 +7,9 @@ import (
func init() {
m.Register(func(app core.App) error {
tracer := NewTracer("(v0.3)1743264000")
tracer.Printf("go ...")
// update collection `settings`
{
collection, err := app.FindCollectionByNameOrId("dy6ccjb60spfy6p")
@ -52,6 +55,8 @@ func init() {
if err := app.Save(record); err != nil {
return err
}
tracer.Printf("record #%s in collection '%s' updated", record.Id, collection.Name)
}
}
@ -62,7 +67,6 @@ func init() {
return err
}
// update field
if err := collection.Fields.AddMarshaledJSONAt(2, []byte(`{
"hidden": false,
"id": "hwy7m03o",
@ -136,6 +140,8 @@ func init() {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
// update collection `acme_accounts`
@ -163,9 +169,12 @@ func init() {
if err := app.Save(record); err != nil {
return err
}
tracer.Printf("record #%s in collection '%s' updated", record.Id, collection.Name)
}
}
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil

View File

@ -7,12 +7,16 @@ import (
func init() {
m.Register(func(app core.App) error {
tracer := NewTracer("(v0.3)1744192800")
tracer.Printf("go ...")
// update collection `access`
{
collection, err := app.FindCollectionByNameOrId("4yzbv8urny5ja1e")
if err != nil {
return err
}
// update field
if err := collection.Fields.AddMarshaledJSONAt(2, []byte(`{
"hidden": false,
"id": "hwy7m03o",
@ -84,7 +88,15 @@ func init() {
return err
}
return app.Save(collection)
if err := app.Save(collection); err != nil {
return nil
}
tracer.Printf("collection '%s' updated", collection.Name)
}
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil
})

View File

@ -7,12 +7,16 @@ import (
func init() {
m.Register(func(app core.App) error {
tracer := NewTracer("(v0.3)1744459000")
tracer.Printf("go ...")
// update collection `access`
{
collection, err := app.FindCollectionByNameOrId("4yzbv8urny5ja1e")
if err != nil {
return err
}
// update field
if err := collection.Fields.AddMarshaledJSONAt(2, []byte(`{
"hidden": false,
"id": "hwy7m03o",
@ -85,7 +89,15 @@ func init() {
return err
}
return app.Save(collection)
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil
})

View File

@ -9,6 +9,11 @@ import (
func init() {
m.Register(func(app core.App) error {
tracer := NewTracer("(v0.3)1745308800")
tracer.Printf("go ...")
// update collection `access`
{
collection, err := app.FindCollectionByNameOrId("4yzbv8urny5ja1e")
if err != nil {
return err
@ -87,6 +92,10 @@ func init() {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil

View File

@ -7,6 +7,9 @@ import (
func init() {
m.Register(func(app core.App) error {
tracer := NewTracer("(v0.3)1745726400")
tracer.Printf("go ...")
// update collection `access`
{
collection, err := app.FindCollectionByNameOrId("4yzbv8urny5ja1e")
@ -34,53 +37,62 @@ func init() {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
// migrate data
{
accesses, err := app.FindAllRecords("access")
collection, err := app.FindCollectionByNameOrId("4yzbv8urny5ja1e")
if err != nil {
return err
}
for _, access := range accesses {
records, err := app.FindAllRecords(collection)
if err != nil {
return err
}
for _, record := range records {
changed := false
if access.GetString("provider") == "buypass" {
access.Set("reserve", "ca")
if record.GetString("provider") == "buypass" {
record.Set("reserve", "ca")
changed = true
} else if access.GetString("provider") == "googletrustservices" {
access.Set("reserve", "ca")
} else if record.GetString("provider") == "googletrustservices" {
record.Set("reserve", "ca")
changed = true
} else if access.GetString("provider") == "sslcom" {
access.Set("reserve", "ca")
} else if record.GetString("provider") == "sslcom" {
record.Set("reserve", "ca")
changed = true
} else if access.GetString("provider") == "zerossl" {
access.Set("reserve", "ca")
} else if record.GetString("provider") == "zerossl" {
record.Set("reserve", "ca")
changed = true
}
if access.GetString("provider") == "webhook" {
if record.GetString("provider") == "webhook" {
config := make(map[string]any)
if err := access.UnmarshalJSONField("config", &config); err != nil {
if err := record.UnmarshalJSONField("config", &config); err != nil {
return err
}
config["method"] = "POST"
config["headers"] = "Content-Type: application/json"
access.Set("config", config)
record.Set("config", config)
changed = true
}
if changed {
err = app.Save(access)
if err != nil {
if err := app.Save(record); err != nil {
return err
}
tracer.Printf("record #%s in collection '%s' updated", record.Id, collection.Name)
}
}
}
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil

View File

@ -7,36 +7,46 @@ import (
func init() {
m.Register(func(app core.App) error {
tracer := NewTracer("(v0.3)1747314000")
tracer.Printf("go ...")
// migrate data
{
accesses, err := app.FindAllRecords("access")
collection, err := app.FindCollectionByNameOrId("4yzbv8urny5ja1e")
if err != nil {
return err
}
for _, access := range accesses {
records, err := app.FindAllRecords(collection)
if err != nil {
return err
}
for _, record := range records {
changed := false
if access.GetString("provider") == "goedge" {
if record.GetString("provider") == "goedge" {
config := make(map[string]any)
if err := access.UnmarshalJSONField("config", &config); err != nil {
if err := record.UnmarshalJSONField("config", &config); err != nil {
return err
}
config["apiRole"] = "user"
access.Set("config", config)
record.Set("config", config)
changed = true
}
if changed {
err = app.Save(access)
if err != nil {
if err := app.Save(record); err != nil {
return err
}
tracer.Printf("record #%s in collection '%s' updated", record.Id, collection.Name)
}
}
}
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil

View File

@ -7,6 +7,9 @@ import (
func init() {
m.Register(func(app core.App) error {
tracer := NewTracer("(v0.3)1747389600")
tracer.Printf("go ...")
// update collection `certificate`
{
collection, err := app.FindCollectionByNameOrId("4szxr9x43tpj6np")
@ -34,38 +37,47 @@ func init() {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
// migrate data
{
accesses, err := app.FindAllRecords("access")
collection, err := app.FindCollectionByNameOrId("4yzbv8urny5ja1e")
if err != nil {
return err
}
for _, access := range accesses {
records, err := app.FindAllRecords(collection)
if err != nil {
return err
}
for _, record := range records {
changed := false
if access.GetString("provider") == "1panel" {
if record.GetString("provider") == "1panel" {
config := make(map[string]any)
if err := access.UnmarshalJSONField("config", &config); err != nil {
if err := record.UnmarshalJSONField("config", &config); err != nil {
return err
}
config["apiVersion"] = "v1"
access.Set("config", config)
record.Set("config", config)
changed = true
}
if changed {
err = app.Save(access)
if err != nil {
if err := app.Save(record); err != nil {
return err
}
tracer.Printf("record #%s in collection '%s' updated", record.Id, collection.Name)
}
}
}
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil

View File

@ -9,9 +9,17 @@ import (
func init() {
m.Register(func(app core.App) error {
tracer := NewTracer("(v0.3)1748178000")
tracer.Printf("go ...")
// migrate data
{
accesses, err := app.FindAllRecords("access")
collection, err := app.FindCollectionByNameOrId("4yzbv8urny5ja1e")
if err != nil {
return err
}
records, err := app.FindAllRecords(collection)
if err != nil {
return err
}
@ -29,30 +37,34 @@ func init() {
"ratpanel",
"safeline",
}
for _, access := range accesses {
for _, record := range records {
changed := false
if slices.Contains(providersToUpdate, access.GetString("provider")) {
if slices.Contains(providersToUpdate, record.GetString("provider")) {
config := make(map[string]any)
if err := access.UnmarshalJSONField("config", &config); err != nil {
if err := record.UnmarshalJSONField("config", &config); err != nil {
return err
}
if config["apiUrl"] != nil {
config["serverUrl"] = config["apiUrl"]
delete(config, "apiUrl")
access.Set("config", config)
record.Set("config", config)
changed = true
}
}
if changed {
err = app.Save(access)
if err != nil {
if err := app.Save(record); err != nil {
return err
}
tracer.Printf("record #%s in collection '%s' updated", record.Id, collection.Name)
}
}
}
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil

View File

@ -7,6 +7,9 @@ import (
func init() {
m.Register(func(app core.App) error {
tracer := NewTracer("(v0.3)1748228400")
tracer.Printf("go ...")
// update collection `certificate`
{
collection, err := app.FindCollectionByNameOrId("4szxr9x43tpj6np")
@ -30,8 +33,11 @@ func init() {
if err := app.Save(collection); err != nil {
return err
}
tracer.Printf("collection '%s' updated", collection.Name)
}
tracer.Printf("done")
return nil
}, func(app core.App) error {
return nil

22
migrations/tracer.go Normal file
View File

@ -0,0 +1,22 @@
package migrations
import (
"fmt"
"log/slog"
)
type Tracer struct {
logger *slog.Logger
flag string
}
func NewTracer(flag string) *Tracer {
return &Tracer{
logger: slog.Default(),
flag: flag,
}
}
func (l *Tracer) Printf(format string, args ...any) {
l.logger.Info("[CERTIMATE] migration " + l.flag + ": " + fmt.Sprintf(format, args...))
}