From 6f7f7692e1b4eb95f84cc754515a79afae185dc3 Mon Sep 17 00:00:00 2001 From: qwerty287 <80460567+qwerty287@users.noreply.github.com> Date: Thu, 28 Nov 2024 10:27:28 +0200 Subject: [PATCH] Rename token fields (#4471) Co-authored-by: Anbraten <6918444+anbraten@users.noreply.github.com> --- server/model/user.go | 4 +- .../migration/021_rename_token_fields.go | 41 +++++++++++++++++++ server/store/datastore/migration/migration.go | 1 + 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 server/store/datastore/migration/021_rename_token_fields.go diff --git a/server/model/user.go b/server/model/user.go index f389be4df..4cc49b6c5 100644 --- a/server/model/user.go +++ b/server/model/user.go @@ -44,10 +44,10 @@ type User struct { Login string `json:"login" xorm:"UNIQUE 'login'"` // AccessToken is the oauth2 access token. - AccessToken string `json:"-" xorm:"TEXT 'token'"` + AccessToken string `json:"-" xorm:"TEXT 'access_token'"` // RefreshToken is the oauth2 refresh token. - RefreshToken string `json:"-" xorm:"TEXT 'secret'"` + RefreshToken string `json:"-" xorm:"TEXT 'refresh_token'"` // Expiry is the AccessToken expiration timestamp (unix seconds). Expiry int64 `json:"-" xorm:"expiry"` diff --git a/server/store/datastore/migration/021_rename_token_fields.go b/server/store/datastore/migration/021_rename_token_fields.go new file mode 100644 index 000000000..34d07b387 --- /dev/null +++ b/server/store/datastore/migration/021_rename_token_fields.go @@ -0,0 +1,41 @@ +// Copyright 2024 Woodpecker Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package migration + +import ( + "src.techknowlogick.com/xormigrate" + "xorm.io/xorm" +) + +var renameTokenFields = xormigrate.Migration{ + ID: "rename-token-fields", + MigrateSession: func(sess *xorm.Session) (err error) { + type users struct { + AccessToken string `xorm:"TEXT 'token'"` + RefreshToken string `xorm:"TEXT 'secret'"` + } + + // ensure columns to rename exist + if err := sess.Sync(new(users)); err != nil { + return err + } + + if err := renameColumn(sess, "users", "token", "access_token"); err != nil { + return err + } + + return renameColumn(sess, "users", "secret", "refresh_token") + }, +} diff --git a/server/store/datastore/migration/migration.go b/server/store/datastore/migration/migration.go index 9fde10f54..8826f68f9 100644 --- a/server/store/datastore/migration/migration.go +++ b/server/store/datastore/migration/migration.go @@ -49,6 +49,7 @@ var migrationTasks = []*xormigrate.Migration{ &correctPotentialCorruptOrgsUsersRelation, &gatedToRequireApproval, &removeRepoNetrcOnlyTrusted, + &renameTokenFields, } var allBeans = []any{