From b7a81e0dce5825e5188a2b6c72aa7e3e0c1306ce Mon Sep 17 00:00:00 2001 From: zicla Date: Mon, 20 May 2019 02:43:44 +0800 Subject: [PATCH] Finish the first version of migrate. --- code/rest/preference_controller.go | 40 +++++++++++++++++++++++++++ code/support/tank_application.go | 44 ++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/code/rest/preference_controller.go b/code/rest/preference_controller.go index 598ff50..6a03eb7 100644 --- a/code/rest/preference_controller.go +++ b/code/rest/preference_controller.go @@ -12,6 +12,7 @@ type PreferenceController struct { BaseController preferenceDao *PreferenceDao preferenceService *PreferenceService + migrating bool } func (this *PreferenceController) Init() { @@ -37,6 +38,7 @@ func (this *PreferenceController) RegisterRoutes() map[string]func(writer http.R routeMap["/api/preference/fetch"] = this.Wrap(this.Fetch, USER_ROLE_GUEST) routeMap["/api/preference/edit"] = this.Wrap(this.Edit, USER_ROLE_ADMINISTRATOR) routeMap["/api/preference/system/cleanup"] = this.Wrap(this.SystemCleanup, USER_ROLE_ADMINISTRATOR) + routeMap["/api/preference/migrate20to30"] = this.Wrap(this.Migrate20to30, USER_ROLE_ADMINISTRATOR) return routeMap } @@ -123,6 +125,44 @@ func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http return this.Success(preference) } +//migrate 2.0's db data and file data to 3.0 +func (this *PreferenceController) Migrate20to30(writer http.ResponseWriter, request *http.Request) *result.WebResult { + + this.logger.Info("start migrating from 2.0 to 3.0") + + if this.migrating { + panic(result.BadRequest("migrating work is processing")) + } else { + this.migrating = true + } + defer func() { + this.migrating = false + }() + + migrateDashboardSql := "INSERT INTO `tank`.`tank30_download_token` ( `uuid`, `sort`, `update_time`, `create_time`, `user_uuid`, `matter_uuid`, `expire_time`, `ip` ) ( SELECT `uuid`, `sort`, `update_time`, `create_time`, `user_uuid`, `matter_uuid`, `expire_time`, `ip` FROM `tank`.`tank20_download_token`)" + this.logger.Info(migrateDashboardSql) + core.CONTEXT.GetDB().Exec(migrateDashboardSql) + + migrateDownloadTokenSql := "INSERT INTO `tank`.`tank30_dashboard` ( `uuid`, `sort`, `update_time`, `create_time`, `invoke_num`, `total_invoke_num`, `uv`, `total_uv`, `matter_num`, `total_matter_num`, `file_size`, `total_file_size`, `avg_cost`, `dt` ) ( SELECT `uuid`, `sort`, `update_time`, `create_time`, `invoke_num`, `total_invoke_num`, `uv`, `total_uv`, `matter_num`, `total_matter_num`, `file_size`, `total_file_size`, `avg_cost`, `dt` FROM `tank`.`tank20_dashboard` )" + this.logger.Info(migrateDownloadTokenSql) + core.CONTEXT.GetDB().Exec(migrateDownloadTokenSql) + + migrateMatterSql := "INSERT INTO `tank`.`tank30_matter` ( `uuid`, `sort`, `update_time`, `create_time`, `puuid`, `user_uuid`, `username`, `dir`, `name`, `md5`, `size`, `privacy`, `path`, `times` ) ( SELECT `uuid`, `sort`, `update_time`, `create_time`, `puuid`, `user_uuid`, '', `dir`, `name`, `md5`, `size`, `privacy`, `path`, `times` FROM `tank`.`tank20_matter` ) " + this.logger.Info(migrateMatterSql) + core.CONTEXT.GetDB().Exec(migrateMatterSql) + + migrateUploadTokenSql := "INSERT INTO `tank`.`tank30_upload_token` ( `uuid`, `sort`, `update_time`, `create_time`, `user_uuid`, `folder_uuid`, `matter_uuid`, `expire_time`, `filename`, `privacy`, `size`, `ip` ) ( SELECT `uuid`, `sort`, `update_time`, `create_time`, `user_uuid`, `folder_uuid`, `matter_uuid`, `expire_time`, `filename`, `privacy`, `size`, `ip` FROM `tank`.`tank20_upload_token` ) " + this.logger.Info(migrateUploadTokenSql) + core.CONTEXT.GetDB().Exec(migrateUploadTokenSql) + + //username in tank2.0 add _20. + migrateUserSql := "INSERT INTO `tank`.`tank30_user` ( `uuid`, `sort`, `update_time`, `create_time`, `role`, `username`, `password`, `avatar_url`, `last_ip`, `last_time`, `size_limit`, `total_size_limit`, `total_size`, `status` ) ( SELECT `uuid`, `sort`, `update_time`, `create_time`, `role`, CONCAT(`username`,'_20') as `username`, `password`, `avatar_url`, `last_ip`, `last_time`, `size_limit`, -1, 0, `status` FROM `tank`.`tank20_user` )" + this.logger.Info(migrateUserSql) + core.CONTEXT.GetDB().Exec(migrateUserSql) + + return this.Success("OK") +} + //cleanup system data. func (this *PreferenceController) SystemCleanup(writer http.ResponseWriter, request *http.Request) *result.WebResult { diff --git a/code/support/tank_application.go b/code/support/tank_application.go index 9467372..6c12415 100644 --- a/code/support/tank_application.go +++ b/code/support/tank_application.go @@ -25,6 +25,8 @@ const ( MODE_CRAWL = "crawl" //Current version. MODE_VERSION = "version" + //migrate 2.0 to 3.0 + MODE_MIGRATE_20_TO_30 = "migrate20to30" ) type TankApplication struct { @@ -118,6 +120,10 @@ func (this *TankApplication) Start() { this.HandleMirror() + } else if strings.ToLower(this.mode) == MODE_MIGRATE_20_TO_30 { + + this.HandleMigrate20to30() + } else if strings.ToLower(this.mode) == MODE_CRAWL { this.HandleCrawl() @@ -254,3 +260,41 @@ func (this *TankApplication) HandleVersion() { fmt.Printf("EyeblueTank %s\r\n", core.VERSION) } + +//migrate 2.0 to 3.0 +func (this *TankApplication) HandleMigrate20to30() { + + if this.src == "" { + panic("src is required") + } + + fmt.Printf("start migrating 2.0 to 3.0. MatterPath2.0 = %s \r\n", this.src) + + urlString := fmt.Sprintf("%s/api/preference/migrate20to30", this.host) + + params := url.Values{ + "matterPath": {this.src}, + core.USERNAME_KEY: {this.username}, + core.PASSWORD_KEY: {this.password}, + } + + response, err := http.PostForm(urlString, params) + core.PanicError(err) + + bodyBytes, err := ioutil.ReadAll(response.Body) + + webResult := &result.WebResult{} + + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(bodyBytes, webResult) + if err != nil { + fmt.Printf("error response format %s \r\n", err.Error()) + return + } + + if webResult.Code == result.OK.Code { + fmt.Println("success") + } else { + fmt.Printf("error %s\r\n", webResult.Msg) + } + +}