Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix(migrations): Fix incorrect database field indexes during initialization data and system upgrade

LinkinStar88160ed51212
diff --git a/internal/migrations/v1.go b/internal/migrations/v1.go
index b9e316d9..e929d45c 100644
--- a/internal/migrations/v1.go
+++ b/internal/migrations/v1.go
@@ -6,6 +6,8 @@ import (
func addUserLanguage(x *xorm.Engine) error {
type User struct {
+ ID string `xorm:"not null pk autoincr BIGINT(20) id"`
+ Username string `xorm:"not null default '' VARCHAR(50) UNIQUE username"`
Language string `xorm:"not null default '' VARCHAR(100) language"`
}
return x.Sync(new(User))
diff --git a/internal/migrations/v2.go b/internal/migrations/v2.go
index b006daf8..368124ef 100644
--- a/internal/migrations/v2.go
+++ b/internal/migrations/v2.go
@@ -6,8 +6,10 @@ import (
func addTagRecommendedAndReserved(x *xorm.Engine) error {
type Tag struct {
- Recommend bool `xorm:"not null default false BOOL recommend"`
- Reserved bool `xorm:"not null default false BOOL reserved"`
+ ID string `xorm:"not null pk comment('tag_id') BIGINT(20) id"`
+ SlugName string `xorm:"not null default '' unique VARCHAR(35) slug_name"`
+ Recommend bool `xorm:"not null default false BOOL recommend"`
+ Reserved bool `xorm:"not null default false BOOL reserved"`
}
return x.Sync(new(Tag))
}
diff --git a/internal/migrations/v3.go b/internal/migrations/v3.go
index e1f95bb4..076a72b5 100644
--- a/internal/migrations/v3.go
+++ b/internal/migrations/v3.go
@@ -95,6 +95,7 @@ ON "question" (
// only increasing field length to 128
type Config struct {
+ ID int `xorm:"not null pk autoincr INT(11) id"`
Key string `xorm:"unique VARCHAR(128) key"`
}
if err := x.Sync(new(Config)); err != nil {
@@ -172,22 +173,34 @@ ON "question" (
}
type Revision struct {
- ReviewUserID int64 `xorm:"not null default 0 BIGINT(20) review_user_id"`
+ ID string `xorm:"not null pk autoincr BIGINT(20) id"`
+ ObjectID string `xorm:"not null default 0 BIGINT(20) INDEX object_id"`
+ ReviewUserID int64 `xorm:"not null default 0 BIGINT(20) review_user_id"`
}
type Activity struct {
+ ID string `xorm:"not null pk autoincr BIGINT(20) id"`
CancelledAt time.Time `xorm:"TIMESTAMP cancelled_at"`
+ UserID string `xorm:"not null index BIGINT(20) user_id"`
+ TriggerUserID int64 `xorm:"not null default 0 index BIGINT(20) trigger_user_id"`
+ ObjectID string `xorm:"not null default 0 index BIGINT(20) object_id"`
RevisionID int64 `xorm:"not null default 0 BIGINT(20) revision_id"`
OriginalObjectID string `xorm:"not null default 0 BIGINT(20) original_object_id"`
}
type Tag struct {
- UserID string `xorm:"not null default 0 BIGINT(20) user_id"`
+ ID string `xorm:"not null pk comment('tag_id') BIGINT(20) id"`
+ SlugName string `xorm:"not null default '' unique VARCHAR(35) slug_name"`
+ UserID string `xorm:"not null default 0 BIGINT(20) user_id"`
}
type Question struct {
+ ID string `xorm:"not null pk BIGINT(20) id"`
+ UserID string `xorm:"not null default 0 BIGINT(20) INDEX user_id"`
UpdatedAt time.Time `xorm:"updated_at TIMESTAMP"`
LastEditUserID string `xorm:"not null default 0 BIGINT(20) last_edit_user_id"`
PostUpdateTime time.Time `xorm:"post_update_time TIMESTAMP"`
}
type Answer struct {
+ ID string `xorm:"not null pk autoincr BIGINT(20) id"`
+ UserID string `xorm:"not null default 0 BIGINT(20) INDEX user_id"`
UpdatedAt time.Time `xorm:"updated_at TIMESTAMP"`
LastEditUserID string `xorm:"not null default 0 BIGINT(20) last_edit_user_id"`
}