Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix migration v141 #14387

Merged
merged 24 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5ca9235
Fix mig 141
6543 Jan 18, 2021
4e2b63d
Add Migration to fix it
6543 Jan 18, 2021
2579a16
update null values to false first
6543 Jan 18, 2021
f340581
Alter Table if posible
6543 Jan 19, 2021
d43a529
use dropTableColumns instead of recreateTable
6543 Jan 19, 2021
76cbc60
MySQL use Alter
6543 Jan 19, 2021
d339e41
Postgres use Alter
6543 Jan 19, 2021
a9c7a23
Update models/migrations/v167.go
6543 Jan 20, 2021
b9524f3
Apply suggestions from code review
6543 Jan 20, 2021
7ad2a22
use 2x add col & 2x update & 2x drop col
6543 Jan 22, 2021
f133ec7
Merge branch 'master' into fix-db-migration_141
6543 Jan 22, 2021
7b36b45
Merge branch 'master' into fix-db-migration_141
6543 Jan 22, 2021
74510e6
Merge branch 'master' into fix-db-migration_141
6543 Jan 23, 2021
6a7130c
let sqlite be the only issue
6543 Jan 23, 2021
0087076
use recreate since it just WORKS
6543 Jan 23, 2021
f934e44
Merge branch 'master' into fix-db-migration_141
6543 Jan 23, 2021
245b671
Merge branch 'master' into fix-db-migration_141
6543 Jan 23, 2021
c32e1b1
Merge branch 'master' into fix-db-migration_141
6543 Jan 24, 2021
2b9e079
Merge branch 'master' into fix-db-migration_141
6543 Jan 24, 2021
4f597ea
Merge branch 'master' into fix-db-migration_141
6543 Jan 26, 2021
70d8fe9
Merge branch 'master' into fix-db-migration_141
6543 Jan 26, 2021
b51ac5d
Merge branch 'master' into fix-db-migration_141
6543 Jan 27, 2021
c40e6bf
Merge branch 'master' into fix-db-migration_141
6543 Jan 28, 2021
5a7ca76
Merge branch 'master' into fix-db-migration_141
6543 Jan 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ var migrations = []Migration{
NewMigration("Convert hook task type from char(16) to varchar(16) and trim the column", convertHookTaskTypeToVarcharAndTrim),
// v166 -> v167
NewMigration("Where Password is Valid with Empty String delete it", recalculateUserEmptyPWD),
// v167 -> v168
NewMigration("Recreate user table to fix default values", recreateUserTableToFixDefaultValues),
}

// GetCurrentDBVersion returns the current db version
Expand Down
2 changes: 1 addition & 1 deletion models/migrations/v141.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func addKeepActivityPrivateUserColumn(x *xorm.Engine) error {
type User struct {
KeepActivityPrivate bool
KeepActivityPrivate bool `xorm:"NOT NULL DEFAULT false"`
}

if err := x.Sync2(new(User)); err != nil {
Expand Down
69 changes: 69 additions & 0 deletions models/migrations/v167.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"xorm.io/builder"
"xorm.io/xorm"
"xorm.io/xorm/schemas"
)

func recreateUserTableToFixDefaultValues(x *xorm.Engine) error {
type User struct {
ID int64 `xorm:"pk autoincr"`
KeepActivityPrivate bool `xorm:"NOT NULL DEFAULT false"`
TmpCol bool `xorm:"NOT NULL DEFAULT false"`
}

if _, err := x.Where(builder.IsNull{"keep_activity_private"}).
6543 marked this conversation as resolved.
Show resolved Hide resolved
Cols("keep_activity_private").
Update(User{KeepActivityPrivate: false}); err != nil {
return err
}

switch x.Dialect().URI().DBType {
6543 marked this conversation as resolved.
Show resolved Hide resolved
6543 marked this conversation as resolved.
Show resolved Hide resolved
case schemas.MYSQL:
_, err := x.Exec("ALTER TABLE `user` MODIFY COLUMN keep_activity_private tinyint(1) DEFAULT 0 NOT NULL;")
return err
case schemas.POSTGRES:
if _, err := x.Exec("ALTER TABLE `user` ALTER COLUMN keep_activity_private SET NOT NULL;"); err != nil {
return err
}
_, err := x.Exec("ALTER TABLE `user` ALTER COLUMN keep_activity_private SET DEFAULT false;")
return err
}
6543 marked this conversation as resolved.
Show resolved Hide resolved

6543 marked this conversation as resolved.
Show resolved Hide resolved
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}

if err := sess.Sync2(new(User)); err != nil {
return err
}

if _, err := sess.Exec("UPDATE `user` SET tmp_col=keep_activity_private;"); err != nil {
return err
}

if err := dropTableColumns(sess, "user", "keep_activity_private"); err != nil {
return err
}

if err := sess.Sync2(new(User)); err != nil {
return err
}

if _, err := sess.Exec("UPDATE `user` SET keep_activity_private=tmp_col;"); err != nil {
return err
}

if err := dropTableColumns(sess, "user", "tmp_col"); err != nil {
return err
}

return sess.Commit()
}