Skip to content

Commit 2ebe609

Browse files
authored
Fix migration v141 (#14387)
* Fix mig 141 * Add Migration to fix it * update null values to false first * Alter Table if posible * use dropTableColumns instead of recreateTable * MySQL use Alter * Postgres use Alter * Update models/migrations/v167.go * Apply suggestions from code review * use 2x add col & 2x update & 2x drop col * let sqlite be the only issue * use recreate since it just WORKS
1 parent 3599d44 commit 2ebe609

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ var migrations = []Migration{
281281
NewMigration("Where Password is Valid with Empty String delete it", recalculateUserEmptyPWD),
282282
// v167 -> v168
283283
NewMigration("Add user redirect", addUserRedirect),
284+
// v168 -> v169
285+
NewMigration("Recreate user table to fix default values", recreateUserTableToFixDefaultValues),
284286
}
285287

286288
// GetCurrentDBVersion returns the current db version

models/migrations/v141.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
func addKeepActivityPrivateUserColumn(x *xorm.Engine) error {
1414
type User struct {
15-
KeepActivityPrivate bool
15+
KeepActivityPrivate bool `xorm:"NOT NULL DEFAULT false"`
1616
}
1717

1818
if err := x.Sync2(new(User)); err != nil {

models/migrations/v168.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"xorm.io/builder"
9+
"xorm.io/xorm"
10+
"xorm.io/xorm/schemas"
11+
)
12+
13+
func recreateUserTableToFixDefaultValues(x *xorm.Engine) error {
14+
type User struct {
15+
ID int64 `xorm:"pk autoincr"`
16+
LowerName string `xorm:"UNIQUE NOT NULL"`
17+
Name string `xorm:"UNIQUE NOT NULL"`
18+
FullName string
19+
Email string `xorm:"NOT NULL"`
20+
KeepEmailPrivate bool
21+
EmailNotificationsPreference string `xorm:"VARCHAR(20) NOT NULL DEFAULT 'enabled'"`
22+
Passwd string `xorm:"NOT NULL"`
23+
PasswdHashAlgo string `xorm:"NOT NULL DEFAULT 'argon2'"`
24+
25+
MustChangePassword bool `xorm:"NOT NULL DEFAULT false"`
26+
27+
LoginType int
28+
LoginSource int64 `xorm:"NOT NULL DEFAULT 0"`
29+
LoginName string
30+
Type int
31+
Location string
32+
Website string
33+
Rands string `xorm:"VARCHAR(10)"`
34+
Salt string `xorm:"VARCHAR(10)"`
35+
Language string `xorm:"VARCHAR(5)"`
36+
Description string
37+
38+
CreatedUnix int64 `xorm:"INDEX created"`
39+
UpdatedUnix int64 `xorm:"INDEX updated"`
40+
LastLoginUnix int64 `xorm:"INDEX"`
41+
42+
LastRepoVisibility bool
43+
MaxRepoCreation int `xorm:"NOT NULL DEFAULT -1"`
44+
45+
// Permissions
46+
IsActive bool `xorm:"INDEX"`
47+
IsAdmin bool
48+
IsRestricted bool `xorm:"NOT NULL DEFAULT false"`
49+
AllowGitHook bool
50+
AllowImportLocal bool
51+
AllowCreateOrganization bool `xorm:"DEFAULT true"`
52+
ProhibitLogin bool `xorm:"NOT NULL DEFAULT false"`
53+
54+
// Avatar
55+
Avatar string `xorm:"VARCHAR(2048) NOT NULL"`
56+
AvatarEmail string `xorm:"NOT NULL"`
57+
UseCustomAvatar bool
58+
59+
// Counters
60+
NumFollowers int
61+
NumFollowing int `xorm:"NOT NULL DEFAULT 0"`
62+
NumStars int
63+
NumRepos int
64+
65+
// For organization
66+
NumTeams int
67+
NumMembers int
68+
Visibility int `xorm:"NOT NULL DEFAULT 0"`
69+
RepoAdminChangeTeamAccess bool `xorm:"NOT NULL DEFAULT false"`
70+
71+
// Preferences
72+
DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"`
73+
Theme string `xorm:"NOT NULL DEFAULT ''"`
74+
KeepActivityPrivate bool `xorm:"NOT NULL DEFAULT false"`
75+
}
76+
77+
if _, err := x.Where(builder.IsNull{"keep_activity_private"}).
78+
Cols("keep_activity_private").
79+
Update(User{KeepActivityPrivate: false}); err != nil {
80+
return err
81+
}
82+
83+
switch x.Dialect().URI().DBType {
84+
case schemas.MYSQL:
85+
_, err := x.Exec("ALTER TABLE `user` MODIFY COLUMN keep_activity_private tinyint(1) DEFAULT 0 NOT NULL;")
86+
return err
87+
case schemas.POSTGRES:
88+
if _, err := x.Exec("ALTER TABLE `user` ALTER COLUMN keep_activity_private SET NOT NULL;"); err != nil {
89+
return err
90+
}
91+
_, err := x.Exec("ALTER TABLE `user` ALTER COLUMN keep_activity_private SET DEFAULT false;")
92+
return err
93+
}
94+
95+
sess := x.NewSession()
96+
defer sess.Close()
97+
if err := sess.Begin(); err != nil {
98+
return err
99+
}
100+
101+
if err := recreateTable(sess, new(User)); err != nil {
102+
return err
103+
}
104+
105+
return sess.Commit()
106+
}

0 commit comments

Comments
 (0)