From bb1fe3675c01c884b1b7f3add1850f20fda74883 Mon Sep 17 00:00:00 2001 From: rn-h Date: Fri, 24 May 2024 08:50:39 +0800 Subject: [PATCH 1/2] Add rollback --- DBPacth/v1.0.1-v1.0.2.sql | 2 + code-push.sql | 1 + main.go | 1 + model/deploymentVersion.go | 4 ++ model/package.go | 30 +++++++++----- request/app.go | 85 ++++++++++++++++++++++++++++++++++---- 6 files changed, 104 insertions(+), 19 deletions(-) create mode 100644 DBPacth/v1.0.1-v1.0.2.sql diff --git a/DBPacth/v1.0.1-v1.0.2.sql b/DBPacth/v1.0.1-v1.0.2.sql new file mode 100644 index 0000000..25a5992 --- /dev/null +++ b/DBPacth/v1.0.1-v1.0.2.sql @@ -0,0 +1,2 @@ +ALTER TABLE `package` +ADD COLUMN `deployment_version_id` INT NULL AFTER `deployment_id`; \ No newline at end of file diff --git a/code-push.sql b/code-push.sql index 645eee5..445684e 100644 --- a/code-push.sql +++ b/code-push.sql @@ -109,6 +109,7 @@ DROP TABLE IF EXISTS `package`; CREATE TABLE `package` ( `id` int NOT NULL AUTO_INCREMENT, `deployment_id` int DEFAULT NULL, + `deployment_version_id` int DEFAULT NULL, `size` bigint DEFAULT NULL, `hash` varchar(256) DEFAULT NULL, `description` TEXT DEFAULT NULL, diff --git a/main.go b/main.go index 0a5a9fc..ca2553b 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,7 @@ func main() { authApi.POST("/lsDeployment", request.App{}.LsDeployment) authApi.GET("/lsApp", request.App{}.LsApp) authApi.POST("/uploadBundle", request.App{}.UploadBundle) + authApi.POST("/rollback", request.App{}.Rollback) } g.Run(configs.Port) diff --git a/model/deploymentVersion.go b/model/deploymentVersion.go index 360e2a3..9cd0e6a 100644 --- a/model/deploymentVersion.go +++ b/model/deploymentVersion.go @@ -31,3 +31,7 @@ func (DeploymentVersion) GetNewVersionByKeyDeploymentId(deploymentId int) *Deplo } return deploymentVersion } + +func (DeploymentVersion) UpdateCurrentPackage(id int, pid *int) { + userDb.Raw("update deployment_version set current_package=? where id=?", pid, id).Scan(&DeploymentVersion{}) +} diff --git a/model/package.go b/model/package.go index c75ce0a..1c4e000 100644 --- a/model/package.go +++ b/model/package.go @@ -1,16 +1,17 @@ package model type Package struct { - Id *int `gorm:"primarykey;autoIncrement;size:32"` - DeploymentId *int `json:"deploymentId"` - Size *int64 `json:"size"` - Hash *string `json:"hash"` - Download *string `json:"download"` - Active *int `json:"active"` - Failed *int `json:"failed"` - Installed *int `json:"installed"` - CreateTime *int64 `json:"create_time"` - Description *string `json:"description"` + Id *int `gorm:"primarykey;autoIncrement;size:32"` + DeploymentId *int `json:"deploymentId"` + DeploymentVersionId *int `json:"deploymentVersionId"` + Size *int64 `json:"size"` + Hash *string `json:"hash"` + Download *string `json:"download"` + Active *int `json:"active"` + Failed *int `json:"failed"` + Installed *int `json:"installed"` + CreateTime *int64 `json:"create_time"` + Description *string `json:"description"` } func (Package) TableName() string { @@ -28,3 +29,12 @@ func (Package) AddFailed(pid int) { func (Package) AddInstalled(pid int) { userDb.Raw("update package set installed=installed+1 where id=?", pid).Scan(&Package{}) } + +func (Package) GetRollbackPack(deploymentId int, lastPakcId int, deploymentVersionId int) *Package { + var lastPackage *Package + err := userDb.Where("deployment_id=?", deploymentId).Where("id Date: Fri, 24 May 2024 15:27:08 +0800 Subject: [PATCH 2/2] Description Optional --- README.md | 5 ----- request/app.go | 2 +- request/client.go | 30 +++++++++++++++++------------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index eae3653..bf86ee5 100644 --- a/README.md +++ b/README.md @@ -120,10 +120,5 @@ chmod +x code-push-go ${CODE_PUSH_SERVER_URL} ``` -## Developing -- [X] Delete app -- [X] Delete deployment -- [ ] Rollback bundel - ## License MIT License [Read](https://github.com/htdcx/code-push-server-go/blob/main/LICENSE) diff --git a/request/app.go b/request/app.go index 724d29e..e28db1b 100644 --- a/request/app.go +++ b/request/app.go @@ -453,7 +453,7 @@ func (App) Rollback(ctx *gin.Context) { log.Panic("Version not found") } if deploymentVersion.CurrentPackage == nil { - log.Panic("Package is last") + log.Panic("There is no upload package for the current version") } newPackage := model.Package{}.GetRollbackPack(*deployment.Id, *deploymentVersion.CurrentPackage, *deploymentVersion.Id) diff --git a/request/client.go b/request/client.go index 5418df1..1b7c866 100644 --- a/request/client.go +++ b/request/client.go @@ -15,7 +15,7 @@ import ( type Client struct{} type updateInfo struct { - DownloadUrl string `json:"download_url"` + DownloadUrl string `json:"download_url"` Description string `json:"description"` IsAvailable bool `json:"is_available"` IsDisabled bool `json:"is_disabled"` @@ -51,18 +51,22 @@ func (Client) CheckUpdate(ctx *gin.Context) { } deploymentVersion := model.DeploymentVersion{}.GetByKeyDeploymentIdAndVersion(*deployment.Id, appVersion) if deploymentVersion != nil { - packag := model.GetOne[model.Package]("id", deploymentVersion.CurrentPackage) - if packag != nil { - // && *packag.Hash != packageHash - updateInfoRedis.TargetBinaryRange = *deploymentVersion.AppVersion - updateInfoRedis.PackageHash = *packag.Hash - updateInfoRedis.PackageSize = *packag.Size - updateInfoRedis.IsAvailable = true - updateInfoRedis.IsMandatory = true - label := strconv.Itoa(*packag.Id) - updateInfoRedis.Label = label - updateInfoRedis.DownloadUrl = config.ResourceUrl + *packag.Download - updateInfoRedis.Description = *packag.Description + if deploymentVersion.CurrentPackage != nil { + packag := model.GetOne[model.Package]("id", deploymentVersion.CurrentPackage) + if packag != nil { + // && *packag.Hash != packageHash + updateInfoRedis.TargetBinaryRange = *deploymentVersion.AppVersion + updateInfoRedis.PackageHash = *packag.Hash + updateInfoRedis.PackageSize = *packag.Size + updateInfoRedis.IsAvailable = true + updateInfoRedis.IsMandatory = true + label := strconv.Itoa(*packag.Id) + updateInfoRedis.Label = label + updateInfoRedis.DownloadUrl = config.ResourceUrl + *packag.Download + if packag.Description != nil { + updateInfoRedis.Description = *packag.Description + } + } } } deploymentVersionNew := model.DeploymentVersion{}.GetNewVersionByKeyDeploymentId(*deployment.Id)