From 59e020755f7ef0ff3df38007b83b79f263c4c0a4 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 18 Apr 2024 15:59:04 +0800 Subject: [PATCH 1/9] Fix wrong table name --- models/migrations/v1_18/v230_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/migrations/v1_18/v230_test.go b/models/migrations/v1_18/v230_test.go index 308f3a50231f6..017de967034a5 100644 --- a/models/migrations/v1_18/v230_test.go +++ b/models/migrations/v1_18/v230_test.go @@ -36,7 +36,7 @@ func Test_AddConfidentialClientColumnToOAuth2ApplicationTable(t *testing.T) { } got := []ExpectedOAuth2Application{} - if err := x.Table("o_auth2_application").Select("id, confidential_client").Find(&got); !assert.NoError(t, err) { + if err := x.Table("oauth2_application").Select("id, confidential_client").Find(&got); !assert.NoError(t, err) { return } From 7922845e7065b22221c28800926c0eb9e55045e1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 18 Apr 2024 16:33:52 +0800 Subject: [PATCH 2/9] Add test for the tablename --- models/auth/oauth2_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/models/auth/oauth2_test.go b/models/auth/oauth2_test.go index 122d43098cc3a..fe6cbf6cf2990 100644 --- a/models/auth/oauth2_test.go +++ b/models/auth/oauth2_test.go @@ -13,7 +13,12 @@ import ( "github.com/stretchr/testify/assert" ) -//////////////////// Application +func TestApplicationTableName(t *testing.T) { + e := unittest.GetXORMEngine() + tableInfo, err := e.TableInfo(new(auth_model.OAuth2Application)) + assert.NoError(t, err) + assert.EqualValues(t, "oauth2_application", tableInfo.Name) +} func TestOAuth2Application_GenerateClientSecret(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) From 437cca7f6f681be49a2b5e992ede15c6bf62aed4 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 18 Apr 2024 16:39:21 +0800 Subject: [PATCH 3/9] Fix the real problem --- models/migrations/v1_18/v230.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/migrations/v1_18/v230.go b/models/migrations/v1_18/v230.go index cf94926be17ca..935916c0b4cd9 100644 --- a/models/migrations/v1_18/v230.go +++ b/models/migrations/v1_18/v230.go @@ -13,5 +13,5 @@ func AddConfidentialClientColumnToOAuth2ApplicationTable(x *xorm.Engine) error { ConfidentialClient bool `xorm:"NOT NULL DEFAULT TRUE"` } - return x.Sync(new(OAuth2Application)) + return x.Table("oauth2_application").Sync(new(OAuth2Application)) } From 712ab94b23d2e8543c3ef8ea8084d7f1f6526e59 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 18 Apr 2024 17:10:45 +0800 Subject: [PATCH 4/9] Fix wrong fixture file name --- .../{o_auth2_application.yml => oauth2_application.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename models/migrations/fixtures/Test_AddConfidentialClientColumnToOAuth2ApplicationTable/{o_auth2_application.yml => oauth2_application.yml} (100%) diff --git a/models/migrations/fixtures/Test_AddConfidentialClientColumnToOAuth2ApplicationTable/o_auth2_application.yml b/models/migrations/fixtures/Test_AddConfidentialClientColumnToOAuth2ApplicationTable/oauth2_application.yml similarity index 100% rename from models/migrations/fixtures/Test_AddConfidentialClientColumnToOAuth2ApplicationTable/o_auth2_application.yml rename to models/migrations/fixtures/Test_AddConfidentialClientColumnToOAuth2ApplicationTable/oauth2_application.yml From f8d57dcfa0d659196d6064a648ac9b2ce38c28cc Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 19 Apr 2024 10:52:42 +0800 Subject: [PATCH 5/9] Add migration --- models/migrations/migrations.go | 2 ++ models/migrations/v1_18/v230.go | 1 + models/migrations/v1_23/v298.go | 10 ++++++++++ 3 files changed, 13 insertions(+) create mode 100644 models/migrations/v1_23/v298.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index cb3a64f48c633..220d8c2331447 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -584,6 +584,8 @@ var migrations = []Migration{ NewMigration("Add missing field of commit status summary table", v1_23.AddCommitStatusSummary2), // v297 -> v298 NewMigration("Add everyone_access_mode for repo_unit", v1_23.AddRepoUnitEveryoneAccessMode), + // v298 -> v299 + NewMigration("Drop wrongly created table o_auth2_application", v1_23.DropWronglyCreatedTable), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v1_18/v230.go b/models/migrations/v1_18/v230.go index 935916c0b4cd9..f4f88ea330295 100644 --- a/models/migrations/v1_18/v230.go +++ b/models/migrations/v1_18/v230.go @@ -10,6 +10,7 @@ import ( // AddConfidentialColumnToOAuth2ApplicationTable: add ConfidentialClient column, setting existing rows to true func AddConfidentialClientColumnToOAuth2ApplicationTable(x *xorm.Engine) error { type OAuth2Application struct { + ID int64 ConfidentialClient bool `xorm:"NOT NULL DEFAULT TRUE"` } diff --git a/models/migrations/v1_23/v298.go b/models/migrations/v1_23/v298.go new file mode 100644 index 0000000000000..8761a05d3dc1c --- /dev/null +++ b/models/migrations/v1_23/v298.go @@ -0,0 +1,10 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_23 //nolint + +import "xorm.io/xorm" + +func DropWronglyCreatedTable(x *xorm.Engine) error { + return x.DropTables("o_auth2_application") +} From 05674f7a381280d3b00b1bd12712a396dc4b63f7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 19 Apr 2024 19:50:06 +0800 Subject: [PATCH 6/9] Fix test --- models/migrations/v1_18/v230_test.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/models/migrations/v1_18/v230_test.go b/models/migrations/v1_18/v230_test.go index 017de967034a5..ff1aa4d75136f 100644 --- a/models/migrations/v1_18/v230_test.go +++ b/models/migrations/v1_18/v230_test.go @@ -11,12 +11,16 @@ import ( "github.com/stretchr/testify/assert" ) -func Test_AddConfidentialClientColumnToOAuth2ApplicationTable(t *testing.T) { - // premigration - type OAuth2Application struct { - ID int64 - } +// premigration +type OAuth2Application struct { + ID int64 +} +func (OAuth2Application) TableName() string { + return "oauth2_application" +} + +func Test_AddConfidentialClientColumnToOAuth2ApplicationTable(t *testing.T) { // Prepare and load the testing database x, deferable := base.PrepareTestEnv(t, 0, new(OAuth2Application)) defer deferable() From 99fef44280ff8c00b8efc274e8fbca9a0d464344 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 20 Apr 2024 20:19:59 +0800 Subject: [PATCH 7/9] Follow wxiaoguang's suggestion --- models/auth/oauth2_test.go | 7 ------- models/migrations/v1_18/v230.go | 16 ++++++++++------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/models/auth/oauth2_test.go b/models/auth/oauth2_test.go index fe6cbf6cf2990..0829d31d51baf 100644 --- a/models/auth/oauth2_test.go +++ b/models/auth/oauth2_test.go @@ -13,13 +13,6 @@ import ( "github.com/stretchr/testify/assert" ) -func TestApplicationTableName(t *testing.T) { - e := unittest.GetXORMEngine() - tableInfo, err := e.TableInfo(new(auth_model.OAuth2Application)) - assert.NoError(t, err) - assert.EqualValues(t, "oauth2_application", tableInfo.Name) -} - func TestOAuth2Application_GenerateClientSecret(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) app := unittest.AssertExistsAndLoadBean(t, &auth_model.OAuth2Application{ID: 1}) diff --git a/models/migrations/v1_18/v230.go b/models/migrations/v1_18/v230.go index f4f88ea330295..4edbdb3f8763e 100644 --- a/models/migrations/v1_18/v230.go +++ b/models/migrations/v1_18/v230.go @@ -7,12 +7,16 @@ import ( "xorm.io/xorm" ) +type oAuth2Application struct { + ID int64 + ConfidentialClient bool `xorm:"NOT NULL DEFAULT TRUE"` +} + +func (oAuth2Application) TableName() string { + return "oauth2_application" +} + // AddConfidentialColumnToOAuth2ApplicationTable: add ConfidentialClient column, setting existing rows to true func AddConfidentialClientColumnToOAuth2ApplicationTable(x *xorm.Engine) error { - type OAuth2Application struct { - ID int64 - ConfidentialClient bool `xorm:"NOT NULL DEFAULT TRUE"` - } - - return x.Table("oauth2_application").Sync(new(OAuth2Application)) + return x.Sync(new(oAuth2Application)) } From c073714c387919927b9f0ea1538e8c1b0ec55c0e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 22 Apr 2024 14:05:53 +0800 Subject: [PATCH 8/9] Follow wxiaoguang's suggestion --- models/migrations/v1_18/v230.go | 15 +++++---------- models/migrations/v1_18/v230_test.go | 16 ++++++---------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/models/migrations/v1_18/v230.go b/models/migrations/v1_18/v230.go index 4edbdb3f8763e..ea5b4d02e1f26 100644 --- a/models/migrations/v1_18/v230.go +++ b/models/migrations/v1_18/v230.go @@ -7,16 +7,11 @@ import ( "xorm.io/xorm" ) -type oAuth2Application struct { - ID int64 - ConfidentialClient bool `xorm:"NOT NULL DEFAULT TRUE"` -} - -func (oAuth2Application) TableName() string { - return "oauth2_application" -} - // AddConfidentialColumnToOAuth2ApplicationTable: add ConfidentialClient column, setting existing rows to true func AddConfidentialClientColumnToOAuth2ApplicationTable(x *xorm.Engine) error { - return x.Sync(new(oAuth2Application)) + type oauth2Application struct { + ID int64 + ConfidentialClient bool `xorm:"NOT NULL DEFAULT TRUE"` + } + return x.Sync(new(oauth2Application)) } diff --git a/models/migrations/v1_18/v230_test.go b/models/migrations/v1_18/v230_test.go index ff1aa4d75136f..fb3ba89eceb42 100644 --- a/models/migrations/v1_18/v230_test.go +++ b/models/migrations/v1_18/v230_test.go @@ -11,18 +11,14 @@ import ( "github.com/stretchr/testify/assert" ) -// premigration -type OAuth2Application struct { - ID int64 -} - -func (OAuth2Application) TableName() string { - return "oauth2_application" -} - func Test_AddConfidentialClientColumnToOAuth2ApplicationTable(t *testing.T) { + // premigration + type oauth2Application struct { + ID int64 + } + // Prepare and load the testing database - x, deferable := base.PrepareTestEnv(t, 0, new(OAuth2Application)) + x, deferable := base.PrepareTestEnv(t, 0, new(oAuth2Application)) defer deferable() if x == nil || t.Failed() { return From 4aa802dd40fe1569ae69278c6049bad0eb474bd2 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 22 Apr 2024 14:35:38 +0800 Subject: [PATCH 9/9] Fix lint --- models/migrations/v1_18/v230_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/migrations/v1_18/v230_test.go b/models/migrations/v1_18/v230_test.go index fb3ba89eceb42..40db4c2ffe20b 100644 --- a/models/migrations/v1_18/v230_test.go +++ b/models/migrations/v1_18/v230_test.go @@ -18,7 +18,7 @@ func Test_AddConfidentialClientColumnToOAuth2ApplicationTable(t *testing.T) { } // Prepare and load the testing database - x, deferable := base.PrepareTestEnv(t, 0, new(oAuth2Application)) + x, deferable := base.PrepareTestEnv(t, 0, new(oauth2Application)) defer deferable() if x == nil || t.Failed() { return