From fe93334f865cc25e04c0a824ebc9c3a9ba0785ac Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Fri, 14 Feb 2025 00:27:25 +0800 Subject: [PATCH] chore: create migration snapshot --- internal/certificate/service.go | 4 +- main.go | 10 +- migrations/1737141502_superusers_initial.go | 27 -- migrations/1738767422_updated_certificate.go | 127 ------- migrations/1738828775_updated_workflow.go | 65 ---- migrations/1738828788_updated_workflow_run.go | 65 ---- migrations/1738839725_updated_certificate.go | 67 ---- .../1738840633_updated_workflow_output.go | 63 ---- migrations/1739202463_updated_access.go | 99 ------ migrations/1739263253_updated_workflow_run.go | 58 ---- .../1739263264_updated_workflow_output.go | 92 ----- migrations/1739453752_updated_access.go | 161 --------- ....go => 1739462400_collections_snapshot.go} | 324 ++++++++++++++++-- migrations/1739462401_superusers_initial.go | 44 +++ 14 files changed, 350 insertions(+), 856 deletions(-) delete mode 100644 migrations/1737141502_superusers_initial.go delete mode 100644 migrations/1738767422_updated_certificate.go delete mode 100644 migrations/1738828775_updated_workflow.go delete mode 100644 migrations/1738828788_updated_workflow_run.go delete mode 100644 migrations/1738839725_updated_certificate.go delete mode 100644 migrations/1738840633_updated_workflow_output.go delete mode 100644 migrations/1739202463_updated_access.go delete mode 100644 migrations/1739263253_updated_workflow_run.go delete mode 100644 migrations/1739263264_updated_workflow_output.go delete mode 100644 migrations/1739453752_updated_access.go rename migrations/{1737141501_collections_snapshot.go => 1739462400_collections_snapshot.go} (80%) create mode 100644 migrations/1739462401_superusers_initial.go diff --git a/internal/certificate/service.go b/internal/certificate/service.go index a207d1c3..adcb8b97 100644 --- a/internal/certificate/service.go +++ b/internal/certificate/service.go @@ -41,13 +41,13 @@ func NewCertificateService(certRepo certificateRepository) *CertificateService { func (s *CertificateService) InitSchedule(ctx context.Context) error { app.GetScheduler().MustAdd("certificateExpireSoonNotify", "0 0 * * *", func() { - certs, err := s.certRepo.ListExpireSoon(context.Background()) + certificates, err := s.certRepo.ListExpireSoon(context.Background()) if err != nil { app.GetLogger().Error("failed to get certificates which expire soon", "err", err) return } - notification := buildExpireSoonNotification(certs) + notification := buildExpireSoonNotification(certificates) if notification == nil { return } diff --git a/main.go b/main.go index 2c3d84c2..73d1a2a9 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,7 @@ package main import ( "flag" - "log" + "log/slog" "os" "strings" _ "time/tzdata" @@ -56,15 +56,13 @@ func main() { app.OnTerminate().BindFunc(func(e *core.TerminateEvent) error { routes.Unregister() - - log.Println("Exit!") - + slog.Info("[CERTIMATE] Exit!") return e.Next() }) - log.Printf("Visit the website: http://%s", flagHttp) + slog.Info("[CERTIMATE] Visit the website: http://" + flagHttp) if err := app.Start(); err != nil { - log.Fatal(err) + slog.Error("[CERTIMATE] Start failed.", "err", err) } } diff --git a/migrations/1737141502_superusers_initial.go b/migrations/1737141502_superusers_initial.go deleted file mode 100644 index 4440ed76..00000000 --- a/migrations/1737141502_superusers_initial.go +++ /dev/null @@ -1,27 +0,0 @@ -package migrations - -import ( - "github.com/pocketbase/pocketbase/core" - m "github.com/pocketbase/pocketbase/migrations" -) - -func init() { - m.Register(func(app core.App) error { - superusers, err := app.FindCollectionByNameOrId(core.CollectionNameSuperusers) - if err != nil { - return err - } - - record, _ := app.FindAuthRecordByEmail(core.CollectionNameSuperusers, "admin@certimate.fun") - if record == nil { - record := core.NewRecord(superusers) - record.Set("email", "admin@certimate.fun") - record.Set("password", "1234567890") - return app.Save(record) - } - - return nil - }, func(app core.App) error { - return nil - }) -} diff --git a/migrations/1738767422_updated_certificate.go b/migrations/1738767422_updated_certificate.go deleted file mode 100644 index e5dfe573..00000000 --- a/migrations/1738767422_updated_certificate.go +++ /dev/null @@ -1,127 +0,0 @@ -package migrations - -import ( - "encoding/json" - - "github.com/pocketbase/pocketbase/core" - m "github.com/pocketbase/pocketbase/migrations" -) - -func init() { - m.Register(func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("4szxr9x43tpj6np") - if err != nil { - return err - } - - // update collection data - if err := json.Unmarshal([]byte(`{ - "indexes": [ - "CREATE INDEX ` + "`" + `idx_Jx8TXzDCmw` + "`" + ` ON ` + "`" + `certificate` + "`" + ` (` + "`" + `workflowId` + "`" + `)", - "CREATE INDEX ` + "`" + `idx_kcKpgAZapk` + "`" + ` ON ` + "`" + `certificate` + "`" + ` (` + "`" + `workflowNodeId` + "`" + `)" - ] - }`), &collection); err != nil { - return err - } - - // add field - if err := collection.Fields.AddMarshaledJSONAt(3, []byte(`{ - "autogeneratePattern": "", - "hidden": false, - "id": "text2069360702", - "max": 0, - "min": 0, - "name": "serialNumber", - "pattern": "", - "presentable": false, - "primaryKey": false, - "required": false, - "system": false, - "type": "text" - }`)); err != nil { - return err - } - - // add field - if err := collection.Fields.AddMarshaledJSONAt(6, []byte(`{ - "autogeneratePattern": "", - "hidden": false, - "id": "text2910474005", - "max": 0, - "min": 0, - "name": "issuer", - "pattern": "", - "presentable": false, - "primaryKey": false, - "required": false, - "system": false, - "type": "text" - }`)); err != nil { - return err - } - - // add field - if err := collection.Fields.AddMarshaledJSONAt(8, []byte(`{ - "autogeneratePattern": "", - "hidden": false, - "id": "text4164403445", - "max": 0, - "min": 0, - "name": "keyAlgorithm", - "pattern": "", - "presentable": false, - "primaryKey": false, - "required": false, - "system": false, - "type": "text" - }`)); err != nil { - return err - } - - // add field - if err := collection.Fields.AddMarshaledJSONAt(11, []byte(`{ - "autogeneratePattern": "", - "hidden": false, - "id": "text2045248758", - "max": 0, - "min": 0, - "name": "acmeAccountUrl", - "pattern": "", - "presentable": false, - "primaryKey": false, - "required": false, - "system": false, - "type": "text" - }`)); err != nil { - return err - } - - return app.Save(collection) - }, func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("4szxr9x43tpj6np") - if err != nil { - return err - } - - // update collection data - if err := json.Unmarshal([]byte(`{ - "indexes": [] - }`), &collection); err != nil { - return err - } - - // remove field - collection.Fields.RemoveById("text2069360702") - - // remove field - collection.Fields.RemoveById("text2910474005") - - // remove field - collection.Fields.RemoveById("text4164403445") - - // remove field - collection.Fields.RemoveById("text2045248758") - - return app.Save(collection) - }) -} diff --git a/migrations/1738828775_updated_workflow.go b/migrations/1738828775_updated_workflow.go deleted file mode 100644 index 7e06df36..00000000 --- a/migrations/1738828775_updated_workflow.go +++ /dev/null @@ -1,65 +0,0 @@ -package migrations - -import ( - "github.com/pocketbase/pocketbase/core" - m "github.com/pocketbase/pocketbase/migrations" -) - -func init() { - m.Register(func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("tovyif5ax6j62ur") - if err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(10, []byte(`{ - "hidden": false, - "id": "zivdxh23", - "maxSelect": 1, - "name": "lastRunStatus", - "presentable": false, - "required": false, - "system": false, - "type": "select", - "values": [ - "pending", - "running", - "succeeded", - "failed", - "canceled" - ] - }`)); err != nil { - return err - } - - return app.Save(collection) - }, func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("tovyif5ax6j62ur") - if err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(10, []byte(`{ - "hidden": false, - "id": "zivdxh23", - "maxSelect": 1, - "name": "lastRunStatus", - "presentable": false, - "required": false, - "system": false, - "type": "select", - "values": [ - "pending", - "running", - "succeeded", - "failed" - ] - }`)); err != nil { - return err - } - - return app.Save(collection) - }) -} diff --git a/migrations/1738828788_updated_workflow_run.go b/migrations/1738828788_updated_workflow_run.go deleted file mode 100644 index b5ff20a7..00000000 --- a/migrations/1738828788_updated_workflow_run.go +++ /dev/null @@ -1,65 +0,0 @@ -package migrations - -import ( - "github.com/pocketbase/pocketbase/core" - m "github.com/pocketbase/pocketbase/migrations" -) - -func init() { - m.Register(func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("qjp8lygssgwyqyz") - if err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(2, []byte(`{ - "hidden": false, - "id": "qldmh0tw", - "maxSelect": 1, - "name": "status", - "presentable": false, - "required": false, - "system": false, - "type": "select", - "values": [ - "pending", - "running", - "succeeded", - "failed", - "canceled" - ] - }`)); err != nil { - return err - } - - return app.Save(collection) - }, func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("qjp8lygssgwyqyz") - if err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(2, []byte(`{ - "hidden": false, - "id": "qldmh0tw", - "maxSelect": 1, - "name": "status", - "presentable": false, - "required": false, - "system": false, - "type": "select", - "values": [ - "pending", - "running", - "succeeded", - "failed" - ] - }`)); err != nil { - return err - } - - return app.Save(collection) - }) -} diff --git a/migrations/1738839725_updated_certificate.go b/migrations/1738839725_updated_certificate.go deleted file mode 100644 index 447d5297..00000000 --- a/migrations/1738839725_updated_certificate.go +++ /dev/null @@ -1,67 +0,0 @@ -package migrations - -import ( - "encoding/json" - - "github.com/pocketbase/pocketbase/core" - m "github.com/pocketbase/pocketbase/migrations" -) - -func init() { - m.Register(func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("4szxr9x43tpj6np") - if err != nil { - return err - } - - // update collection data - if err := json.Unmarshal([]byte(`{ - "indexes": [ - "CREATE INDEX ` + "`" + `idx_Jx8TXzDCmw` + "`" + ` ON ` + "`" + `certificate` + "`" + ` (` + "`" + `workflowId` + "`" + `)", - "CREATE INDEX ` + "`" + `idx_kcKpgAZapk` + "`" + ` ON ` + "`" + `certificate` + "`" + ` (` + "`" + `workflowNodeId` + "`" + `)", - "CREATE INDEX ` + "`" + `idx_2cRXqNDyyp` + "`" + ` ON ` + "`" + `certificate` + "`" + ` (` + "`" + `workflowRunId` + "`" + `)" - ] - }`), &collection); err != nil { - return err - } - - // add field - if err := collection.Fields.AddMarshaledJSONAt(15, []byte(`{ - "cascadeDelete": false, - "collectionId": "qjp8lygssgwyqyz", - "hidden": false, - "id": "relation3917999135", - "maxSelect": 1, - "minSelect": 0, - "name": "workflowRunId", - "presentable": false, - "required": false, - "system": false, - "type": "relation" - }`)); err != nil { - return err - } - - return app.Save(collection) - }, func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("4szxr9x43tpj6np") - if err != nil { - return err - } - - // update collection data - if err := json.Unmarshal([]byte(`{ - "indexes": [ - "CREATE INDEX ` + "`" + `idx_Jx8TXzDCmw` + "`" + ` ON ` + "`" + `certificate` + "`" + ` (` + "`" + `workflowId` + "`" + `)", - "CREATE INDEX ` + "`" + `idx_kcKpgAZapk` + "`" + ` ON ` + "`" + `certificate` + "`" + ` (` + "`" + `workflowNodeId` + "`" + `)" - ] - }`), &collection); err != nil { - return err - } - - // remove field - collection.Fields.RemoveById("relation3917999135") - - return app.Save(collection) - }) -} diff --git a/migrations/1738840633_updated_workflow_output.go b/migrations/1738840633_updated_workflow_output.go deleted file mode 100644 index 6e836a76..00000000 --- a/migrations/1738840633_updated_workflow_output.go +++ /dev/null @@ -1,63 +0,0 @@ -package migrations - -import ( - "encoding/json" - - "github.com/pocketbase/pocketbase/core" - m "github.com/pocketbase/pocketbase/migrations" -) - -func init() { - m.Register(func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("bqnxb95f2cooowp") - if err != nil { - return err - } - - // update collection data - if err := json.Unmarshal([]byte(`{ - "indexes": [ - "CREATE INDEX ` + "`" + `idx_BYoQPsz4my` + "`" + ` ON ` + "`" + `workflow_output` + "`" + ` (` + "`" + `workflowId` + "`" + `)", - "CREATE INDEX ` + "`" + `idx_O9zxLETuxJ` + "`" + ` ON ` + "`" + `workflow_output` + "`" + ` (` + "`" + `runId` + "`" + `)" - ] - }`), &collection); err != nil { - return err - } - - // add field - if err := collection.Fields.AddMarshaledJSONAt(2, []byte(`{ - "cascadeDelete": false, - "collectionId": "qjp8lygssgwyqyz", - "hidden": false, - "id": "relation821863227", - "maxSelect": 1, - "minSelect": 0, - "name": "runId", - "presentable": false, - "required": false, - "system": false, - "type": "relation" - }`)); err != nil { - return err - } - - return app.Save(collection) - }, func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("bqnxb95f2cooowp") - if err != nil { - return err - } - - // update collection data - if err := json.Unmarshal([]byte(`{ - "indexes": [] - }`), &collection); err != nil { - return err - } - - // remove field - collection.Fields.RemoveById("relation821863227") - - return app.Save(collection) - }) -} diff --git a/migrations/1739202463_updated_access.go b/migrations/1739202463_updated_access.go deleted file mode 100644 index ccaffbce..00000000 --- a/migrations/1739202463_updated_access.go +++ /dev/null @@ -1,99 +0,0 @@ -package migrations - -import ( - "github.com/pocketbase/pocketbase/core" - m "github.com/pocketbase/pocketbase/migrations" -) - -func init() { - m.Register(func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("4yzbv8urny5ja1e") - if err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(2, []byte(`{ - "hidden": false, - "id": "hwy7m03o", - "maxSelect": 1, - "name": "provider", - "presentable": false, - "required": false, - "system": false, - "type": "select", - "values": [ - "acmehttpreq", - "aliyun", - "aws", - "azure", - "baiducloud", - "baotapanel", - "byteplus", - "cloudflare", - "dogecloud", - "godaddy", - "huaweicloud", - "k8s", - "local", - "namedotcom", - "namesilo", - "powerdns", - "qiniu", - "ssh", - "tencentcloud", - "ucloud", - "volcengine", - "webhook" - ] - }`)); err != nil { - return err - } - - return app.Save(collection) - }, func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("4yzbv8urny5ja1e") - if err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(2, []byte(`{ - "hidden": false, - "id": "hwy7m03o", - "maxSelect": 1, - "name": "provider", - "presentable": false, - "required": false, - "system": false, - "type": "select", - "values": [ - "acmehttpreq", - "aliyun", - "aws", - "azure", - "baiducloud", - "byteplus", - "cloudflare", - "dogecloud", - "godaddy", - "huaweicloud", - "k8s", - "local", - "namedotcom", - "namesilo", - "powerdns", - "qiniu", - "ssh", - "tencentcloud", - "ucloud", - "volcengine", - "webhook" - ] - }`)); err != nil { - return err - } - - return app.Save(collection) - }) -} diff --git a/migrations/1739263253_updated_workflow_run.go b/migrations/1739263253_updated_workflow_run.go deleted file mode 100644 index 5286a7b8..00000000 --- a/migrations/1739263253_updated_workflow_run.go +++ /dev/null @@ -1,58 +0,0 @@ -package migrations - -import ( - "github.com/pocketbase/pocketbase/core" - m "github.com/pocketbase/pocketbase/migrations" -) - -func init() { - m.Register(func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("qjp8lygssgwyqyz") - if err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(1, []byte(`{ - "cascadeDelete": true, - "collectionId": "tovyif5ax6j62ur", - "hidden": false, - "id": "m8xfsyyy", - "maxSelect": 1, - "minSelect": 0, - "name": "workflowId", - "presentable": false, - "required": false, - "system": false, - "type": "relation" - }`)); err != nil { - return err - } - - return app.Save(collection) - }, func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("qjp8lygssgwyqyz") - if err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(1, []byte(`{ - "cascadeDelete": false, - "collectionId": "tovyif5ax6j62ur", - "hidden": false, - "id": "m8xfsyyy", - "maxSelect": 1, - "minSelect": 0, - "name": "workflowId", - "presentable": false, - "required": false, - "system": false, - "type": "relation" - }`)); err != nil { - return err - } - - return app.Save(collection) - }) -} diff --git a/migrations/1739263264_updated_workflow_output.go b/migrations/1739263264_updated_workflow_output.go deleted file mode 100644 index e2add066..00000000 --- a/migrations/1739263264_updated_workflow_output.go +++ /dev/null @@ -1,92 +0,0 @@ -package migrations - -import ( - "github.com/pocketbase/pocketbase/core" - m "github.com/pocketbase/pocketbase/migrations" -) - -func init() { - m.Register(func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("bqnxb95f2cooowp") - if err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(1, []byte(`{ - "cascadeDelete": true, - "collectionId": "tovyif5ax6j62ur", - "hidden": false, - "id": "jka88auc", - "maxSelect": 1, - "minSelect": 0, - "name": "workflowId", - "presentable": false, - "required": false, - "system": false, - "type": "relation" - }`)); err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(2, []byte(`{ - "cascadeDelete": true, - "collectionId": "qjp8lygssgwyqyz", - "hidden": false, - "id": "relation821863227", - "maxSelect": 1, - "minSelect": 0, - "name": "runId", - "presentable": false, - "required": false, - "system": false, - "type": "relation" - }`)); err != nil { - return err - } - - return app.Save(collection) - }, func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("bqnxb95f2cooowp") - if err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(1, []byte(`{ - "cascadeDelete": false, - "collectionId": "tovyif5ax6j62ur", - "hidden": false, - "id": "jka88auc", - "maxSelect": 1, - "minSelect": 0, - "name": "workflowId", - "presentable": false, - "required": false, - "system": false, - "type": "relation" - }`)); err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(2, []byte(`{ - "cascadeDelete": false, - "collectionId": "qjp8lygssgwyqyz", - "hidden": false, - "id": "relation821863227", - "maxSelect": 1, - "minSelect": 0, - "name": "runId", - "presentable": false, - "required": false, - "system": false, - "type": "relation" - }`)); err != nil { - return err - } - - return app.Save(collection) - }) -} diff --git a/migrations/1739453752_updated_access.go b/migrations/1739453752_updated_access.go deleted file mode 100644 index 31dd607a..00000000 --- a/migrations/1739453752_updated_access.go +++ /dev/null @@ -1,161 +0,0 @@ -package migrations - -import ( - "encoding/json" - - "github.com/pocketbase/pocketbase/core" - m "github.com/pocketbase/pocketbase/migrations" -) - -func init() { - m.Register(func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("4yzbv8urny5ja1e") - if err != nil { - return err - } - - // update collection data - if err := json.Unmarshal([]byte(`{ - "indexes": [ - "CREATE INDEX `+"`"+`idx_wkoST0j`+"`"+` ON `+"`"+`access`+"`"+` (`+"`"+`name`+"`"+`)", - "CREATE INDEX `+"`"+`idx_frh0JT1Aqx`+"`"+` ON `+"`"+`access`+"`"+` (`+"`"+`provider`+"`"+`)" - ] - }`), &collection); err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(2, []byte(`{ - "hidden": false, - "id": "hwy7m03o", - "maxSelect": 1, - "name": "provider", - "presentable": false, - "required": false, - "system": false, - "type": "select", - "values": [ - "1panel", - "acmehttpreq", - "akamai", - "aliyun", - "aws", - "azure", - "baiducloud", - "baishan", - "baotapanel", - "byteplus", - "cachefly", - "cdnfly", - "cloudflare", - "cloudns", - "cmcccloud", - "ctcccloud", - "cucccloud", - "dogecloud", - "edgio", - "fastly", - "gname", - "gcore", - "godaddy", - "goedge", - "huaweicloud", - "k8s", - "local", - "namedotcom", - "namesilo", - "ns1", - "powerdns", - "qiniu", - "rainyun", - "safeline", - "ssh", - "tencentcloud", - "ucloud", - "volcengine", - "webhook", - "westcn" - ] - }`)); err != nil { - return err - } - - // remove field - collection.Fields.RemoveById("hsxcnlvd") - - return app.Save(collection) - }, func(app core.App) error { - collection, err := app.FindCollectionByNameOrId("4yzbv8urny5ja1e") - if err != nil { - return err - } - - // update collection data - if err := json.Unmarshal([]byte(`{ - "indexes": [ - "CREATE INDEX `+"`"+`idx_wkoST0j`+"`"+` ON `+"`"+`access`+"`"+` (`+"`"+`name`+"`"+`)" - ] - }`), &collection); err != nil { - return err - } - - // update field - if err := collection.Fields.AddMarshaledJSONAt(2, []byte(`{ - "hidden": false, - "id": "hwy7m03o", - "maxSelect": 1, - "name": "provider", - "presentable": false, - "required": false, - "system": false, - "type": "select", - "values": [ - "acmehttpreq", - "aliyun", - "aws", - "azure", - "baiducloud", - "baotapanel", - "byteplus", - "cloudflare", - "dogecloud", - "godaddy", - "huaweicloud", - "k8s", - "local", - "namedotcom", - "namesilo", - "powerdns", - "qiniu", - "ssh", - "tencentcloud", - "ucloud", - "volcengine", - "webhook" - ] - }`)); err != nil { - return err - } - - // add field - if err := collection.Fields.AddMarshaledJSONAt(4, []byte(`{ - "hidden": false, - "id": "hsxcnlvd", - "maxSelect": 1, - "name": "usage", - "presentable": false, - "required": false, - "system": false, - "type": "select", - "values": [ - "apply", - "deploy", - "all" - ] - }`)); err != nil { - return err - } - - return app.Save(collection) - }) -} diff --git a/migrations/1737141501_collections_snapshot.go b/migrations/1739462400_collections_snapshot.go similarity index 80% rename from migrations/1737141501_collections_snapshot.go rename to migrations/1739462400_collections_snapshot.go index 1a933d41..523792c7 100644 --- a/migrations/1737141501_collections_snapshot.go +++ b/migrations/1739462400_collections_snapshot.go @@ -1,12 +1,116 @@ package migrations import ( + x509 "crypto/x509" + "log/slog" + "strings" + "github.com/pocketbase/pocketbase/core" m "github.com/pocketbase/pocketbase/migrations" + + "github.com/usual2970/certimate/internal/pkg/utils/certs" ) func init() { m.Register(func(app core.App) error { + slog.Info("[CERTIMATE] migration: ready ...") + + // backup collection records + collectionRecords := make([]*core.Record, 0) + collections, err := app.FindAllCollections(core.CollectionTypeBase) + if err != nil { + return err + } else { + for _, collection := range collections { + switch collection.Name { + case "acme_accounts", "access", "certificate", "workflow", "settings": + { + records, err := app.FindAllRecords(collection) + if err != nil { + return err + } + collectionRecords = append(collectionRecords, records...) + + slog.Info("[CERTIMATE] migration: collection '" + collection.Name + "' backed up") + + if collection.Name == "access" { + collection.Fields.RemoveByName("usage") + + for i, field := range collection.Fields { + if field.GetName() == "provider" { + collection.Fields.AddMarshaledJSONAt(i+1, []byte(`{ + "hidden": false, + "id": "hwy7m03o", + "maxSelect": 1, + "name": "provider", + "presentable": false, + "required": false, + "system": false, + "type": "select", + "values": [ + "1panel", + "acmehttpreq", + "akamai", + "aliyun", + "aws", + "azure", + "baiducloud", + "baishan", + "baotapanel", + "byteplus", + "cachefly", + "cdnfly", + "cloudflare", + "cloudns", + "cmcccloud", + "ctcccloud", + "cucccloud", + "dogecloud", + "edgio", + "fastly", + "gname", + "gcore", + "godaddy", + "goedge", + "huaweicloud", + "k8s", + "local", + "namedotcom", + "namesilo", + "ns1", + "powerdns", + "qiniu", + "rainyun", + "safeline", + "ssh", + "tencentcloud", + "ucloud", + "volcengine", + "webhook", + "westcn" + ] + }`)) + } + } + + err := app.Save(collection) + if err != nil { + return err + } + } + } + + case "domains", "deployments", "access_groups": + { + app.Delete(collection) + + slog.Info("[CERTIMATE] migration: collection '" + collection.Name + "' truncated") + } + } + } + } + + // migrate jsonData := `[ { "createRule": null, @@ -50,27 +154,46 @@ func init() { "system": false, "type": "select", "values": [ + "1panel", "acmehttpreq", + "akamai", "aliyun", "aws", "azure", "baiducloud", + "baishan", + "baotapanel", "byteplus", + "cachefly", + "cdnfly", "cloudflare", + "cloudns", + "cmcccloud", + "ctcccloud", + "cucccloud", "dogecloud", + "edgio", + "fastly", + "gname", + "gcore", "godaddy", + "goedge", "huaweicloud", "k8s", "local", "namedotcom", "namesilo", + "ns1", "powerdns", "qiniu", + "rainyun", + "safeline", "ssh", "tencentcloud", "ucloud", "volcengine", - "webhook" + "webhook", + "westcn" ] }, { @@ -83,21 +206,6 @@ func init() { "system": false, "type": "json" }, - { - "hidden": false, - "id": "hsxcnlvd", - "maxSelect": 1, - "name": "usage", - "presentable": false, - "required": false, - "system": false, - "type": "select", - "values": [ - "apply", - "deploy", - "all" - ] - }, { "hidden": false, "id": "lr33hiwg", @@ -132,7 +240,8 @@ func init() { ], "id": "4yzbv8urny5ja1e", "indexes": [ - "CREATE INDEX ` + "`" + `idx_wkoST0j` + "`" + ` ON ` + "`" + `access` + "`" + ` (` + "`" + `name` + "`" + `)" + "CREATE INDEX ` + "`" + `idx_wkoST0j` + "`" + ` ON ` + "`" + `access` + "`" + ` (` + "`" + `name` + "`" + `)", + "CREATE INDEX ` + "`" + `idx_frh0JT1Aqx` + "`" + ` ON ` + "`" + `access` + "`" + ` (` + "`" + `provider` + "`" + `)" ], "listRule": null, "name": "access", @@ -450,7 +559,8 @@ func init() { "pending", "running", "succeeded", - "failed" + "failed", + "canceled" ] }, { @@ -513,7 +623,7 @@ func init() { "type": "text" }, { - "cascadeDelete": false, + "cascadeDelete": true, "collectionId": "tovyif5ax6j62ur", "hidden": false, "id": "jka88auc", @@ -525,6 +635,19 @@ func init() { "system": false, "type": "relation" }, + { + "cascadeDelete": true, + "collectionId": "qjp8lygssgwyqyz", + "hidden": false, + "id": "relation821863227", + "maxSelect": 1, + "minSelect": 0, + "name": "runId", + "presentable": false, + "required": false, + "system": false, + "type": "relation" + }, { "autogeneratePattern": "", "hidden": false, @@ -590,7 +713,10 @@ func init() { } ], "id": "bqnxb95f2cooowp", - "indexes": [], + "indexes": [ + "CREATE INDEX ` + "`" + `idx_BYoQPsz4my` + "`" + ` ON ` + "`" + `workflow_output` + "`" + ` (` + "`" + `workflowId` + "`" + `)", + "CREATE INDEX ` + "`" + `idx_O9zxLETuxJ` + "`" + ` ON ` + "`" + `workflow_output` + "`" + ` (` + "`" + `runId` + "`" + `)" + ], "listRule": null, "name": "workflow_output", "system": false, @@ -644,6 +770,20 @@ func init() { "system": false, "type": "text" }, + { + "autogeneratePattern": "", + "hidden": false, + "id": "text2069360702", + "max": 0, + "min": 0, + "name": "serialNumber", + "pattern": "", + "presentable": false, + "primaryKey": false, + "required": false, + "system": false, + "type": "text" + }, { "autogeneratePattern": "", "hidden": false, @@ -672,6 +812,20 @@ func init() { "system": false, "type": "text" }, + { + "autogeneratePattern": "", + "hidden": false, + "id": "text2910474005", + "max": 0, + "min": 0, + "name": "issuer", + "pattern": "", + "presentable": false, + "primaryKey": false, + "required": false, + "system": false, + "type": "text" + }, { "autogeneratePattern": "", "hidden": false, @@ -686,6 +840,20 @@ func init() { "system": false, "type": "text" }, + { + "autogeneratePattern": "", + "hidden": false, + "id": "text4164403445", + "max": 0, + "min": 0, + "name": "keyAlgorithm", + "pattern": "", + "presentable": false, + "primaryKey": false, + "required": false, + "system": false, + "type": "text" + }, { "hidden": false, "id": "v40aqzpd", @@ -708,6 +876,20 @@ func init() { "system": false, "type": "date" }, + { + "autogeneratePattern": "", + "hidden": false, + "id": "text2045248758", + "max": 0, + "min": 0, + "name": "acmeAccountUrl", + "pattern": "", + "presentable": false, + "primaryKey": false, + "required": false, + "system": false, + "type": "text" + }, { "exceptDomains": null, "hidden": false, @@ -743,6 +925,19 @@ func init() { "system": false, "type": "relation" }, + { + "cascadeDelete": false, + "collectionId": "qjp8lygssgwyqyz", + "hidden": false, + "id": "relation3917999135", + "maxSelect": 1, + "minSelect": 0, + "name": "workflowRunId", + "presentable": false, + "required": false, + "system": false, + "type": "relation" + }, { "autogeneratePattern": "", "hidden": false, @@ -803,7 +998,11 @@ func init() { } ], "id": "4szxr9x43tpj6np", - "indexes": [], + "indexes": [ + "CREATE INDEX ` + "`" + `idx_Jx8TXzDCmw` + "`" + ` ON ` + "`" + `certificate` + "`" + ` (` + "`" + `workflowId` + "`" + `)", + "CREATE INDEX ` + "`" + `idx_kcKpgAZapk` + "`" + ` ON ` + "`" + `certificate` + "`" + ` (` + "`" + `workflowNodeId` + "`" + `)", + "CREATE INDEX ` + "`" + `idx_2cRXqNDyyp` + "`" + ` ON ` + "`" + `certificate` + "`" + ` (` + "`" + `workflowRunId` + "`" + `)" + ], "listRule": null, "name": "certificate", "system": false, @@ -830,7 +1029,7 @@ func init() { "type": "text" }, { - "cascadeDelete": false, + "cascadeDelete": true, "collectionId": "tovyif5ax6j62ur", "hidden": false, "id": "m8xfsyyy", @@ -855,7 +1054,8 @@ func init() { "pending", "running", "succeeded", - "failed" + "failed", + "canceled" ] }, { @@ -1517,8 +1717,84 @@ func init() { "viewRule": "@request.auth.id != '' && recordRef = @request.auth.id && collectionRef = @request.auth.collectionId" } ]` + err = app.ImportCollectionsByMarshaledJSON([]byte(jsonData), false) + if err != nil { + return err + } - return app.ImportCollectionsByMarshaledJSON([]byte(jsonData), false) + slog.Info("[CERTIMATE] migration: collections imported") + + // restore records + for _, record := range collectionRecords { + changed := false + + switch record.Collection().Name { + case "access": + { + if record.GetString("provider") == "tencent" { + record.Set("provider", "tencentcloud") + changed = true + } else if record.GetString("provider") == "pdns" { + record.Set("provider", "powerdns") + changed = true + } else if record.GetString("provider") == "httpreq" { + record.Set("provider", "acmehttpreq") + changed = true + } + } + + case "certificate": + { + if record.GetString("issuer") == "" { + cert, _ := certs.ParseCertificateFromPEM(record.GetString("certificate")) + if cert != nil { + record.Set("issuer", strings.Join(cert.Issuer.Organization, ";")) + changed = true + } + } + if record.GetString("serialNumber") == "" { + cert, _ := certs.ParseCertificateFromPEM(record.GetString("certificate")) + if cert != nil { + record.Set("serialNumber", strings.ToUpper(cert.SerialNumber.Text(16))) + changed = true + } + } + if record.GetString("keyAlgorithm") == "" { + cert, _ := certs.ParseCertificateFromPEM(record.GetString("certificate")) + if cert != nil { + switch cert.SignatureAlgorithm { + case x509.SHA256WithRSA, x509.SHA256WithRSAPSS: + record.Set("keyAlgorithm", "RSA2048") + case x509.SHA384WithRSA, x509.SHA384WithRSAPSS: + record.Set("keyAlgorithm", "RSA3072") + case x509.SHA512WithRSA, x509.SHA512WithRSAPSS: + record.Set("keyAlgorithm", "RSA4096") + case x509.ECDSAWithSHA256: + record.Set("keyAlgorithm", "EC256") + case x509.ECDSAWithSHA384: + record.Set("keyAlgorithm", "EC384") + case x509.ECDSAWithSHA512: + record.Set("keyAlgorithm", "EC512") + } + changed = true + } + } + } + } + + if changed { + err = app.Save(record) + if err != nil { + return err + } + + slog.Info("[CERTIMATE] migration: collection '" + record.Collection().Name + "' record #" + record.Id + " updated") + } + } + + slog.Info("[CERTIMATE] migration: done") + + return nil }, func(app core.App) error { return nil }) diff --git a/migrations/1739462401_superusers_initial.go b/migrations/1739462401_superusers_initial.go new file mode 100644 index 00000000..d3098adb --- /dev/null +++ b/migrations/1739462401_superusers_initial.go @@ -0,0 +1,44 @@ +package migrations + +import ( + "os" + "strings" + + "github.com/pocketbase/pocketbase/core" + m "github.com/pocketbase/pocketbase/migrations" +) + +func init() { + m.Register(func(app core.App) error { + superusers, err := app.FindCollectionByNameOrId(core.CollectionNameSuperusers) + if err != nil { + return err + } + + records, err := app.FindAllRecords(superusers) + if err != nil { + return err + } + + if len(records) == 0 { + envUsername := strings.TrimSpace(os.Getenv("CERTIMATE_ADMIN_USERNAME")) + if envUsername == "" { + envUsername = "admin@certimate.fun" + } + + envPassword := strings.TrimSpace(os.Getenv("CERTIMATE_ADMIN_PASSWORD")) + if envPassword == "" { + envPassword = "1234567890" + } + + record := core.NewRecord(superusers) + record.Set("email", envUsername) + record.Set("password", envPassword) + return app.Save(record) + } + + return nil + }, func(app core.App) error { + return nil + }) +}